hdu 4291 矩阵快速幂+循环节

很简单的题目,将嵌套循环先找到三个循环节,然后快速幂。


ACcode:

#include<cstdio>
#include<cstring>

typedef long long LL;
const int MOD1=183120;
const int MOD2=222222224;
const int MOD3=1e9+7;

LL n;

LL fun(LL mod)
{
    LL a=0,b=1,c=-1;
    LL cnt=1;
    while (1)
    {
        c=(3*b+a)%mod;
        if (c==1&&b==0) break;
        cnt++;
        a=b,b=c;
    }
    return cnt;
}

int qp(LL y,int mod)
{
    LL t[2][2];
    LL a[2][2]={3,1,1,0};
    LL b[2][2]={1,0,1,0};
    while (y)
    {
        if (y&1)
        {
            for (int i=0;i<2;i++)
            for (int j=0;j<2;j++)
            {
                t[i][j]=0;
                for (int k=0;k<2;k++)
                t[i][j]=(t[i][j]+b[i][k]*a[k][j])%mod;
            }
            for (int i=0;i<2;i++)
            for (int j=0;j<2;j++)
            b[i][j]=t[i][j];
        }
        y>>=1;
        for (int i=0;i<2;i++)
        for (int j=0;j<2;j++)
        {
            t[i][j]=0;
            for (int k=0;k<2;k++)
            t[i][j]=(t[i][j]+a[i][k]*a[k][j])%mod;
        }
        for (int i=0;i<2;i++)
        for (int j=0;j<2;j++)
        a[i][j]=t[i][j];
    }
    return b[1][1];
}

int main()
{
    while (~scanf("%I64d",&n))
    {
        n=qp(n,MOD1);
        n=qp(n,MOD2);
        n=qp(n,MOD3);
        printf("%I64d\n",n);

    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值