博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
阅读量:5278 次
发布时间:2019-06-14

本文共 4427 字,大约阅读时间需要 14 分钟。

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 249    Accepted Submission(s): 140

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
2 3 1 2 4 1 10
 

 

Sample Output
85 369
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.
 

 

Source
 

 

Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:            
 

 

 |   |   | 

 

 

题目链接:

  

题目大意:

  Fi=Fi-1+2Fi-2+i4。给定F1和F2求Fn

题目思路:

  【递推+矩阵快速幂】

  现场用算了1个多小时的公式过了。

  主要还是我太菜。递推写的太少。

  先考虑f(i)=f(i-1)+2f(i-2),很容易写出递推矩阵

    0 2

    1 1

  (i+1)4=i4+4i3+6i2+4i+1。

  所以需要在递推矩阵种存下i的4 3 2 1 0次幂,以便推出(i+1)4,矩阵为

    1 0 0 0 0

    4 1 0 0 0

    6 3 1 0 0

    4 3 2 1 0

    1 1 1 1 1

  于是f={fi-1,fi,i4,i3,i2,i1,i0},将以上两个矩阵合并,即可推出{fi,fi+1,(i+1)4,(i+1)3,(i+1)2,(i+1)1,(i+1)0}.矩阵如下

    0 2 0 0 0 0 0

    1 1 0 0 0 0 0

    0 1 1 0 0 0 0

    0 4 4 1 0 0 0

    0 6 6 3 1 0 0

    0 4 4 3 2 1 0

    0 1 1 1 1 1 1

  推出转移矩阵后只需要根据n求矩阵快速幂即可。

1 //  2 //by coolxxx  3 //#include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 //#include
19 #include
20 #pragma comment(linker,"/STACK:1024000000,1024000000") 21 #define min(a,b) ((a)<(b)?(a):(b)) 22 #define max(a,b) ((a)>(b)?(a):(b)) 23 #define abs(a) ((a)>0?(a):(-(a))) 24 #define lowbit(a) (a&(-a)) 25 #define sqr(a) ((a)*(a)) 26 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b)) 27 #define mem(a,b) memset(a,b,sizeof(a)) 28 #define eps (1e-8) 29 #define J 10000 30 #define mod 2147493647 31 #define MAX 0x7f7f7f7f 32 #define PI 3.14159265358979323 33 #define N 14 34 #define M 7 35 using namespace std; 36 typedef long long LL; 37 double anss; 38 LL aans; 39 int cas,cass; 40 int n,m,lll,ans; 41 LL f[N]; 42 LL a[N][N]; 43 LL ma[N][N]={ { 0}, 44 { 0,0,2,0,0,0,0,0}, 45 { 0,1,1,0,0,0,0,0}, 46 { 0,0,1,1,0,0,0,0}, 47 { 0,0,4,4,1,0,0,0}, 48 { 0,0,6,6,3,1,0,0}, 49 { 0,0,4,4,3,2,1,0}, 50 { 0,0,1,1,1,1,1,1}}; 51 void multi(LL a[][N],LL b[][N],LL c[][N]) 52 { 53 int i,j,k; 54 LL t[N][N]; 55 mem(t,0); 56 for(i=1;i<=M;i++) 57 for(j=1;j<=M;j++) 58 for(k=1;k<=M;k++) 59 t[i][j]=(t[i][j]+a[i][k]*b[k][j]%mod)%mod; 60 memcpy(c,t,sizeof(t)); 61 } 62 void mi(LL a[][N],int y) 63 { 64 LL tmp[N][N]; 65 mem(tmp,0); 66 tmp[1][1]=tmp[2][2]=tmp[3][3]=tmp[4][4]=tmp[5][5]=tmp[6][6]=tmp[7][7]=1; 67 while(y) 68 { 69 if(y&1)multi(tmp,a,tmp); 70 y>>=1;multi(a,a,a); 71 } 72 memcpy(a,tmp,sizeof(tmp)); 73 } 74 void work() 75 { 76 LL t[N]; 77 mem(t,0); 78 int i,j; 79 for(i=1;i<=M;i++) 80 for(j=1;j<=M;j++) 81 t[i]=(t[i]+f[j]*a[j][i]%mod)%mod; 82 memcpy(f,t,sizeof(t)); 83 } 84 int main() 85 { 86 #ifndef ONLINE_JUDGE 87 freopen("1.txt","r",stdin); 88 // freopen("2.txt","w",stdout); 89 #endif 90 int i,j,k; 91 int x,y,z; 92 // init(); 93 for(scanf("%d",&cass);cass;cass--) 94 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++) 95 // while(~scanf("%s",s)) 96 // while(~scanf("%d%d",&n,&m)) 97 { 98 memcpy(a,ma,sizeof(a)); 99 scanf("%d%lld%lld",&n,&f[1],&f[2]);100 f[3]=16,f[4]=8,f[5]=4,f[6]=2,f[7]=1;101 if(n==1)102 {103 printf("%lld\n",f[1]);104 continue;105 }106 mi(a,n-2);107 work();108 printf("%lld\n",f[2]);109 }110 return 0;111 }112 /*113 //114 115 //116 */
View Code

 

转载于:https://www.cnblogs.com/Coolxxx/p/6021332.html

你可能感兴趣的文章
P1192-台阶问题
查看>>
Java大数——a^b + b^a
查看>>
简单的数据库操作
查看>>
帧的最小长度 CSMA/CD
查看>>
树状数组及其他特别简单的扩展
查看>>
普通求素数和线性筛素数
查看>>
PHP截取中英文混合字符
查看>>
电子眼抓拍大解密
查看>>
51nod1076 (边双连通)
查看>>
Linux pipe函数
查看>>
Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
查看>>
程序的静态链接,动态链接和装载 (补充)
查看>>
关于本博客说明
查看>>
[Kaggle] Sentiment Analysis on Movie Reviews
查看>>
价值观
查看>>
mongodb命令----批量更改文档字段名
查看>>
国外常见互联网盈利创新模式
查看>>
android:scaleType属性
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>