【求循环节+矩阵快速幂】HDOJ A Short problem 4291

A Short problem

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


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
 


题意:

依据g(n)的计算公式,计算g(g(g(n)))%10^9+7;

解题思路:

主要的方法就是求出循环节。然后处理的时候就是用矩阵快速幂。注意快速幂的指数的大小

AC代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;

typedef long long LL;
const int MAXN = 2;
int Mod1=1000000007;
int Mod2=222222224;
int Mod3=183120;

LL g[MAXN][MAXN];

void multiply(LL x[MAXN][MAXN],LL y[MAXN][MAXN],LL Mod)
{
    LL t[MAXN][MAXN]={0};
    for(int i=0;i<MAXN;i++){
        for(int k=0;k<MAXN;k++){
            if(x[i][k]){
                for(int j=0;j<MAXN;j++){
                    t[i][j]=(t[i][j]+x[i][k]*y[k][j])%Mod;
                }
            }
        }
    }
    for(int i=0;i<MAXN;i++){
        for(int j=0;j<MAXN;j++){
            x[i][j]=t[i][j];
        }
    }
}

void Matrix(LL x[MAXN][MAXN],LL n,LL Mod)
{
    LL res[MAXN][MAXN];
    for(int i=0;i<MAXN;i++){
        for(int j=0;j<MAXN;j++){
            if(i==j) res[i][j]=1;
            else res[i][j]=0;
        }
    }
    while(n){
        if(n&1) multiply(res,x,Mod);
        multiply(x,x,Mod);
        n>>=1;
    }
    for(int i=0;i<MAXN;i++){
        for(int j=0;j<MAXN;j++){
            x[i][j]=res[i][j];
        }
    }
}

//暴力求 Mod n的循环节a=3,b=1,c=1,d=3,n;
void circular_section(LL a,LL b,LL c,LL d,LL n)
{
    LL x=c,y=d,t;
    LL i=1;
    while(i){
        if(i>20*n){
            printf("-1\n");
            return;
        }
        t=a*d+b*c;
        t%=n;
        c=d;
        d=t;
        if(c==x&&d==y){
            printf("%lld\n",i);
            return;
        }
        i++;
    }
}


void solve(LL n)
{
    LL f[MAXN],t;
    f[0]=1;f[1]=0;
    g[0][0]=3;g[0][1]=1;
    g[1][0]=1;g[1][1]=0;
    Matrix(g,n,Mod3);
    LL temp=0;
    for(int i=0;i<MAXN;i++){
        temp=(temp+f[i]*g[i][1])%Mod3;
    }
    g[0][0]=3;g[0][1]=1;
    g[1][0]=1;g[1][1]=0;
    Matrix(g,temp,Mod2);
    temp=0;
    for(int i=0;i<MAXN;i++){
        temp=(temp+f[i]*g[i][1])%Mod2;
    }
    g[0][0]=3;g[0][1]=1;
    g[1][0]=1;g[1][1]=0;
    Matrix(g,temp,Mod1);
    temp=0;
    for(int i=0;i<MAXN;i++){
        temp=(temp+f[i]*g[i][1])%Mod1;
    }
    printf("%lld\n",temp);
}

int main()
{
    LL n;
    while(scanf("%lld",&n)!=EOF){
        if(n==0)printf("0\n");
        else if(n==1) printf("1\n");
        else solve(n);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值