hdu5950
来源:互联网 发布:百卓优采软件 编辑:程序博客网 时间:2024/06/01 18:41
Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and
i
4
. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
题意:
:F(n) = 2*F(n-2) + F(n-1) + n4 和F(1) = a,F(2) = b;给定一个N,求F(N)
再来一道矩阵快速幂吧~~出现n4考虑((n-1)+1)4。。。。二项式定理。。。
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const long long MOD = 2147493647;struct Matrix{ long long m[7][7];};long long aa,bb,n;Matrix Mul(Matrix a ,Matrix b){ Matrix tmp; memset(tmp.m,0,sizeof(tmp.m)); for(int i = 0 ; i < 7 ; i++) for(int j = 0 ; j < 7 ;j++) for(int k = 0 ; k < 7 ; k++) tmp.m[i][j] = (tmp.m[i][j] + a.m[i][k] * b.m[k][j])%MOD; return tmp;}Matrix qpow(Matrix a,long long n){ Matrix tmp; memset(tmp.m,0,sizeof(tmp.m)); for(long long i = 0 ; i < 7 ; i++) tmp.m[i][i] = 1; while(n){ if(n & 1) tmp = Mul(tmp,a); n >>= 1; a = Mul(a,a); } return tmp;}void sov(){ Matrix tmp; memset(tmp.m,0,sizeof(tmp.m)); tmp.m[0][0] = tmp.m[0][2] = tmp.m[0][6] = tmp.m[1][0] = 1; for(int i = 2 ; i <= 6 ; i++) tmp.m[i][6] = tmp.m[i][i] = 1; tmp.m[0][3] = tmp.m[0][5] = tmp.m[2][3] = tmp.m[2][5] = 4; tmp.m[0][4] = tmp.m[2][4] = 6; tmp.m[3][5] = tmp.m[3][4] = 3; tmp.m[4][5] = tmp.m[0][1] = 2; tmp = qpow(tmp,n-2); long long ans; ans = (tmp.m[0][0] * bb)%MOD; ans = (ans+tmp.m[0][1] *aa)%MOD; ans = (ans+tmp.m[0][2] *16)%MOD; ans = (ans+tmp.m[0][3] * 8)%MOD; ans = (ans+tmp.m[0][4] * 4)%MOD; ans = (ans+tmp.m[0][5] * 2)%MOD; ans = (ans+tmp.m[0][6])%MOD; cout << ans<<endl;}int main(){ int T; scanf("%d",&T); while(T--){ cin>> n>> aa >> bb; if(n == 1) cout << aa<<endl; else if( n== 2) cout << bb<<endl; else{ sov(); } }}
0 0
- hdu5950
- HDU5950
- hdu5950 dfs
- HDU5950【矩阵快速幂】
- hdu5950 矩阵快速幂
- hdu5950 矩阵dp
- hdu5950 矩阵构造
- HDU5950(67/600)
- HDU5950 Recursive sequence 沈阳赛
- HDU5950(矩阵快速幂)
- HDU5950 (2016沈阳现场赛)
- HDU5950(矩阵快速幂)
- 矩阵构造+矩阵快速幂-HDU5950
- (矩阵快速幂)hdu5950 Recursive sequence
- HDU5950 Recursive sequence(矩阵快速幂)
- HDU5950 2016ACM/ICPC亚洲区沈阳站现场赛
- HDU5950 Recursive sequence —— 矩阵快速幂
- hdu5950(递推的矩阵快速幂)
- 表达式求值
- Httpclient发送http请求
- Angular开发者指南(一)入门介绍
- NSIS总脚本代码参考
- 《Head First JAVA》学习笔记(一)
- hdu5950
- JAVA设计模式之工厂模式
- 输出错误信息与调试信息
- iOS10 不自动弹出询问是否允许使用蜂窝数据的解决方法
- Android自定义控件系列(六)—优雅的实现广告轮播图
- Socket在Android开发中的应用
- 0305作业
- Input学习笔记
- ASP.NET自定义控件组件开发 第一章:从一个简单的控件谈起