C - A Short problem(找循环节)

   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
#include <iostream>
#define mod1 1000000007
#define mod2 222222224
#define mod3 183120
#include <stdio.h>
typedef long long ll;
using namespace std;

struct two
{
    ll a[3][3];
};
two n= {0};
two m= {0};
two updata(two a,two b,ll mod)
{
    two c= {0};
    int i,j,k;
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            for(k=0; k<2; k++)
            {
                c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j]))%mod;
            }
        }
    }
    return c;
}

ll mi(ll x,ll mod)
{
    n.a[0][0]=3;
    n.a[0][1]=1;
    n.a[1][0]=1;
    n.a[1][1]=0;
    m=n;
    x=x-2;
    while(x)
    {
        if(x&1)
        {
            n=updata(n,m,mod);
        }
        m=updata(m,m,mod);
        x=x/2;
    }
    return n.a[0][0];
}

int main()
{
    ll x,ans;
    while(scanf("%lld",&x)!=EOF)
    {
        if(x==0)
        {
            cout << 0 << endl;
            continue;
        }
        else if(x==1)
        {
            cout << 1 << endl;
            continue;
        }
        else
        {
            ans=mi(x,mod3);
            if(ans>=2)
            {
                ans=mi(ans,mod2);
            }
            if(ans>=2)
            {
                ans=mi(ans,mod1);
            }
            cout << ans << endl;
        }
    }
    return 0;
}

#include <iostream>
#define mod1 1000000007
#define mod2 222222224
#define mod3 183120
#include <stdio.h>
typedef long long ll;
using namespace std;

struct two
{
    ll a[3][3];
};
two n;
two m;


ll mi(ll x,ll mod)
{
    int i,j,k;
    m.a[0][0]=n.a[0][0]=3;
    m.a[0][1]=n.a[0][1]=1;
    m.a[1][0]=n.a[1][0]=1;
    m.a[1][1]=n.a[1][1]=0;
    x=x-2;
    while(x)
    {
        if(x&1)
        {
            two c= {0};
            for(i=0; i<2; i++)
            {
                for(j=0; j<2; j++)
                {
                    for(k=0; k<2; k++)
                    {
                        c.a[i][j]=(c.a[i][j]+(n.a[i][k]*m.a[k][j]))%mod;
                    }
                }
            }
            for(i=0; i<2; i++)
            {
                for(j=0; j<2; j++)
                {
                    m.a[i][j]=c.a[i][j];
                }
            }
        }
        two c= {0};
        for(i=0; i<2; i++)
        {
            for(j=0; j<2; j++)
            {
                for(k=0; k<2; k++)
                {
                    c.a[i][j]=(c.a[i][j]+(n.a[i][k]*n.a[k][j]))%mod;
                }
            }
        }
        for(i=0; i<2; i++)
        {
            for(j=0; j<2; j++)
            {
                n.a[i][j]=c.a[i][j];
            }
        }
        x=x/2;
    }
    return m.a[0][0];
}

int main()
{
    ll x,ans;
    while(scanf("%lld",&x)!=EOF)
    {
        if(x==0)
        {
            cout << 0 << endl;
            continue;
        }
        else if(x==1)
        {
            cout << 1 << endl;
            continue;
        }
        else
        {
            ans=mi(x,mod3);
            if(ans>=2)
            {
                ans=mi(ans,mod2);
            }
            if(ans>=2)
            {
                ans=mi(ans,mod1);
            }
            cout << ans << endl;
        }
    }
    return 0;
}
#include <iostream>
#define mod1 1000000007
#define mod2 222222224
#define mod3 183120
#include <stdio.h>
typedef long long ll;
using namespace std;

struct two
{
    ll a[15][15];
};

two updata(two a,two b,ll mod)
{
    two c= {0};
    int i,j,k;
    for(i=0; i<5; i++)
    {
        for(j=0; j<5; j++)
        {
            for(k=0; k<5; k++)
            {
                c.a[i][j]=(c.a[i][j]+(a.a[i][k]*b.a[k][j])%mod)%mod;
            }
        }
    }
    return c;
}

two mi(two a,ll x,ll mod)
{
    int i;
    two b= {0};
    for(i=0; i<5; i++)
    {
        b.a[i][i]=1;
    }
    while(x)
    {
        if(x&1)
        {
            b=updata(a,b,mod);
        }
        a=updata(a,a,mod);
        x=x/2;
    }
    return b;
}

int main()
{
    ll x,ans;
    two b,c;
    two n= {0},m= {0};
    n.a[0][0]=3;
    n.a[0][1]=1;
    n.a[1][0]=1;
    m.a[0][0]=1;
    m.a[1][1]=1;
    while(scanf("%lld",&x)!=EOF)
    {
        if(x==0)
        {
            cout << 0 << endl;
            continue;
        }
        else if(x==1)
        {
            cout << 1 << endl;
            continue;
        }
        else
        {
            b=n,c=m;
            b=mi(b,x-1,mod3);
            c=updata(b,c,mod3);
            ans=c.a[0][0];
            ans=ans+mod3;
            b=n,c=m;
            b=mi(b,ans-1,mod2);
            c=updata(b,c,mod2);
            ans=c.a[0][0];
            ans=ans+mod2;
            b=n,c=m;
            b=mi(b,ans-1,mod1);
            c=updata(b,c,mod1);
            ans=c.a[0][0];
            cout << c.a[0][0]%mod1 << endl;
        }
    }
    return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long mo1=1000000007;
long long mo2=222222224;
long long mo3=183120;
long long s1[3][3],s2[3][3],s3[3][3];
long long f(long long n,long long mo)
{
    int i,j,k;
    s1[0][0]=s2[0][0]=3;
    s1[0][1]=s2[0][1]=1;
    s1[1][0]=s2[1][0]=1;
    s1[1][1]=s2[1][1]=0;
    n-=2;
    while (n)
    {
        if (n&1)
        {
            memset(s3,0,sizeof(s3));
            for (k=0;k<2;k++)
            for (i=0;i<2;i++){if (!s1[i][k]) continue;
            for (j=0;j<2;j++)
            s3[i][j]=(s3[i][j]+s1[i][k]*s2[k][j])%mo;}
            for (i=0;i<2;i++)
            for (j=0;j<2;j++) s2[i][j]=s3[i][j];
        }
        memset(s3,0,sizeof(s3));
        for (k=0;k<2;k++)
        for (i=0;i<2;i++) {if (!s1[i][k]) continue;
        for (j=0;j<2;j++)
        s3[i][j]=(s3[i][j]+s1[i][k]*s1[k][j])%mo;}
        for (i=0;i<2;i++)
        for (j=0;j<2;j++) s1[i][j]=s3[i][j];
        n>>=1;
    }
    return s2[0][0];
}
int main()
{
    long long n;
    long long ans;
    while (~scanf("%lld",&n))
    {
        if (n==0) {printf("0\n");continue;}
        if (n==1) {printf("1\n");continue;}
        ans=f(n,mo3);
        if (ans>=2) //一定要有,不然ans<2时函数就会死循环。
        ans=f(ans,mo2);
        if (ans>=2)
        ans=f(ans,mo1);
        printf("%lld\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值