Recursive sequence
来源:互联网 发布:手链饰品店淘宝 编辑:程序博客网 时间:2024/05/22 04:36
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2204 Accepted Submission(s): 975
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, 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.
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.
Each case contains only one line with three numbers N, a and b where N,a,b <
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
85369HintIn 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.
Source
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
Recommend
jiangzijing2015 | We have carefully selected several similar problems for you: 6216 6215 6214 6213 6212
公式为:a[i+1]=2a[i-1]+a[i]+(i+1)^4,典型的矩阵快速幂,首先将(i+1)^4 展出来 i^4+4*n^3+6*n^2+4*n+1;
即:a[i+1]=2a[i-1]+a[i]+ i^4+4*n^3+6*n^2+4*n+1;然后矩阵快速幂
代码如下:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;typedef long long ll;const ll mod=2147493647;struct Matrix{ ll matrix[7][7];};int n;//阶数void init(Matrix &res){ memset(res.matrix,0,sizeof(res.matrix)); for(int i=0;i<n;i++) res.matrix[i][i]=1;}Matrix multiplicative(Matrix a,Matrix b){ Matrix res; memset(res.matrix,0,sizeof(res.matrix)); for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < n ; j++){ for(int k = 0 ; k < n ; k++){ res.matrix[i][j] = (res.matrix[i][j]+(a.matrix[i][k]%mod)*(b.matrix[k][j]%mod))%mod; } } } return res;}Matrix pow(Matrix mx,int m){ Matrix res,base=mx; init(res); while(m) { if(m&1) res=multiplicative(res,base); base=multiplicative(base,base); m>>=1; } return res;}int main(){ ll t,m,a,b; n=7; Matrix mx={ 0,1,0,0,0,0,0, 2,1,1,4,6,4,1, 0,0,1,4,6,4,1, 0,0,0,1,3,3,1, 0,0,0,0,1,2,1, 0,0,0,0,0,1,1, 0,0,0,0,0,0,1}; scanf("%d",&t); while(t--) { scanf("%lld%lld%lld",&m,&a,&b); if(m==1) printf("%lld\n",a); else if(m==2) printf("%lld\n",b); else { Matrix res=pow(mx,m-2); ll ans=0; ans=(ans+a*res.matrix[1][0])%mod; ans=(ans+b*res.matrix[1][1])%mod; ans=(ans+16*res.matrix[1][2])%mod; ans=(ans+8*res.matrix[1][3])%mod; ans=(ans+4*res.matrix[1][4])%mod; ans=(ans+2*res.matrix[1][5])%mod; ans=(ans+res.matrix[1][6])%mod; printf("%lld\n",ans); } } return 0;}
阅读全文
0 0
- Recursive sequence
- 递归数列(recursive sequence)
- Problem C. Recursive sequence
- HDU 5950 Recursive sequence
- HDU 5950(Recursive sequence)
- HDU 5950 Recursive sequence
- HDU 5950 Recursive sequence
- Recursive sequence HDU
- hdoj 5950 Recursive sequence
- hdu 5950 Recursive sequence
- hdu 5950 Recursive sequence
- Recursive sequence HDU
- HDU5950 Recursive sequence 沈阳赛
- 【矩阵加速】[SPOJ SEQ]Recursive Sequence
- Sequence Modeling:Recurrent and Recursive Nets
- [hdu 5950 Recursive sequence] 矩阵快速幂
- HDU-5950-Recursive sequence-矩阵快速幂
- HDU 5950 Recursive sequence 矩阵快速幂
- 第一次开通博客
- 【Python】virtualenv管理Python虚拟环境
- 更新
- 编译Qt4.8.5源码
- AForge Video
- Recursive sequence
- 【Java开发】使用Semaphore控制资源访问并发量
- 659. Split Array into Consecutive Subsequences
- UnityShader初级篇——实现逐顶点高光反射光照模型
- HDPCD-Java-复习笔记(11)
- JQuery
- 跟磊哥学工控-第一课 设备的模式和状态
- 洛谷10月月赛R1·浴谷八连测R1·提高组 T2
- linux 虚拟机环境 rpm方式安装 jdk1.8