bnu新生赛D 柯南的精灵(矩阵快速幂)
2014-12-27 18:28
411 查看
题目地址:http://www.bnuoj.com/v3/contest_show.php?cid=5727#problem/D
学了矩阵快速幂之后,终于用到一次了。。没学之前天天遇到。。学了之后终于遇到一次了。。。特来纪念一下
分别设方程f(x)为发育期精灵,g(x)为更年期精灵,h(x)为天数。
然后
f(x-1) f(x)
g(x-1) * A = g(x)
h(x-1) h(x)
1 1
关系矩阵为
0 y p 0
x z 0 0
0 0 1 1
0 0 0 1
然后利用矩阵快速幂即可。
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define LL long long
const int mod=1e9+7;
struct matrix
{
LL ma[5][5];
}init, res;
matrix Mult(matrix x, matrix y)
{
matrix tmp;
int i, j, k;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
tmp.ma[i][j]=0;
for(k=0;k<4;k++)
{
tmp.ma[i][j]=tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j];
}
}
}
return tmp;
}
matrix Pow(matrix x, LL k)
{
matrix tmp;
int i, j;
for(i=0;i<4;i++) for(j=0;j<4;j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
}
int main()
{
LL t, x, y, z, p, i, j, n, a[6];
LL ans1, ans2;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld%lld%lld%lld",&x,&y,&z,&p,&n);
a[0]=a[1]=a[2]=0;a[3]=1;
memset(init.ma,0,sizeof(init.ma));
init.ma[0][1]=y;init.ma[0][2]=p;
init.ma[1][0]=x;init.ma[1][1]=z;
init.ma[2][2]=init.ma[2][3]=init.ma[3][3]=1;
res=Pow(init,n);
ans1=1*res.ma[0][3];
ans2=1*res.ma[1][3];
printf(">>%lld\n",ans1+ans2);
}
return 0;
}
学了矩阵快速幂之后,终于用到一次了。。没学之前天天遇到。。学了之后终于遇到一次了。。。特来纪念一下
分别设方程f(x)为发育期精灵,g(x)为更年期精灵,h(x)为天数。
然后
f(x-1) f(x)
g(x-1) * A = g(x)
h(x-1) h(x)
1 1
关系矩阵为
0 y p 0
x z 0 0
0 0 1 1
0 0 0 1
然后利用矩阵快速幂即可。
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define LL long long
const int mod=1e9+7;
struct matrix
{
LL ma[5][5];
}init, res;
matrix Mult(matrix x, matrix y)
{
matrix tmp;
int i, j, k;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
tmp.ma[i][j]=0;
for(k=0;k<4;k++)
{
tmp.ma[i][j]=tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j];
}
}
}
return tmp;
}
matrix Pow(matrix x, LL k)
{
matrix tmp;
int i, j;
for(i=0;i<4;i++) for(j=0;j<4;j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&1) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=1;
}
return tmp;
}
int main()
{
LL t, x, y, z, p, i, j, n, a[6];
LL ans1, ans2;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld%lld%lld%lld",&x,&y,&z,&p,&n);
a[0]=a[1]=a[2]=0;a[3]=1;
memset(init.ma,0,sizeof(init.ma));
init.ma[0][1]=y;init.ma[0][2]=p;
init.ma[1][0]=x;init.ma[1][1]=z;
init.ma[2][2]=init.ma[2][3]=init.ma[3][3]=1;
res=Pow(init,n);
ans1=1*res.ma[0][3];
ans2=1*res.ma[1][3];
printf(">>%lld\n",ans1+ans2);
}
return 0;
}
相关文章推荐
- bnu 34985 Elegant String(矩阵快速幂+dp推导公式)
- bnu 34895 Elegant String(矩阵快速幂)
- POJ 3150 矩阵快速幂
- 2014北京邀请赛E题-矩阵快速幂
- So Easy! HDU - 4565 矩阵快速幂
- hdoj M斐波那契数列 4549&nyoj 又见斐波那契数列 1000 (矩阵快速幂&规律)
- Delphi下实现全屏快速找图找色 二、矩阵遍历
- 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2
- hdu 6030 Happy Necklace (递推,矩阵快速幂)
- [HIHO1555] 四次方根(递推,容斥,矩阵快速幂)
- hdu5667 矩阵快速幂
- 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
- BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)
- 矩阵快速幂
- 51nod 1126 求递推序列的第N项 (矩阵快速幂)
- hdu_2842_Chinese Rings(矩阵快速幂)
- 稀疏矩阵快速转置
- hdu 2256 Problem of Precision -矩阵快速幂
- Quad Tiling POJ - 3420 矩阵快速幂
- 【矩阵快速幂】Fibonacci