HDU 5950(矩阵快速幂)
来源:互联网 发布:飞飞cms 无法加载模块 编辑:程序博客网 时间:2024/06/15 19:48
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, andi4 Input The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b <231 as described above.
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. Sample Input Sample Output Hint
大概就这样了。。。。。。
. 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.
Each case contains only one line with three numbers N, a and b where N,a,b <
23 1 24 1 10
85369
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[i]=2*a[i-2]+a[i-1]+i^4,给出n,a1,a2,求an
思路:a[i]=2*a[i-2]+a[i-1]+i^4=2*a[i-2]+a[i-1]+((i-1)+1)^4=2*a[i-2]+a[i-1]+(i-1)^4+4*(i-1)^3+6*(i-1)^2+4*(i-1)+1
构造7*7的矩阵然后快速幂
设矩阵:
1,1,0,0,0,0,0
2,0,0,0,0,0,0
1,0,1,0,0,0,0
4,0,4,1,0,0,0
6,0,6,3,1,0,0
4,0,4,3,2,1,0
1,0,1,1,1,1,1为 martix-1
设 矩阵( a[i-1],a[i-2],(i-1)^4,(i-1)^3,(i-1)^2,(i-1),1 ) 为martix-2.
则 martix-2 * martix-1 = ( a[i],a[i-1],i^4,i^3,i^2,i,1 )
所以ac代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cmath>using namespace std;typedef long long LL;const LL MOD=2147493647;struct martix{ LL a[7][7];}x;void output(martix a){ for(int i=0;i<7;i++) { for(int j=0;j<7;j++) { printf("%lld ",a.a[i][j]); } puts(""); } puts("");}martix mul(martix a,martix b){ martix c; for(int i=0;i<7;i++)for(int j=0;j<7;j++)c.a[i][j]=0; for(int i=0;i<7;i++) { for(int j=0;j<7;j++) { for(int k=0;k<7;k++) { c.a[i][j]+=a.a[i][k]*b.a[k][j]; c.a[i][j]%=MOD; } } }// puts("C************");// output(a);output(b);// output(c); return c;}martix quick(LL n){ martix e;// printf("n=%lld********************\n",n); for(int i=0;i<7;i++)for(int j=0;j<7;j++)if(i==j)e.a[i][j]=1;else e.a[i][j]=0; while(n) { if(n&1)e=mul(e,x); x=mul(x,x); n>>=1;// printf("n=%lld********************\n",n); } return e;}void init(){ x.a[0][0]=1,x.a[0][1]=1,x.a[0][2]=0,x.a[0][3]=0,x.a[0][4]=0,x.a[0][5]=0,x.a[0][6]=0; x.a[1][0]=2,x.a[1][1]=0,x.a[1][2]=0,x.a[1][3]=0,x.a[1][4]=0,x.a[1][5]=0,x.a[1][6]=0; x.a[2][0]=1,x.a[2][1]=0,x.a[2][2]=1,x.a[2][3]=0,x.a[2][4]=0,x.a[2][5]=0,x.a[2][6]=0; x.a[3][0]=4,x.a[3][1]=0,x.a[3][2]=4,x.a[3][3]=1,x.a[3][4]=0,x.a[3][5]=0,x.a[3][6]=0; x.a[4][0]=6,x.a[4][1]=0,x.a[4][2]=6,x.a[4][3]=3,x.a[4][4]=1,x.a[4][5]=0,x.a[4][6]=0; x.a[5][0]=4,x.a[5][1]=0,x.a[5][2]=4,x.a[5][3]=3,x.a[5][4]=2,x.a[5][5]=1,x.a[5][6]=0; x.a[6][0]=1,x.a[6][1]=0,x.a[6][2]=1,x.a[6][3]=1,x.a[6][4]=1,x.a[6][5]=1,x.a[6][6]=1;}LL n,a1,a2;int main(){ int T; scanf("%d",&T); while(T--) { scanf("%lld%lld%lld",&n,&a1,&a2); if(n==1)printf("%lld\n",a1); else if(n==2)printf("%lld\n",a2); else { init();// puts("X*************");// output(x); x=quick(n-2);// puts("Y*************");// output(x); LL b[7]={a2,a1,16,8,4,2,1}; LL ans=0; for(int i=0;i<7;i++) { ans=(ans+b[i]*x.a[i][0])%MOD; } printf("%lld\n",ans); } } return 0;}
大概就这样了。。。。。。
本人蒟蒻,如有错误,还望指正
阅读全文
0 0
- HDU 5950(矩阵快速幂)
- HDU 5950 Recursive sequence(矩阵构造+矩阵快速幂)
- HDU 5950 矩阵快速幂
- hdu 5950(化解公式+矩阵快速幂)
- HDU 5950 Recursive sequence(矩阵快速幂)
- hdu 5950 Recursive sequence(矩阵快速幂,构造)
- HDU 5950 Recursive sequence(矩阵快速幂)
- HDU 5950 Recursive sequence(矩阵快速幂)
- HDU 5950 Recursive sequence(矩阵快速幂)
- hdu 5950 Recursive sequence (矩阵快速幂)
- [hdu 5950 Recursive sequence] 矩阵快速幂
- HDU-5950-Recursive sequence-矩阵快速幂
- HDU 5950 Recursive sequence 矩阵快速幂
- [矩阵快速幂]HDU 5950 Recursive sequence
- hdu 5950 数学公式 + 矩阵快速幂
- hdu 2254 奥运(矩阵快速幂)
- hdu 1575 (矩阵快速幂)
- HDU 2256(矩阵快速幂)
- win10安装python的xlrd
- mui:第一次在AppStore 上线应用
- zabbix监控系统客户端安装
- http
- C语言 简单的递归算法
- HDU 5950(矩阵快速幂)
- Springmvc和Mybatis(RequestMapping及参数绑定)
- php效率高写法
- HTML5实现粒子效果的文字
- Ubuntu 14.04 安装ROS Indigo
- 2017/11/8模拟赛总结
- Hibernate级联删除失败问题
- JS面向对象基础篇(封装 继承 多态)
- SQL中获取一段时间有多少个星期