hdu 5950 数学公式 + 矩阵快速幂
2017-10-15 14:16
337 查看
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2205 Accepted Submission(s): 976
[align=left]Problem Description[/align]
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.
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3 1 2
4 1 10
[align=left]Sample Output[/align]
85
369
HintIn 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^4 化简 n^4=(n-1)^4+4*(n-1)^3+6*(n-1)^2+4*(n-1)^+1;
具体构造矩阵请看 代码:
#include <iostream> #include <stdio.h> #include <string.h> #define N 7 using namespace std; typedef long long ll; const ll mod=2147493647; ll a ,temp ,res ,c[3]; ll x,y,n; void init() { a[0][0]=1,a[0][1]=2,a[0][2]=1,a[0][3]=4,a[0][4]=6,a[0][5]=4,a[0][6]=1; a[1][0]=1,a[1][1]=0,a[1][2]=0,a[1][3]=0,a[1][4]=0,a[1][5]=0,a[1][6]=0; a[2][0]=0,a[2][1]=0,a[2][2]=1,a[2][3]=4,a[2][4]=6,a[2][5]=4,a[2][6]=1; a[3][0]=0,a[3][1]=0,a[3][2]=0,a[3][3]=1,a[3][4]=3,a[3][5]=3,a[3][6]=1; a[4][0]=0,a[4][1]=0,a[4][2]=0,a[4][3]=0,a[4][4]=1,a[4][5]=2,a[4][6]=1; a[5][0]=0,a[5][1]=0,a[5][2]=0,a[5][3]=0,a[5][4]=0,a[5][5]=1,a[5][6]=1; a[6][0]=0,a[6][1]=0,a[6][2]=0,a[6][3]=0,a[6][4]=0,a[6][5]=0,a[6][6]=1; } void pw(ll a ,ll b ) { memset(temp,0,sizeof(temp)); for (int i=0;i<N;i++) for (int j=0;j<N;j++) for (int k=0;k<N;k++) temp[i][j]=(temp[i][j]%mod+a[i][k]*b[k][j]%mod)%mod; for (int i=0;i<N;i++) for (int j=0;j<N;j++) a[i][j]=temp[i][j]; } void ppw(ll a ,ll nu) { memset(res,0,sizeof(res)); for (int i=0;i<N;i++) res[i][i]=1; while (nu) { if (nu&1) pw(res,a); pw(a,a); nu>>=1; } } void init1() { c[0]=(2*x%mod+y%mod+81)%mod; c[1]=y%mod; } int main() { int t; ll ans; cin>>t; while (t--) { scanf("%lld%lld%lld",&n,&x,&y); ans=0; if (n==1) cout<<x%mod<<endl; else if (n==2) cout<<y%mod<<endl; else if (n==3) cout<<(2*x%mod+y%mod+81)%mod<<endl; else { init(); init1(); ppw(a,n-3); ans=(ans+c[0]*res[0][0]%mod)%mod; ans=(ans+c[1]*res[0][1]%mod)%mod; ans=(ans+81*res[0][2]%mod)%mod; ans=(ans+27*res[0][3]%mod)%mod; ans=(ans+9*res[0][4]%mod)%mod; ans=(ans+3*res[0][5]%mod)%mod; ans=(ans+res[0][6]%mod)%mod; printf("%lld\n",ans); } } return 0; }
相关文章推荐
- 数学公式+矩阵快速幂 hdu-4565 So Easy!
- (矩阵快速幂, 数学公式推导)HDU 6050 Funny Function
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
- 【瞌睡的胡策】训练2.23(数学相关+矩阵快速幂+dp)
- 矩阵快速幂 [HNOI2011]数学作业
- HDU 3117 Fibonacci Numbers(矩阵快速幂+公式)
- zzuli 2180 GJJ的日常之沉迷数学【矩阵快速幂 || 逆元】
- HDU 5690 查找循环节 数学公式快速幂+乘法逆元(除法取模)
- bnu 34985 Elegant String(矩阵快速幂+dp推导公式)
- latex数学公式编写(一):latex矩阵编写
- HDU4565 So Easy!(数学+矩阵快速幂)
- HDU 4565 So Easy!(数学+矩阵快速幂)
- [矩阵快速幂 数学] UR #16 A.破坏发射台
- WOJ1540 Fibonacci(数学推导,矩阵快速幂)
- 【洛谷P1962 斐波那契数列】矩阵快速幂+数学推导
- HDU-4565 So Easy!(数学&&矩阵快速幂)
- [BZOJ 2326][HNOI 2011]数学作业(矩阵快速幂)
- BZOJ2326 HNOI2011数学作业(矩阵快速幂)
- HDU 6145 Arithmetic of Bomb II(数学+矩阵快速幂)
- hdu 3411 推公式+矩阵快速幂