1. 程式人生 > >2018年東北農業大學春季校賽 K、wyh的數列

2018年東北農業大學春季校賽 K、wyh的數列

題意:  斐波拉契數列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;
}