2018年东北农业大学春季校赛 K、wyh的数列

题目链接:https://www.nowcoder.com/acm/contest/93/K

题意:  斐波拉契数列f[0]=0、f[1]=1 , f[n]=f[n-1]+f[n-2](n>=2);求f[a^b]%c.(a、b<=2^64、2<=c<=1000)
***因为a、b可等于2^64,则类型应该为 unsigned long long ;

***c值非常小,我们可以暴力出f[x]%c的周期T,然后求出a^b%T,最终结果为f[a^b%T]%c;


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#define llt unsigned long long
using namespace std;

const int Size=1e5+7;
int f[Size];//储存斐波拉契树列模c结果

llt quick_Power(llt x,llt a,llt mod){
    if(a==0) return 1;
    if(a==1) return x;

    llt temp=quick_Power(x,a/2,mod);
    if(a%2==0)
        return temp*temp%mod;
    return  temp*temp%mod*x%mod;
}
int main(){

    int T;
    scanf("%d",&T);
    while(T--){
        llt a,b;
        int c;
        scanf("%llu%llu%d",&a,&b,&c);


        f[0]=0;f[1]=1;
        int i;
        for(i=2; ;++i){
            f[i]=(f[i-1]+f[i-2])%c;
           // cout<<f[i]<<endl;
            if(f[i]==0&&f[i-1]==1)
                break;
        }

        printf("%d\n",f[quick_Power(a%i,b,i)]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38786088/article/details/79830259