//UVA11582ColossalFibonacci
#include<cstdio>
#include<cstring>
const int maxn = 1000 + 5;
int f[maxn][maxn * 6], period[maxn];
int pow_mod(unsigned long long a, unsigned long long b, int n) {
if(b == 0) return 1;
int x = pow_mod(a, b / 2, n);//将指数折半乘
x = x * x % n;
if(b % 2 == 1) x = x * a % n;//中间还剩一个数
return x;
}
int main() {
unsigned long long a, b;
int n, T;
for(int i = 2; i <= 1000; i++) {//打表
f[i][0] = 0; f[i][1] = 1;//printf("i = %d***\n", i);
for(int j = 2; ; j++){
f[i][j] = (f[i][j - 1] + f[i][j - 2]) % i;
if(f[i][j - 1] == 0 && f[i][j] == 1) {
period[i] = j - 1; break;
}
}
}
scanf("%d", &T);
while(T--) {
scanf("%llu%llu%d", &a, &b, &n);
int tmp = (a == 0 || n == 1) ? 0 : pow_mod(a % period[n], b, period[n]);
//根据mod的性质,先对a进行预处理,一方面提升速度。另一方面防溢出
printf("%d\n", f[n][tmp]);
}
return 0;
}
/*
3
1 1 2
2 3 1000
18446744073709551615 18446744073709551615 1000
*/
UVA11582ColossalFibonacci!
最新推荐文章于 2020-01-30 13:55:56 发布