Recursive sequence(HDU5950 矩阵快速幂)
2018-10-11 09:58:55来源:博客园 阅读 ()
题目:
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 i4i4. 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.
Input:
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 i4i4. 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.
Output:
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.
Hint:
In the first case, the third number is 85 = 2*1+2+3^4.
In the second case, the third number is 93 = 2*1+1*10+3^4 and the fourth number is 369 = 2 * 10 + 93 + 4^4.
题意:
奶牛报数,先给两个数a和b,分别是f[n-2],f[n-1],之后每头奶牛i报数为f[i-1] + 2 * f[i-2] + i^4;给出n,求din头奶牛要报的数字,对2147493647取余。
思路:
看到这个式子知道这是一个矩阵快速幂,然后开始推式子,在我给队友写出平方差公式来队友看到杨辉三角形式后后,就去推7*7的矩阵快速幂了,但因为刚刚学这个,但结束就挂死在这个题上了。
具体式子如下:
之后就是套裸的矩阵快速幂就好了,个人感觉做题补题真的是长知识最快的方法啊。补题的时候自己直接用矩阵来写麻烦的要死,就把矩阵放在一个结构体中,顺便方便很多。
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> using namespace std; typedef long long ll; const ll MOD = 2147493647; struct Maxt { ll mp[8][8]; Maxt() { for(int i = 1; i<=7; i++) { for(int j = 1; j<=7; j++) { mp[i][j] = 0; } } } } fp,tmp; int n,a,b,T; int read() { int res = 0; char op; op = getchar(); if(op>='0' && op<='9') { res = op-'0'; op = getchar(); } while(op>='0' && op<='9') { res = res*10 + op-'0'; op = getchar(); } return res; } void init() { for(int i = 1; i<=7; i++) { for(int j =1; j<=7; j++) { fp.mp[i][j] = 0; tmp.mp[i][j] = 0; } } fp.mp[1][1] = 1,fp.mp[2][1] = 1,fp.mp[2][2] = 1,fp.mp[7][6] = 1; fp.mp[3][1] = 1,fp.mp[3][2] = 2,fp.mp[3][3] = 1,fp.mp[4][1] = 1; fp.mp[4][2] = 3,fp.mp[4][3] = 3,fp.mp[4][4] = 1,fp.mp[5][1] = 1; fp.mp[5][2] = 4,fp.mp[5][3] = 6,fp.mp[5][4] = 4,fp.mp[5][5] = 1; fp.mp[6][5] = 1,fp.mp[6][6] = 1,fp.mp[6][7] = 2; tmp.mp[1][1] = 1,tmp.mp[2][1] = 3,tmp.mp[3][1] = 9,tmp.mp[4][1] = 27,tmp.mp[5][1] = 81,tmp.mp[6][1] = b,tmp.mp[7][1] = a; } Maxt Maxtcalc(const Maxt& a,const Maxt& b) { Maxt t; for(int i = 1; i<=7; i++) { for(int j = 1; j<=7; j++) { t.mp[i][j] = 0; for(int k = 1; k<=7; k++) { t.mp[i][j] = (t.mp[i][j] + (a.mp[i][k]*b.mp[k][j]) % MOD) % MOD; } } } return t; } Maxt qcalc(int x,Maxt s) { Maxt tmp; for(int i = 1; i<=7; i++) { tmp.mp[i][i] = 1; } while(x) { if(x&1) { tmp = Maxtcalc(tmp, s); } s = Maxtcalc(s, s); x>>=1; } return tmp; } int main() { T = read(); while(T--) { n = read(); a = read(); b= read(); if(n == 1) { printf("%d\n",a); continue; } if(n == 2) { printf("%d\n",b); continue; } if(n == 3) { printf("%d\n",81+2*a+b); continue; } n = n-2; init(); fp = qcalc(n,fp); Maxt ans = Maxtcalc(fp, tmp); printf("%lld\n",ans.mp[6][1]%MOD); } return 0; } /* 样例输入: 2 3 1 2 4 1 10 样例输出: 85 369 */
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Longest Ordered Subsequence 2020-02-09
- common subsequence 2020-02-09
- 最长上升子序列(LIS: Longest Increasing Subsequence) 2019-09-08
- Ural 1248 Sequence Sum 题解 2019-08-16
- BZOJ4355: Play with sequence(吉司机线段树) 2018-09-19
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash