题目分析
大家可以发现,如果f[i] = 1且f[i-1] = 0,那么数组f进入循环。接下来我们就需要找出f数组的循环周期,同时我们会发现,循环周期最大只可能为1000*1000,因为取模之后之后1000*1000种选择。同时a的b次方对周期进行取模,暴力显然会超时,只运用快速幂,logn的时间就能得到结果。这道题需要打表存下来,因为有1000次询问,但是只有1000种情况,所以打表就可以减少时间,查表就可以得到结果。hint:因为没有在uva上写过几次题,我不知道输出64位无符号整数是llu,我用的I64u,结果答案一直wa,后来看了别人的代码才发现别的都一样就这个不同,改了之后果然过了,真是坑呀!!
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1005;
typedef unsigned long long ull;
vector <int> vec[maxn];
int len[maxn];
void init()
{
memset(len, 0, sizeof(len));
for(int i = 2; i <= 1000; i++)
{
int a = 0,b = 1,c = 1;
vec[i].push_back(a);
vec[i].push_back(b);
vec[i].push_back(c);
while(!(b == 0 && c == 1))
{
a = b;
b = c;
c = (a+b)%i;
vec[i].push_back(c);
}
len[i] = vec[i].size()-2;
}
}
ull pow_mod(ull a, ull b, ull MOD) //快速幂
{
int ans = 1%MOD;
a = a%MOD;
while(b)
{
if(b&1)
ans = (int)(ans*a)%MOD;
a = (a*a)%MOD;
b = b/2;
}
return ans;
}
int main()
{
init();
int n,t;
ull a,b;
scanf("%d", &t);
while(t--)
{
scanf("%llu%llu%d", &a, &b, &n);
if(n == 1)
{
printf("0\n");
continue;
}
ull ans = pow_mod(a, b, len[n]);
printf("%d\n", vec[n][ans]);
}
return 0;
}