CF 185A(递推式构造矩阵求解)
来源:互联网 发布:淘宝主图背景素材psd 编辑:程序博客网 时间:2024/05/16 14:14
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.
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.
Print a single integer — the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).
1
3
2
10
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
思路:
找规律得递推式为:F(n) = 4*F(n-1) - 2^(n-1)
有递推式构造快速幂矩阵 : 4 -1
0 2
代码:
#include <cstdio>#include <cstdlib>#include <algorithm>#include <cmath>#include <cstring>using namespace std;typedef long long int ll;const ll mod = 1e9+7;struct matrix{ll a[2][2];matrix operator*(const matrix& tmp){matrix ans;for(int i=0; i<2; i++)for(int j=0; j<2; j++){ans.a[i][j] = 0;for(int k=0; k<2; k++)ans.a[i][j] = (ans.a[i][j] + ((a[i][k]*tmp.a[k][j])%mod) )%mod;}return ans;}};matrix quick_pow(matrix a,ll n){matrix ans;ans.a[0][0] = ans.a[1][1] = 1;ans.a[0][1] = ans.a[1][0] = 0;while(n){if(n%2)ans = ans*a;a = a*a;n /= 2;}return ans;}int main(){ll N;scanf("%I64d",&N);if(N==0){printf("1\n");return 0;}matrix m;m.a[0][0] = 4;m.a[0][1] = -1;m.a[1][0] = 0;m.a[1][1] = 2;m = quick_pow(m,N-1);ll ans = (m.a[0][0]*3 + m.a[0][1]*2)%mod + mod;printf("%I64d\n",ans%mod);return 0;}
- CF 185A(递推式构造矩阵求解)
- cf 185A(矩阵快速幂)
- CF - 484 - A. Bits(构造)
- CodeForces 185A. Plant(矩阵快速幂) 构造
- cf 468A 24 Game 构造
- CF 518A(Vitaly and Strings-构造)
- cf 185div2 A
- HDU - 1757 A Simple Math Problem (构造矩阵)
- HDU 1757 A Simple Math Problem(构造矩阵)
- cf(420A)
- CF 715A Plus and a square root(数学构造)
- HDU 2855(由递推式构造矩阵+矩阵快速幂)
- 线性方程组(A是上三角矩阵时)的C++求解
- 根据递推式构造矩阵
- 【构造策略】-CF-301A-Yaroslav and sequence
- CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))
- A Simple Math Problem (矩阵快速幂、构造矩阵)
- CF 171(div2)A
- 承接上--URL
- 02-线性结构1 两个有序链表序列的合并 (15分)
- set的hashset和treeSet
- Linux + QQ feiqiu iptux
- 51nod 1640 天气晴朗的魔法(kruskal)
- CF 185A(递推式构造矩阵求解)
- webdriver-使用自定义的firefox配置
- win7 32+vs2012+opencv3.0.0配置
- 23种设计模式全解析
- Android入门教程 fragment通信
- 四、数组和指针
- 2016.10.05【初中部 NOIP普及组 】模拟赛总结
- 【JZOJ4811】排队
- [LeetCode]--121. Best Time to Buy and Sell Stock