hdu 6172 矩阵快速幂 找规律
来源:互联网 发布:张伯礼 知乎 编辑:程序博客网 时间:2024/05/21 21:49
Array Challenge
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others)Total Submission(s): 206 Accepted Submission(s): 92
Problem Description
There’s an array that is generated by following rule.
h0=2,h1=3,h2=6,hn=4hn−1+17hn−2−12hn−3−16
And let us define two arraysbnandan as below.
bn=3hn+1hn+9hn+1hn−1+9h2n+27hnhn−1−18hn+1−126hn−81hn−1+192(n>0)
an=bn+4n
Now, you have to print⌊√(an)⌋ , n>1.
Your answer could be very large so print the answer modular 1000000007.
And let us define two arrays
Now, you have to print
Your answer could be very large so print the answer modular 1000000007.
Input
The first line of input contains T (1 <= T <= 1000) , the number of test cases.
Each test case contains one integer n (1 < n <=1015 ) in one line.
Each test case contains one integer n (1 < n <=
Output
For each test case print ⌊√(a_n )⌋ modular 1000000007.
Sample Input
3479
Sample Output
125532472513185773
Source
2017 Multi-University Training Contest - Team 10
题意:
求
题解:
设
又
可以发现
于是根据
是一个线性递推式,用矩阵快速幂
计算an这个数组前几个数字的代码:
然后就可以发现规律了
#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define LL long longLL k;struct Matrix{ int m[10][10];};Matrix unit,init;void Init(){ memset(init.m,0,sizeof(init.m)); init.m[1][0]=1; init.m[2][1]=1; init.m[3][3]=1; init.m[0][0]=4; init.m[0][1]=17; init.m[0][2]=-12; init.m[0][3]=-16; memset(unit.m,0,sizeof(unit.m)); for(int i=0;i<4;i++) unit.m[i][i]=1;}Matrix Mul(Matrix a,Matrix b){ Matrix c; for(int i=0;i<4;i++) for(int j=0;j<4;j++){ c.m[i][j]=0; for(int k=0;k<4;k++) c.m[i][j]+=(a.m[i][k]*b.m[k][j]); } return c;}Matrix Pow(Matrix a,Matrix b,int x){ while(x) { if(x&1) b=Mul(a,b); a=Mul(a,a); x>>=1; } return b;}LL fast_pow(LL a,LL b){ LL ans=1; while(b) { if(b&1) ans*=a; a*=a; b>>=1; } return ans;}int main(){ LL n; int a[10]={6,3,2,1}; freopen("in.txt","r",stdin); while(scanf("%lld",&n)!=EOF) { Init(); Matrix res=Pow(init,unit,n-2); LL ans=0,h3=0,h2=0,h1=0; for(int i=0;i<4;i++) h3+=res.m[0][i]*a[i]; for(int i=0;i<4;i++) h2+=res.m[1][i]*a[i]; for(int i=0;i<4;i++) h1+=res.m[2][i]*a[i]; ans=3*h3*h2+9*h3*h1+9*h2*h2+27*h2*h1; ans=ans-18*h3-126*h2-81*h1+192; ans+=fast_pow(4,n-1); LL x=((LL)sqrt(ans*1.0)); printf("%8lld %8lld %8lld\n",x,h3,h3-x); } return 0;}
ac代码,注意精度
#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define LL long long#define mod 1000000007LL k;struct Matrix{ LL m[10][10];};Matrix unit,init;void Init(){ memset(init.m,0,sizeof(init.m)); init.m[1][0]=1; init.m[2][1]=1; init.m[0][0]=4; init.m[0][1]=17; init.m[0][2]=-12; memset(unit.m,0,sizeof(unit.m)); for(int i=0;i<3;i++) unit.m[i][i]=1;}Matrix Mul(Matrix a,Matrix b){ Matrix c; for(int i=0;i<3;i++) for(int j=0;j<3;j++){ c.m[i][j]=0; for(int k=0;k<3;k++) c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j])%mod+mod)%mod; } return c;}Matrix Pow(Matrix a,Matrix b,LL x){ while(x) { if(x&1) b=Mul(a,b); a=Mul(a,a); x>>=1; } return b;}int main(){ LL n,T; int a[10]={1255,197,31}; freopen("in.txt","r",stdin); scanf("%lld",&T); while(T--) { scanf("%lld",&n); if(n==2) puts("31"); else if(n==3) puts("197"); else if(n==4) puts("1255"); else{ Init(); Matrix res=Pow(init,unit,n-4); LL h=(res.m[0][0]*1255%mod+res.m[0][1]*197%mod+res.m[0][2]*31%mod+mod)%mod; printf("%lld\n",h); } } return 0;}
阅读全文
0 0
- hdu 6172 矩阵快速幂 找规律
- HDU 6172 Array Challenge 找规律+矩阵快速幂
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- HDU 4990 Reading comprehension (找规律+矩阵快速幂)
- hdu 3519 快速幂矩阵(7)+找规律
- HDU 4990 (找规律 矩阵快速幂)
- HDU 4990 Reading comprehension(找规律)(矩阵快速幂)
- HDU 6030 Happy Necklace[找规律][矩阵快速幂]
- HDU 4990 Reading comprehension(找规律+矩阵快速幂)
- HDU 6198 number number number【找规律+矩阵快速幂】
- 2017 Multi-University Training Contest 10 1002 Array Challenge HDU 6172(找规律 矩阵快速幂)
- hdu6198(矩阵快速幂+找规律)
- hdu 4506(快速幂+找规律)
- 【bzoj 4547】【Hdu 5171】小奇的集合(找规律+矩阵快速幂)
- HDU 5171 规律+矩阵快速幂(水)
- HDU 4549 M斐波那契数列 (找规律+矩阵快速幂+数快速幂)
- 【矩阵快速幂-找规律】HDOJ Reading comprehension 4990
- SDNU 1313.Chess 找规律 矩阵快速幂
- 迭代器失效问题讲解
- ASP.NET Core 运行原理解剖[1]-Hosting
- 关于物联网
- 交叉编译lis3dh
- 图论总结(10)二分图匹配
- hdu 6172 矩阵快速幂 找规律
- listview双联动
- LinkedList与ArrayList区别
- Redis源码剖析--跳跃表
- KindEditor 自定义插件:实现在内容编辑器中选中任意一张图片将其设置为文章封面缩略图
- Magic Bracelet POJ
- 如何为网站配置(Let’s Encrypt)HTTPS协议
- java编程分享
- 解决webuploader点击开始上传无反应