UVA

来源:互联网 发布:阿里云 docker 加速 编辑:程序博客网 时间:2024/05/16 07:11

题意:

求f(a^b)%n的值,因为a,b的范围非常大,2^64要用unsigned long long,用long long会出各种错误。在斐波那契中找f(a^b)会超范围,先把f[]预处理出来,找f[i]==f[2],f[i-1]==f[1]就找到了循环节M,用幂取模a^b%M求得范围内的斐波那契数,因为a非常大,所以要先mod M,余数最多是n种,最多n^2就会重复出现。

The i’th Fibonacci numberf(i) isrecursively defined in the followingway:

f(0)=0andf(1)=1
f(i+2) =f(i+1)+f(i) for

every i0
Your task is to compute some

values of this sequence.

Input

Input begins with an integert10,000, the number of test cases.Each test case consists of three in-tegersa,b,nwhere 0a,b <264(aandbwill not both be zero) and1n1000. 

Output

Oooh...pretty

For each test case, output a single line containing the remainder off(ab) upon division byn.Sample Input

3
112
2 3 1000
18446744073709551615 18446744073709551615 1000

Sample Output

1

21

250 

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<set>#include<cstdlib>#include<cmath>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define rep3(i,a) for(int i=0;i<a;i++)#define rep4(i,a) for(int i=1;i<=a;i++)#define rep1(i,a,b) for(int i=a;i<=b;i++)#define rep2(i,a,b) for(int i=a;i>=b;i--)#define mem(x) memset(x,0,sizeof(x))#define sfd(a) scanf("%d",&a)#define sfld(a) scanf("%lld",&a)#define twosf(a,b) scanf("%d%d",&a,&b)#define sfs(a) scanf("%s",s)#define oi(a) printf("%d\n",a)#define ol(a) printf("%lld\n",a)#define maxn 1005typedef unsigned long long ull;ll f[maxn*maxn];int pow_mod(ull a,ull n,int m){    if(n==0)        return 1;    int x=pow_mod(a,n/2,m);    long long ans=(long long)x*x%m;    if(n%2==1)        ans=ans*a%m;    return (int)ans;}int main(){    int t;    ull a,b;    int n;    cin>>t;    while(t--)    {        int M;        cin>>a>>b>>n;        if(a==0||n==1)        {            cout<<"0"<<endl;            continue;        }        f[1]=f[2]=1;        for(int i=3;i<=maxn*maxn;i++)        {            f[i]=f[i-1]+f[i-2];            f[i]%=n;            if(f[i]==f[2]&&f[i-1]==f[1])            {                M=i-2;                break;            }        }        int ans=pow_mod(a%M,b,M);        cout<<f[ans]<<endl;    }}


原创粉丝点击