codeforces 185A. Plant(规律)

codeforces 185A. Plant


问经过N年后,向上的三角形的个数。

第一种解法:矩阵递推可以发现一年后,新的三角形图形含有的三角形的个数是上一三角形图形含有的三角形个数的4倍。不过中间的那一块上下方向转变了。由此我们可以得到递推式:设f[n][0]是第N年后的图形的上三角形个数,f[n][1]是第N年后图形的下三角形个数。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
struct matrix{
    LL m[2][2];
};
matrix I={
1,0,
0,1
};
matrix A={
3,1,
1,3
};
matrix multi(matrix a,matrix b){
    matrix c;
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            c.m[i][j]=0;
            for(int k=0;k<2;k++){
                c.m[i][j]=c.m[i][j]+a.m[i][k]*b.m[k][j]%mod;
            }
            c.m[i][j]%=mod;
        }
    }
    return c;
}
matrix power(LL p){
    matrix ans=I,temp=A;
    while(p){
        if(p&1) ans=multi(ans,temp);
        temp=multi(temp,temp);
        p>>=1;
    }
    return ans;
}
int main()
{
    LL n;
    while(cin>>n){
        matrix ans=power(n);
        printf("%I64d\n",ans.m[0][0]);
    }
    return 0;
}

第二种解法:斜着右上方查看每一列的上三角形的数量变化。嘿嘿,规律出现了。

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
LL power(LL p){
    LL ans=1,temp=2;
    while(p){
        if(p&1) ans=ans*temp%mod;
        temp=temp*temp%mod;
        p>>=1;
    }
    return ans;
}
int main()
{
    LL n;
    while(cin>>n){
         if(n==0){
            puts("1");
            continue;
         }
         printf("%I64d\n",(power(2*n-1)+power(n-1))%mod);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值