Plant CodeForces
来源:互联网 发布:如何快速升淘宝等级 编辑:程序博客网 时间:2024/05/29 19:34
点击打开链接
我们设 第n个三角形中 上三角形数量为an 下三角形数量为bn
不难找出这样的递推关系 an = 3*an-1 + bn-1 , bn = an-1 + 3*bn-1
而找规律的题一般可用矩阵快速幂解决 试着凑出这样一个式子
| 3 1 | * | an-1 0 | = | 3*an-1 + bn-1 0 | ==> | an 0 | = ( | 3 1 | ^ n ) * | 1 0 |
| 1 3 | | bn-1 0 | | an-1 + 3*bn-1 0 | | bn 0 | | 1 3 | | 0 0 |
然后就是套模板了
#include <bits/stdc++.h>using namespace std;#define ll long long#define M 1000000007ll pre[2][2],ans[2][2],mat[2][2];ll n;void quickpow();void mul(ll a[][2],ll b[][2],ll c[][2]);int main(){ pre[0][0]=1,pre[0][1]=0,pre[1][0]=0,pre[1][1]=0; while(scanf("%lld",&n)!=EOF) { quickpow(); mul(ans,pre,pre); printf("%lld\n",ans[0][0]); } return 0;}void quickpow(){ ans[0][0]=1,ans[0][1]=0,ans[1][0]=0,ans[1][1]=1; mat[0][0]=3,mat[0][1]=1,mat[1][0]=1,mat[1][1]=3; while(n>0) { if(n%2==1) { mul(ans,mat,ans); } mul(mat,mat,mat); n/=2; } return;}void mul(ll a[][2],ll b[][2],ll c[][2]){ ll t[2][2]; memset(t,0,sizeof(t)); int i,j,k; for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(k=0;k<2;k++) { t[i][j]+=((a[i][k]%M)*(b[k][j]%M))%M; t[i][j]%=M; } } } memcpy(c,t,sizeof(t)); return;}
阅读全文
1 0
- Plant CodeForces
- Codeforces 185A Plant
- codeforces 185A Plant
- CodeForces 185A - Plant
- CodeForces - 185A Plant
- CodeForces 185A Plant
- Codeforces 185 A Plant
- codeforces 186C Plant
- codeforces Plant Tree
- codeforces plant 矩阵快速幂
- Plant
- Plant
- Plant
- CodeForces Round #118 (185A) - Plant
- 【快速幂】 CodeForces 185A Plant
- CodeForces - 185A Plant 矩阵快速幂
- Codeforces 185A Plant 普通快速幂取模
- codeforces 185A. Plant(规律)
- 一对多双向关联 、 多对一
- ios-collectionView?.deleteItems(at: [indexPath])造成的错误
- (M)Dynamic Programming: 474. Ones and Zeroes
- SylixOS 系统TICK实现
- js与jquery知识点总结(1)
- Plant CodeForces
- SSL2759 2017年10月6日提高组T2 挖矿(dp)
- 多线程之锁机制
- javaWeb-SAX解析和DOM解析并实现CRUD操作
- 一个HelloWorld程序的生成过程
- Android自定义控件(二)
- 杂志社投稿
- 初学python一些会用到的零件2【参考小甲鱼的书学习】
- 正则中需要转义的特殊字符小结