数学题。也感觉算是一个综合题吧。
Fibonacci 数列 一般是用大数解决。 但是这个地方 出现了 大数的n次方。 必然不会用到大数。 因为数列必然取不到这一项。
所以既然数列数组开不到那么大 就会必然有周期性的存在。 因为数据是达到了 2^64 long long 的数据范围是 2^63 - 1 所以longlong 是存不下来的。
用unsigned long long才行。 又因为 如果 高次幂的话 循环 肯定会超时。 所以用到了 快速幂运算。 超快。 这个题 RE了 3次。
一开始 还以为数组的问题。 开大点之后。还是不行。就读题 读了一遍。在数组里面发现问题了。在运算中 一定注意一些细节处理。
周期的寻找很简单。 再一次 出现 0 1的时候 就会是 循环的开始。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
typedef unsigned long long ull;
#define maxn 10000000+10
#define INF 1<<30
ull f[maxn];
ull pow_(ull a, ull b , ull time){
if(b == 0)
return 1;
ull x = pow_(a, b/2 , time);
ull ans = ((ull)(x % time) * (ull)(x % time)) % time;
if(b % 2 == 1)
ans = ((ans % time) * (a % time)) % time;
return ans;
}
int main (){
int counts ;
scanf("%d",&counts);
while(counts--){
ull a,b,n;
f[0] = 0;
f[1] = 1;
scanf("%llu%llu%llu",&a,&b,&n);
ull time;
if(a == 0 || n == 1){
printf("0\n");
continue;
}
for(ull i = 2; i <= n * n; i++){
f[i] = (f[i-1] + f[i-2]) % n;
if(f[i-1] == 0 && f[i] == 1){
time = i-1;
break;
}
}
ull sum = pow_(a % time, b, time);
printf("%llu\n",f[sum % time]);
}
return 0;
}