UVa11582巨大的斐波那契数

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <iostream>
 7 #include <algorithm>
 8 using namespace std;
 9 typedef unsigned long long ull;
10 const int INF = 1 << 30;
11 
12 int t,n;
13 vector<int> fib[1005];///打表,二维数组下标表示对应的除数,对其进行取余
14 int cyc[1005];///存放除数为p的对应周期
15 ull a,b;
16 
17 void Pre_process(){
18     for(int p = 2; p <= 1000; ++p){///p表示n(即除数)
19         fib[p].push_back(0);
20         fib[p].push_back(1);
21         for(int i = 2; ; ++i){
22             fib[p].push_back((fib[p][i - 1] + fib[p][i - 2]) % p);///将余数存放在对应p的"一位数组"中
23             if(fib[p][i] == 1 && fib[p][i - 1] == 0){///找到一个循环周期
24                 cyc[p] = i - 1;///记录周期结束位置
25                 break;
26             }
27         }
28     }
29 }
30 
31 int Quick_pow_mod(ull x,ull y,int mod){///模版代码
32     int ans = 1;
33     while(y){
34         if(y & 1)   ans = (int)((ans * x) % mod);
35         x = (x * x) % mod;
36         y >>= 1;
37     }
38     return ans;
39 }
40 
41 int main(){
42     Pre_process();///打表
43     scanf("%d",&t);
44     while(t--){
45         scanf("%llu%llu%d",&a,&b,&n);
46         if(a == 0 || n == 1) printf("0\n");
47         else printf("%d\n",fib[n][Quick_pow_mod(a % cyc[n],b,cyc[n])]);
48     }
49     return 0;
50 }

 

posted @ 2016-05-10 21:55  赤云封天  阅读(207)  评论(0编辑  收藏  举报