题目链接:Uva 11582 [vjudge]
题意
输入两个非负整数a、b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值,其中f(0) = 0,f(1) = 1;且对任意非负整数i,f(i+2)= f(i+1)+f(i)。
分析
所有的计算都是对n取模,设F(i) =f(i)mod n, 很容易发现,F(x)是具有周期性的,因为对N取模的值最多也就N个,当二元组(F(i-1),F(i))重复的时候,整个序列也就重复了,周期i – 1啊,自己可以找组小的数据研究研究,就可以发现这个规律了。
周期最大会有多大呢?由于余数最多也就N个,那么最多N^2就会重复,完全可以才时限内解决了。
剩下的知识就是针对快速幂取模了,这个在我另外一篇博客《超级快速幂【费马小定理】+【快速幂取模】》里面有比较详细的介绍。
参考代码
/****************************>>>>HEADFILES<<<<****************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
#define fst first
#define snd second
#define root 1,N,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PB(a) push_back(a)
#define MP(a,b) make_pair(a,b)
#define CASE(T) for(scanf("%d",&T);T--;)
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//typedef unsigned __int64 ull;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int maxn = 1000 + 5;
const int maxm = 20000 + 5;
/****************************>>>>SEPARATOR<<<<****************************/
int T, N;
ull a, b;
int ans[maxn*maxn];
int Fibo(const int& MOD)
{
int ret;
ans[0] = 0, ans[1] = 1 % MOD;
int i = 2;
while(1)
{
ans[i] = (ans[i - 1] + ans[i - 2]) % MOD;
if(ans[i - 1] == 0 && ans[i] == 1 % MOD) break;
i++;
}
return i - 1;
}
int pow_mod(ull x, ull y, int MOD)
{
ull ret = 1;
while(y)
{
if(y & 1) ret = (ret * x) % MOD;
x = (x * x) % MOD;
y >>= 1;
}
return (int)ret;
}
int main()
{
// FIN;
CASE(T)
{
scanf("%llu %llu %d",&a,&b,&N);
if(a == 0 || N == 1)
{
printf("0\n");
continue;
}
int Cyc = Fibo(N);
int pos = pow_mod(a % Cyc, b, Cyc);
printf("%d\n",ans[pos]);
}
return 0;
}