题意:对A的B次方项斐波那契序列求余。
解题思路:根据递推关系找到余数的循环周期, 再利用快速求余确定位置。
注意n = 0, 和 1的情况
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=26
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)
int buf[10];
inline long long read()
{
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void writenum(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 1010 * 1010
const int INF = 0x3f3f3f3f;
int ans[MAX_N];
int init(int n)
{
ans[0] = 0;
ans[1] = 1;
for(int i = 2 ; i <= n * n ; i++)
{
ans[i] = (ans[i - 1] % n + ans[i - 2] % n) % n;
if(ans[i] == 1 && ans[i - 1] == 0)
{
return i - 1;
}
}
}
int pow_mod(unsigned long long a, unsigned long long n, int m)
{
a %= m;
for(int i = 1 ; ; a = a * a % m)
{
if(n & 1) i = i * a % m;
if(!(n >>= 1)) return i;
}
}
int main()
{
int t = read();
unsigned long long a, b;
while(t--)
{
scanf("%llu%llu", &a, &b);
int n = read();
if(n == 1 || n == 0)
{
printf("0\n");
continue;
}
int tmp = init(n);
int num = pow_mod(a, b, tmp);
printf("%d\n", ans[num]);
}
return 0;
}