UVA - 11582 Colossal Fibonacci Numbers!

题目:https://odzkskevi.qnssl.com/1455f93c0d30098a930917660fd27c44?v=1533101971

思路:

见紫书……然后要用到快速幂嗯。

我T的原因居然是因为当时写了个memset。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int fib[1007*1007];
typedef unsigned long long LL;
int mod_pow(LL x,LL n,int mod)
{
    int res=1;
    x=x%mod;
    while(n>0){
        if(n&1)res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}
int main()
{
    int T;
    cin>>T;
    while(T--){
        LL a,b;
        int n;
        cin>>a>>b>>n;
        if(n==1||!a)printf("0\n");
        else{
        int M;
        fib[1]=1,fib[2]=1;
        for(int i=3;i<=n*n+10;i++){
            fib[i]=(fib[i-1]+fib[i-2])%n;
            if(fib[i]==fib[2]&&fib[i-1]==fib[1]){M=i-2;break;}
        }
        LL m=mod_pow(a%M,b,M);
        cout<<fib[m]<<endl;}
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Zero_979/article/details/81431543