您的位置:首页 > 其它

UVA 11582(p316)----Colossal Fibonacci Numbers!

2016-02-27 13:47 399 查看
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1000+50;
typedef unsigned long long ULL;
ULL a,b,n;
ULL flag[maxn],f[maxn][maxn*10];
ULL  Mi(ULL a,ULL b,ULL n)
{
ULL t=1,y=a;
while(b)
{
if(b&1==1) t=t*y%n;
y=y*y%n;
b=b>>1;
}
return t;
}
void prepare()
{
for(int n=2; n<=1000; n++)
{
f
[0]=0;
f
[1]=1;
for(int i=2;; i++)
{
f
[i]=(f
[i-1]+f
[i-2])%n;
if(f
[i-1]==0&&f
[i]==1)
{
flag
=i-1;
break;
}
}
}
}
void solve()
{
if(a==0||n==1) printf("0\n");
else
{
//cout<<a<<" "<<b<<" "<<flag
<<endl;
ULL tmp=Mi(a%flag
,b,flag
);
cout<<f
[tmp]<<endl;
}
}
int main()
{
int t;
prepare();
scanf("%d",&t);
while(t--)
{
cin>>a>>b>>n;
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: