hdu 4291 A Short problem

本文讨论了如何高效解决复杂递归序列求模问题,通过矩阵快速幂技巧优化计算过程,适用于需要频繁求解特定序列模运算的场景。

A Short problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 852    Accepted Submission(s): 335


Problem Description
  According to a research, VIM users tend to have shorter fingers, compared with Emacs users.
  Hence they prefer problems short, too. Here is a short one:
  Given n (1 <= n <= 10 18), You should solve for
g(g(g(n))) mod 10 9 + 7

  where
g(n) = 3g(n - 1) + g(n - 2)

g(1) = 1

g(0) = 0

 

Input
  There are several test cases. For each test case there is an integer n in a single line.
  Please process until EOF (End Of File).
 

Output
  For each test case, please print a single line with a integer, the corresponding answer to this case.
 

Sample Input
  
0 1 2
 

Sample Output
  
0 1 42837
 

Source
 


代码:
#include<cstdio>
#define ll __int64
int mod;
struct Matrix
{
    ll m[2][2];
    void init()
    {
        m[0][0]=3;m[0][1]=1;
        m[1][0]=1;m[1][1]=0;
    }
}a;
Matrix mul(Matrix a,Matrix b)
{
    Matrix c;
    int i,j,k;
    for(i=0;i<2;i++)for(j=0;j<2;j++)
    {
        c.m[i][j]=0;
        for(k=0;k<2;k++)
            c.m[i][j]=(a.m[i][k]*b.m[k][j]%mod+c.m[i][j])%mod;
    }
    return c;
}
Matrix mpow(Matrix a,ll n)
{
    Matrix c;
    int i,j;
    for(i=0;i<2;i++)for(j=0;j<2;j++)c.m[i][j]=(i==j);
    for(;n;n>>=1)
    {
        if(n&1)c=mul(c,a);
        a=mul(a,a);
    }
    return c;
}
ll solve(ll n,ll m)
{
    a.init();
    mod=m;
    a=mpow(a,n);
    return a.m[0][1];
}
int main()
{
    ll n;
    while(~scanf("%I64d",&n))
        printf("%I64d\n",solve(solve(solve(n,183120),222222224),1000000007));
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值