Hdu--3306(数学,矩阵快速幂)

2014-09-15 17:57:47

思路:单纯的矩阵构造,推下公式就行。矩阵B:{ AN^2 , AN * AN-1 , AN-1^2 , SN},要注意在初始化转移矩阵时就要取模,因X^2..过大。

 1 /*************************************************************************
 2     > File Name: 3306.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Mon 15 Sep 2014 05:20:56 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 typedef long long ll;
18 const int INF = 1 << 29;
19 const ll mod = 10007;
20 
21 ll N,X,Y;
22 
23 struct Mx{
24     ll a[4][4];
25     void clear(){ memset(a,0,sizeof(a)); }
26     void stand(){ memset(a,0,sizeof(a)); for(int i = 0; i < 4; ++i) a[i][i] = 1;}
27     Mx operator * (const Mx &b){
28         Mx c; c.clear();
29         for(int i = 0; i < 4; ++i)
30         for(int j = 0; j < 4; ++j)
31         for(int k = 0; k < 4; ++k){
32             c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % mod;
33         }
34         return c;
35     }
36 };
37 
38 Mx Mx_pow(ll num){
39     Mx res; res.stand();
40     Mx t; t.clear();
41     t.a[0][0] = t.a[3][0] = X*X % mod;
42     t.a[0][1] = t.a[3][1] = 2*X*Y % mod;
43     t.a[0][2] = t.a[3][2] = Y*Y % mod;
44     t.a[1][0] = X % mod;
45     t.a[1][1] = Y % mod;
46     t.a[2][0] = 1;
47     t.a[3][3] = 1;
48     while(num){
49         if(num & 1) res = res * t;
50         t = t * t;
51         num >>= 1;
52     }
53     return res;
54 }
55 
56 int main(){
57     while(scanf("%I64d%I64d%I64d",&N,&X,&Y) != EOF){
58         if(N == 0){
59             printf("0\n");
60             continue;
61         }
62         Mx ans; ans.clear();
63         ans.a[0][0] = 1;
64         ans.a[1][0] = 1;
65         ans.a[2][0] = 1;
66         ans.a[3][0] = 2;
67         ans = Mx_pow(N - 1) * ans;
68         printf("%I64d\n",ans.a[3][0]);
69     }
70     return 0;
71 }

 

posted @ 2014-09-15 17:59  Naturain  阅读(153)  评论(0)    收藏  举报