HDU5950 Recursive sequence(矩阵快速幂)
来源:互联网 发布:linux的家目录 编辑:程序博客网 时间:2024/06/05 08:33
Problem Description
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 i4. 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
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
23 1 24 1 10
Sample Output
85369
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.
思路
先说题意,有一个递推式:
求第n项的值
很明显的一道矩阵快速幂的题,难点就是如何构造初始矩阵。
因为后面加的是
由此我们可以构造出转置矩阵:
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
我们还需要一个原始矩阵,那么就是:
f2 f1 16 8 4 2 1
那么要求
代码
#include <cstdio>#include <cstring>#include <string>#include <set>#include <cmath>#include <iostream>#include <stack>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 2147493647#define debug() puts("what the fuck!!!")#define ll long longusing namespace std;const ll N=7;struct Matrix{ ll a[N][N]; Matrix() { mem(a,0); } void init() { mem(a,0); for(ll i=0; i<N; i++) a[i][i]=1; }};Matrix mul(Matrix a,Matrix b){ Matrix ans; for(ll i=0; i<N; i++) for(ll j=0; j<N; j++) for(ll k=0; k<N; k++) { ans.a[i][j]=(ans.a[i][j]%mod+a.a[i][k]*b.a[k][j]%mod)%mod; } return ans;}Matrix mat_pow(Matrix a,ll n){ Matrix ans; ans.init(); while(n) { if(n&1) ans=mul(ans,a); a=mul(a,a); n>>=1; } return ans;}int main(){ ll f1,f2,t,n; scanf("%lld",&t); while(t--) { scanf("%lld%lld%lld",&n,&f1,&f2); if(n==1) printf("%lld\n",f1); if(n==2) printf("%lld\n",f2); else { ll zhuanzhi[N][N]= { 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 }; Matrix A,B; for(ll i=0; i<N; i++) for(ll j=0; j<N; j++) B.a[i][j]=zhuanzhi[i][j]; A.a[0][0]=f2,A.a[0][1]=f1,A.a[0][2]=16; A.a[0][3]=8,A.a[0][4]=4,A.a[0][5]=2,A.a[0][6]=1; Matrix p=mat_pow(B,n-2); Matrix ans=mul(A,p); printf("%lld\n",ans.a[0][0]); } } return 0;}
- (矩阵快速幂)hdu5950 Recursive sequence
- HDU5950 Recursive sequence(矩阵快速幂)
- HDU5950 Recursive sequence —— 矩阵快速幂
- Recursive sequence——(hdu5950)_(矩阵快速幂)
- hdu5950 Recursive sequence(矩阵快速幂:模板类题目,矩阵快速幂的扩展与学习,复习必看题目)
- HDU5950【矩阵快速幂】
- hdu5950 矩阵快速幂
- 【HDU5950 2016ACM ICPC亚洲区沈阳站-重现赛 C】【矩阵快速幂】Recursive sequence 矩阵递推 i ^ 4
- [hdu 5950 Recursive sequence] 矩阵快速幂
- HDU-5950-Recursive sequence-矩阵快速幂
- HDU 5950 Recursive sequence 矩阵快速幂
- [矩阵快速幂]HDU 5950 Recursive sequence
- HDU5950 Recursive sequence 沈阳赛
- HDU5950(矩阵快速幂)
- HDU5950(矩阵快速幂)
- HDU 5950 Recursive sequence(矩阵构造+矩阵快速幂)
- 矩阵构造+矩阵快速幂-HDU5950
- HDU 5950 Recursive sequence(矩阵快速幂)
- 讨论有状态和无状态的对象
- C#控件之DateTimePicker控件使用
- 掏钱总是痛苦的
- mac python3.* ModuleNotFoundError: No module named 'MySQLdb'
- 运行窗口常用命令(持续更新)
- HDU5950 Recursive sequence(矩阵快速幂)
- eclipse修改Java文件的编码
- Android编译分析
- Integer和int的区别?在什么时候用Integer和什么时候用int
- Android基础总结十一:intent-filter的action,category,data匹配规则
- 关于jquery中on绑定click事件在苹果手机失效的问题
- Android图片之Glide二
- docker相关
- Linux内核:中断、软中断、tasklet