數學題,尋找斐波那契取模後的週期T,然後求Mod_Fib(a^b % T)即可。
求a^b % T時要用快速冪來計算,詳細方法爲http://blog.csdn.net/kun768/article/details/42344897
#include <cstdio>
#include <iostream>
using LLU = unsigned long long;
using namespace std;
int Fib[1024000];
int init(const int n) {
Fib[0] = 0, Fib[1] = 1;
for (int i = 2;; ++i){
Fib[i] = (Fib[i - 1] + Fib[i - 2]) % n;
if (Fib[i] == 1 && Fib[i - 1] == 0)
return i - 1;
}
}
int Pow(LLU a, LLU b, int Base){
LLU res = 1;
LLU t = a;
while (b > 0) {
if (b % 2) res = (res * t) % Base;
t = (t * t) % Base;
b >>= 1;
}
return (int)res;
}
int main()
{
int T; cin >> T;
while (T--) {
LLU a, b; int n; cin >> a >> b >> n;
if (n == 1) { puts("0"); continue; }
int Base = init(n);
cout << Fib[Pow(a % Base, b, Base)] << endl;
}
return 0;
}