hdu 2256 Problem of Precision

Problem of Precision

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1225    Accepted Submission(s): 730


Problem 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
 
Sample Output
9
97
841
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3117 2254 1588 2294 2276 
 
按照题意推就是的,代码很简单,关键是推理过程。贴一段别人的思路:
 
题意:给出一个式子,求值。
 
附上代码:
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #define mod 1024
 5 using namespace std;
 6 struct mat
 7 {
 8     int m[2][2];
 9 };
10 
11 mat mul(mat a,mat b)
12 {
13     mat c;
14     int i,j,k;
15     memset(c.m,0,sizeof(c.m));
16     for(i=0; i<2; i++)
17         for(j=0; j<2; j++)
18         {
19             for(k=0; k<2; k++)
20                 c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
21             c.m[i][j]%=mod;
22         }
23     return c;
24 }
25 
26 mat product(mat a,int k)
27 {
28     if(k==1) return a;
29     else if(k&1) return mul(product(a,k-1),a);
30     else return product(mul(a,a),k/2);
31 }
32 
33 int main()
34 {
35     int T,i,j,n,m;
36     mat a,b;
37     scanf("%d",&T);
38     while(T--)
39     {
40         scanf("%d",&n);
41         if(n==1)
42         {
43             printf("9
");
44             continue;
45         }
46         a.m[0][0]=5;
47         a.m[0][1]=12;
48         a.m[1][0]=2;
49         a.m[1][1]=5;
50         b=product(a,n-1);
51         int ans=(b.m[0][0]*5+b.m[0][1]*2)%mod;
52         ans=(ans*2-1)%mod;
53         printf("%d
",ans);
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/pshw/p/5547393.html