uva11582 Colossal Fibonacci Numbers!

快速幂, 循环节,斐波那契数列

只是一个水水的题目,可以看出循环节是小于n^2的,所以先枚举出循环节。然后快速幂取模就可以了。

但要注意必须用unsigned long long,而且我用scanf读入还出现了意想不到的问题,所以只能用cin读入。

这是一个很大的坑点.

#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 10000000 + 10;

typedef unsigned long long LL;
LL a,b,n,m;

int f[maxn];

LL power(LL k,LL e) {
    LL res=1;
    while(e) {
        if(e&1) res=res*k%m;
        k=k*k%m;
        e=e/2;    
    }
    return res;
}

int main() {
    int T;
    cin>>T;
    while(T--) {
        cin>>a>>b>>n;
        if(n==1) {
            cout<<0<<endl;
            continue;    
        }
        f[0]=0; f[1]=1;
        for(int i=2;;i++) {
            f[i]=(f[i-1]+f[i-2])%n;
            if(f[i]==f[1]&&f[i-1]==f[0]) {
                m=i-1;
                break;
            }
        }
        a%=m;
        cout<<f[power(a,b)]<<endl;
    }
    return 0;    
}
posted @ 2016-06-10 10:31  invoid  阅读(119)  评论(0编辑  收藏  举报