矩阵快速幂取模

#include<iostream>
using namespace std;
const int Mod=10000;
int main()
{
    long long n,a,b,c,d,a1,b1,c1,d1,a2,b2,c2,d2;
    while(cin>>n)
    {
        if(n==-1)
        break;
        if(n==0||n==1)
        {
        cout<<n<<endl;
        continue; 
        } 
        a=1;b=0;c=0;d=1;
        a2=1;b2=1;c2=1;d2=0;
        while(n>0)
        {
            if(n%2!=0)
            {
                a1=(a*a2)%Mod+(b*c2)%Mod;
                b1=(a*b2)%Mod+(b*d2)%Mod;
                c1=(c*a2)%Mod+(d*c2)%Mod;
                d1=(c*b2)%Mod+(d*d2)%Mod;
                a=a1;b=b1;c=c1;d=d1;
            }
            n=n/2;
            a1=((a2*a2)%Mod+(b2*c2)%Mod)%Mod;
            b1=((a2*b2)%Mod+(b2*d2)%Mod)%Mod;
            c1=((a2*c2)%Mod+(c2*d2)%Mod)%Mod;
            d1=((b2*c2)%Mod+(d2*d2)%Mod)%Mod;
            a2=a1;b2=b1;c2=c1;d2=d1;
        }
        if(b%Mod==0)
        cout<<"0"<<endl;
        else
        cout<<b%Mod<<endl;
    }
}

 

posted @ 2019-05-08 22:23  Leozi  阅读(355)  评论(0编辑  收藏  举报