codeforces 185A Plant
来源:互联网 发布:mac os10.6.8光盘镜像 编辑:程序博客网 时间:2024/06/05 10:15
点击打开cf 185A
思路: 递推+矩阵快速幂
分析:
1 题目要求找到在n年后向上三角形的个数
2 写出前面的几个数F(0) = 1 , F(1) = 3 , F(2) = 10 , F(3) = 36 , F(4) = 136
通过前面几项我们可以找到通项公式F[n] = 4*F[n-1]-2^(n-1)
那么我们通过够找矩阵
| 4 -1 | * | F(n-1) | = | F(n) |
| 0 2 | | 2^(n-1) | | 2^n |
3 那么够造出矩阵之后我们直接利用矩阵快速幂,由于矩阵开始有负数,所以应该在取模的时候注意一下
代码:
/************************************************ * By: chenguolin * * Date: 2013-08-23 * * Address: http://blog.csdn.net/chenguolinblog * ***********************************************/#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef __int64 int64;const int MOD = 1e9+7;const int N = 2;int64 n;struct Matrix{ int64 mat[N][N]; Matrix operator*(const Matrix& m)const{ Matrix tmp; for(int i = 0 ; i < N ; i++){ for(int j = 0 ; j < N ; j++){ tmp.mat[i][j] = 0; for(int k = 0 ; k < N ; k++){ tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%MOD; tmp.mat[i][j] %= MOD; } } } return tmp; }};int64 Pow(Matrix &m){ if(n == 0) return 1; Matrix ans; ans.mat[0][0] = 1 , ans.mat[0][1] = 0; ans.mat[1][0] = 0 , ans.mat[1][1] = 1; while(n){ if(n%2) ans = ans*m; n /= 2; m = m*m; } int64 sum = 0; sum = (ans.mat[0][0]+ans.mat[0][1])%MOD; sum = (sum+MOD)%MOD; return sum;}int main(){ Matrix m; while(cin>>n){ m.mat[0][0] = 4 , m.mat[0][1] = -1; m.mat[1][0] = 0 , m.mat[1][1] = 2; cout<<Pow(m)<<endl; } return 0;}
- Codeforces 185A Plant
- codeforces 185A Plant
- CodeForces 185A - Plant
- CodeForces - 185A Plant
- CodeForces 185A Plant
- Codeforces 185 A Plant
- CodeForces Round #118 (185A) - Plant
- 【快速幂】 CodeForces 185A Plant
- CodeForces - 185A Plant 矩阵快速幂
- Codeforces 185A Plant 普通快速幂取模
- codeforces 185A. Plant(规律)
- codeforces 185A Plant(矩阵快速幂)
- codeforces 185A. Plant 矩阵快速幂
- CodeForces 185A. Plant (矩阵快速幂)
- codeforces 185A Plant(矩阵快速幂)
- codeforces #185 A Plant(矩阵快速幂+递推)
- CodeForces 185A. Plant(矩阵快速幂)
- CodeForces 185A. Plant(矩阵快速幂) 构造
- linux共享内存
- 使用 VisualVM 进行性能分析及调优
- 新浪微博 ios/android 你所访问的站点在新浪微博的认证失败 解决方法
- ORA-16019: cannot use LOG_ARCHIVE_DEST_1 with LOG_ARCHIVE_DEST or LOG_ARCHIVE_DUPLEX_DEST。
- hdu 畅通工程
- codeforces 185A Plant
- Hdu 2795 Billboard
- Spring Security 2 配置精讲
- 使用异步 I/O 大大提高应用程序的性能
- Android入门之SeekBar
- 网盘-同步-多终端漫天飞
- 开源协议:GPL/LGPL/BSD/Apache
- hdu 还是畅通工程
- android Theme使用总结 http://henzil.easymorse.com/?p=364