codeforces - 185 - A - Plant 【经典矩阵快速幂】
2017-10-27 19:37
483 查看
A. Plant
time limit per test2 secondsmemory limit per test256 megabytes
inputstandard input
outputstandard output
Dwarfs have planted a very interesting plant, which is a triangle directed “upwards”. This plant has an amusing feature. After one year a triangle plant directed “upwards” divides into four triangle plants: three of them will point “upwards” and one will point “downwards”. After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.
Help the dwarfs find out how many triangle plants that point “upwards” will be in n years.
Input
The first line contains a single integer n (0 ≤ n ≤ 1018) — the number of full years when the plant grew.Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer — the remainder of dividing the number of plants that will point “upwards” in n years by 1000000007 (109 + 7).Examples
input
1output
3
input
2
output
10
Note
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
题意 : 根据图片的规律,每年会分裂一次,让你求出第n年后的图形中有几个”朝上”的三角形。范围: 0 ≤ n ≤ 1018
分析: 一开始做这个题看了第二个图和第三个图后直接断定递推式就是F[n]=3∗F[n−1]+1,然后就wa了,后来当推了后面的时候发现不对,少算了不少,然后就是慢慢找递推式,还是找不到,后来偶然发现无论怎么在第n次后,朝上的三角形+朝下的三角形 = 4n,然后又推了下发现,每一次朝上的”分裂”一次后会产生三个朝上的和一个朝下的,而一个朝下的“分裂”后会产生三个朝下的和一个朝上的,所以就可以得到递推式了,从而根据矩阵快速幂得到详细看代码
参考代码
#include<bits/stdc++.h> #define ll long long #define mod(x) ((x)%MOD) using namespace std; const ll MOD = 1e9 + 7; struct mat{ ll m[3][3]; }a,ans,unit; void init() { memset(unit.m,0,sizeof(unit.m)); memset(a.m,0,sizeof(a.m)); unit.m[0][0] = 1; unit.m[1][1] = 1; a.m[0][0] = 3; a.m[0][1] = 1; a.m[1][0] = 1; a.m[1][1] = 3; } mat operator * (mat m1,mat m2) { mat t; ll r; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { r = 0; for(int k = 0;k < 3;k++) { r = mod(r*1ll + mod(mod(m1.m[i][k])*1ll*mod(m2.m[k][j]))); } t.m[i][j] = r; } } return t; } mat quick_pow(ll x) { mat t = unit; while(x) { if(x & 1) { t = t*a; } a = a*a; x >>= 1; } return t; } int main(){ ios_base::sync_with_stdio(0); ll n;while(cin>>n){ init(); if(n == 0) { cout<<1<<endl; return 0; } if(n == 1) { cout<<3<<endl; return 0; } ans = quick_pow(n); cout<<mod(ans.m[0][0]*1ll)<<endl;} return 0; }
如有错误或遗漏,请私聊下UP,thx
相关文章推荐
- CodeForces - 185A Plant 矩阵快速幂
- codeforces 185A Plant(矩阵快速幂)
- codeforces 185A Plant(矩阵快速幂)
- Codeforces 450B-Jzzhu and Sequences (矩阵快速幂)
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
- [矩阵快速幂加速DP] Codeforces 717D Bubble Cup 9 - Finals D. Dexterina’s Lab
- CodeForces 185A. Plant(矩阵快速幂) 构造
- 垒骰子(经典递推、矩阵快速幂)
- CodeForces 719E Sasha and Array 【线段树】【快速矩阵幂】
- Codeforces 719E [斐波那契区间操作][矩阵快速幂][线段树区间更新]
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】
- CodeForces 185A. Plant (矩阵快速幂)
- codeforces - 450 - B - Jzzhu and Sequences 【简单的矩阵快速幂】
- HDU 2157 How many ways??(经典矩阵快速幂)
- CodeForces-185A(矩阵快速幂)
- CodeForces 60 E.Mushroom Gnomes(矩阵快速幂)
- codeforces #185 A Plant(矩阵快速幂+递推)
- 【Codeforces 718C&719E】Sasha and Array【线段树成段更新+矩阵快速幂】
- Codeforces - 551D. GukiZ and Binary Operations - 数学+矩阵快速幂
- Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】