hdu 5950 Recursive sequence(递推 快速幂矩阵)
2017-11-04 21:34
274 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5950
参考自:http://blog.csdn.net/spring371327/article/details/52973534
![](https://img-blog.csdn.net/20171104213320879?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzM5OTc1NzI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 2147493647;
//定义矩阵乘法
struct matrix{
ll arr[7][7];
matrix operator*(matrix b){
matrix ans;
ll tmp;
for(int i=0; i<7; i++)
for(int j=0; j<7; j++){
ans.arr[i][j] = 0;
for(int k=0; k<7; k++){
tmp = (arr[i][k]*b.arr[k][j])%mod;
ans.arr[i][j] = (ans.arr[i][j] + tmp)%mod;
}
}
return ans;
}
};
//矩阵快速幂
matrix quick_pow(matrix a,ll N){
matrix ans;
memset(ans.arr,0,sizeof(ans.arr));
for(int i=0; i<7; i++)
ans.arr[i][i] = 1;
while(N){
if(N&1)
ans = ans*a;
a = a*a;
N /= 2;;
}
return ans;
}
int main(){
matrix a;
memset(a.arr,0,sizeof(a.arr));
int tmp[7][7]={{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}};
for(int i=0;i<7;i++)for(int j=0;j<7;j++) a.arr[i][j]=tmp[i][j];
int T;
scanf("%d",&T);
ll N,aa,bb;
while(T--){
scanf("%lld%lld%lld",&N,&aa,&bb);
if(N==1)
printf("%lld\n",aa);
else if(N==2)
printf("%lld\n",bb);
else{
matrix ans = quick_pow(a,N-2);
ll ANS = 0;
ANS = (ANS+ans.arr[1][0]*aa)%mod;
ANS = (ANS+ans.arr[1][1]*bb)%mod;
ANS = (ANS+ans.arr[1][2]*16)%mod;
ANS = (ANS+ans.arr[1][3]*8)%mod;
ANS = (ANS+ans.arr[1][4]*4)%mod;
ANS = (ANS+ans.arr[1][5]*2)%mod;
ANS = (ANS+ans.arr[1][6]*1)%mod;
printf("%lld\n",ANS);
}
}
return 0;
}
参考自:http://blog.csdn.net/spring371327/article/details/52973534
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 2147493647;
//定义矩阵乘法
struct matrix{
ll arr[7][7];
matrix operator*(matrix b){
matrix ans;
ll tmp;
for(int i=0; i<7; i++)
for(int j=0; j<7; j++){
ans.arr[i][j] = 0;
for(int k=0; k<7; k++){
tmp = (arr[i][k]*b.arr[k][j])%mod;
ans.arr[i][j] = (ans.arr[i][j] + tmp)%mod;
}
}
return ans;
}
};
//矩阵快速幂
matrix quick_pow(matrix a,ll N){
matrix ans;
memset(ans.arr,0,sizeof(ans.arr));
for(int i=0; i<7; i++)
ans.arr[i][i] = 1;
while(N){
if(N&1)
ans = ans*a;
a = a*a;
N /= 2;;
}
return ans;
}
int main(){
matrix a;
memset(a.arr,0,sizeof(a.arr));
int tmp[7][7]={{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}};
for(int i=0;i<7;i++)for(int j=0;j<7;j++) a.arr[i][j]=tmp[i][j];
int T;
scanf("%d",&T);
ll N,aa,bb;
while(T--){
scanf("%lld%lld%lld",&N,&aa,&bb);
if(N==1)
printf("%lld\n",aa);
else if(N==2)
printf("%lld\n",bb);
else{
matrix ans = quick_pow(a,N-2);
ll ANS = 0;
ANS = (ANS+ans.arr[1][0]*aa)%mod;
ANS = (ANS+ans.arr[1][1]*bb)%mod;
ANS = (ANS+ans.arr[1][2]*16)%mod;
ANS = (ANS+ans.arr[1][3]*8)%mod;
ANS = (ANS+ans.arr[1][4]*4)%mod;
ANS = (ANS+ans.arr[1][5]*2)%mod;
ANS = (ANS+ans.arr[1][6]*1)%mod;
printf("%lld\n",ANS);
}
}
return 0;
}
相关文章推荐
- ICPC 沈阳站C题 HDU 5950 Recursive sequence 矩阵快速幂 线性递推
- HDU 5950(矩阵快速幂)
- hdu 5950 Recursive sequence (矩阵快速幂)
- HDU 5015 233Matrix(矩阵快速幂+递推)
- HDU 5950 矩阵快速幂
- hdu 1143 递推 + 矩阵快速幂做法
- HDU 5950 Recursive sequence(矩阵快速幂)
- hdu 6030 Happy Necklace (递推,矩阵快速幂)
- [HDOJ5950]Recursive sequence(递推,二项展开,矩阵快速幂)
- hdu 5950 Recursive sequence(矩阵快速幂,构造)
- hdu 2842(矩阵快速幂+递推)
- HDU - 5950 - Recursive sequence 【巧妙构建矩阵快速幂】
- hdu 5950 矩阵加速递推
- hdu 4602 递推关系矩阵快速幂模
- Reading comprehension HDU - 4990(矩阵快速幂 递推)
- hdu 2604 递推 矩阵快速幂
- HDU 2604 递推 + 矩阵快速幂
- HDU 5950 Recursive sequence(矩阵快速幂)
- HDU 5667 矩阵快速幂关于指数的递推
- hdu 5015 233 Matrix (递推 矩阵快速幂)