nyist 301 递推求值(矩阵快速幂)
2016-05-17 21:36
148 查看
题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=301
思路:注意矩阵乘法不满足交换律,所以要注意初始矩阵在右边,要进行快速幂的矩阵在左边
左边的矩阵为 右边的矩阵为
b a c f2 0 0
1 0 0 f1 0 0
0 0 1 1 0 0
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 40000;
const int mod = 1000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct node
{
ll a[3][3];
};
node matrixmul(node a,node b)
{
node c;
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
c.a[i][j] = 0;
for(int k=0; k<3; k++)
{
c.a[i][j] += a.a[i][k] * b.a[k][j];
c.a[i][j] %= mod;
}
}
}
return c;
}
node quickmod(node s,int k)
{
node ans;
memset(ans.a,0,sizeof(s.a));
ans.a[0][0]=ans.a[1][1]=ans.a[2][2]=1;//初始化为单位矩阵,值不变
while(k)
{
if(k & 1)
ans = matrixmul(ans,s);
k = k >> 1;
s = matrixmul(s,s);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int f1,f2,d,e,f,n;
scanf("%d%d%d%d%d%d",&f1,&f2,&d,&e,&f,&n);
if(n == 1)
{
printf("%d\n",(f1+mod)%mod);
continue;
}
if(n == 2)
{
printf("%d\n",(f2+mod)%mod);
continue;
}
node sa;
memset(sa.a,0,sizeof(sa.a));
sa.a[0][0]=e;
sa.a[0][1]=d;
sa.a[0][2]=f;
sa.a[1][0]=1;
sa.a[2][2]=1;
sa = quickmod(sa,n-2);
node sb;
memset(sb.a,0,sizeof(sb.a));
sb.a[0][0]=f2;
sb.a[1][0]=f1;
sb.a[2][0]=1;
sb = matrixmul(sa,sb);//注意顺序
ll ans = (sb.a[0][0]+mod)%mod;
printf("%I64d\n",ans);
}
return 0;
}
大神地址:http://www.lxway.com/146981016.htm
思路:注意矩阵乘法不满足交换律,所以要注意初始矩阵在右边,要进行快速幂的矩阵在左边
左边的矩阵为 右边的矩阵为
b a c f2 0 0
1 0 0 f1 0 0
0 0 1 1 0 0
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 40000;
const int mod = 1000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
struct node
{
ll a[3][3];
};
node matrixmul(node a,node b)
{
node c;
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
c.a[i][j] = 0;
for(int k=0; k<3; k++)
{
c.a[i][j] += a.a[i][k] * b.a[k][j];
c.a[i][j] %= mod;
}
}
}
return c;
}
node quickmod(node s,int k)
{
node ans;
memset(ans.a,0,sizeof(s.a));
ans.a[0][0]=ans.a[1][1]=ans.a[2][2]=1;//初始化为单位矩阵,值不变
while(k)
{
if(k & 1)
ans = matrixmul(ans,s);
k = k >> 1;
s = matrixmul(s,s);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int f1,f2,d,e,f,n;
scanf("%d%d%d%d%d%d",&f1,&f2,&d,&e,&f,&n);
if(n == 1)
{
printf("%d\n",(f1+mod)%mod);
continue;
}
if(n == 2)
{
printf("%d\n",(f2+mod)%mod);
continue;
}
node sa;
memset(sa.a,0,sizeof(sa.a));
sa.a[0][0]=e;
sa.a[0][1]=d;
sa.a[0][2]=f;
sa.a[1][0]=1;
sa.a[2][2]=1;
sa = quickmod(sa,n-2);
node sb;
memset(sb.a,0,sizeof(sb.a));
sb.a[0][0]=f2;
sb.a[1][0]=f1;
sb.a[2][0]=1;
sb = matrixmul(sa,sb);//注意顺序
ll ans = (sb.a[0][0]+mod)%mod;
printf("%I64d\n",ans);
}
return 0;
}
大神地址:http://www.lxway.com/146981016.htm
相关文章推荐
- 控制策略模型建模规范(命名原则)
- LeetCode Evaluate Reverse Polish Notation
- Linux下文件的三个时间:ctime、mtime、atime的区别
- C语言中memset函数详解
- MFC六大核心机制之四:永久保存(串行化)
- Tomcat 7优化前及优化后的性能对比
- 核心编程(第十章)嵌套函数,装饰器
- cocos2dx支动画的实现
- web编程的一些概念
- nginx变量使用方法详解(2)
- redis分享会笔记
- poj1099
- 构建之法阅读笔记08
- #include”* .h“ 在查找预编译头使用时跳过
- hdu3308 LCIS(区间求和)
- HashMap的内部实现机制,Hash是怎样实现的,什么时候ReHash
- 链表指定区间元素的反转
- bzoj 1002: [FJOI2007]轮状病毒(打表找规律或基尔霍夫矩阵)
- 膨胀和腐蚀
- 面向接口编程以及面向接口编程的好处