题目描述
wyh学长特别喜欢斐波那契数列,F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n>=2)
一天他突发奇想,想求F(a^b)%c
输入描述:
输入第一行一个整数T(1<=T<=100),代表测试组数
接下来T行,每行三个数 a,b,c (a,b<=2^64) (1<c<1000)
输出描述:
输出第a^b项斐波那契数对c取余的结果
示例1
输入
3
1 1 2
2 3 1000
32122142412412142 124124124412124 123
输出
1
21
3
思路详情: https://blog.csdn.net/peng0614/article/details/79833821
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
ll a, b, c;
ll f[2050000];
ll POW(ll a, ll b, ll c)
{
ll ans = 1;
while(b)
{
if(b & 1)
ans = ans * a % c;
a = a * a % c;
b >>= 1;
}
return ans;
}
int main()
{
int N;
scanf("%d", &N);
while(N--)
{
scanf("%llu%llu%llu", &a, &b, &c);
f[0] = 0;
f[1] = f[2] = 1;
ll mod;
for(int i = 3; i < c * c; i++)
{
f[i] = (f[i - 1] + f[i - 2]) % c;
if(f[i] == 0 && f[i - 1] == 1)
{
mod = i;
break;
}
}
cout << f[POW(a % mod, b % mod, mod)] << endl;
}
return 0;
}//0 1 1 2 3 5 8 13 21 34 55 89
//0 1 1 2 0 2 2 1 0 1 1 2