您的位置:首页 > 其它

hdu 2256-Problem of Precision

2014-07-30 11:04 435 查看
Description



 

Input

The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)

 

Output

For each input case, you should output the answer in one line.

 
Sample Input

3
1
2
5

---------------------------------------------------------------------------------------

看了题解才会的题`~~特别巧妙!



CODE:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

const int mod=1024;
typedef vector<int> vec;
typedef vector<vec> mat;

mat mul(mat &A,mat &B)
{
mat C(A.size(),vec(B.size()));
for(int i=0;i<A.size();i++){
for(int k=0;k<B.size();k++){
for(int j=0;j<B[0].size();j++){
C[i][j] = (C[i][j] + A[i][k]*B[k][j])%mod;
}
}
}
return C;
}
mat pow(mat &A,int nn)
{
mat B(A.size(),vec(A.size()));
for(int i=0;i<A.size();i++)
B[i][i] = 1;
while(nn > 0){
if(nn & 1) B = mul(B,A);
A = mul(A,A);
nn >>= 1;
}
return B;
}
int main()
{
//freopen("in.in","r",stdin);
int T,n;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
mat A(2,vec(2));
A[0][0] = 5;A[0][1] = 12;
A[1][0] = 2;A[1][1] = 5;
A = pow(A,n-1);
int ans = 0;
ans = (A[0][0]*5+A[0][1]*2)%mod;
printf("%d\n", (2*ans-1)%mod);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  矩阵快速幂 HDU