HDU-4686 Arc of Dream 构造矩阵
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686
因为ai = ai-1*AX+AY ,bi = bi-1*BX+BY ,那么ai*bi=AX*BX*A*ai-1*bi-1+AX*BY*ai-1+BX*AY*bi-1+AY*BYAY。令Sn为ai*bi前n项的和,Sn=Sn-1 + an*bn,因此我们可以构造一个如下的转移矩阵:

然后矩阵乘法优化就可以了。。。
注意此题n=0的情况!
其实矩阵大小只要5就可以了,那几个常数项可以合并到一列。。。
//STATUS:C++_AC_1296MS_232KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL n,a0,ax,ay,b0,bx,by; const int size=; struct Matrix{
LL ma[size][size];
Matrix friend operator * (const Matrix a,const Matrix b){
Matrix ret;
mem(ret.ma,);
int i,j,k;
for(i=;i<size;i++)
for(j=;j<size;j++)
for(k=;k<size;k++)
ret.ma[i][j]=(ret.ma[i][j]+a.ma[i][k]*b.ma[k][j]%MOD)%MOD;
return ret;
}
}A; Matrix mutilpow(LL k)
{
int i;
Matrix ret;
mem(ret.ma,);
for(i=;i<size;i++)
ret.ma[i][i]=;
for(;k;k>>=(1LL)){
if(k&(1LL))ret=ret*A;
A=A*A;
}
return ret;
} int main(){
// freopen("in.txt","r",stdin);
int i,j;
LL ans;
LL B[size];
Matrix F;
while(~scanf("%I64d",&n))
{
scanf("%I64d%I64d%I64d",&a0,&ax,&ay);
scanf("%I64d%I64d%I64d",&b0,&bx,&by);
if(n==){printf("0\n");continue;} a0%=MOD;ax%=MOD;ay%=MOD;
b0%=MOD;bx%=MOD;by%=MOD;
mem(A.ma,);
A.ma[][]=A.ma[][]=;
A.ma[][]=ax;A.ma[][]=ay;
A.ma[][]=;
A.ma[][]=bx;A.ma[][]=by;
A.ma[][]=;
A.ma[][]=ax*by%MOD;A.ma[][]=bx*ay%MOD;A.ma[][]=ax*bx%MOD;A.ma[][]=ay*by%MOD;
A.ma[][]=;
F=mutilpow(n-);
B[]=a0*b0%MOD;
B[]=(a0*ax%MOD+ay)%MOD;B[]=;
B[]=(b0*bx%MOD+by)%MOD;B[]=;
B[]=B[]*B[]%MOD;B[]=;
ans=;
for(i=;i<size;i++){
ans=(ans+F.ma[][i]*B[i]%MOD)%MOD;
} printf("%I64d\n",ans);
}
return ;
}
HDU-4686 Arc of Dream 构造矩阵的更多相关文章
- hdu 4686 Arc of Dream(矩阵快速幂乘法)
Problem Description An Arc of Dream is a curve defined by following function: where a0 = A0 ai = ai- ...
- HDU 4686 Arc of Dream (矩阵快速幂)
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4686 Arc of Dream(矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 思路: #include <iostream>#include <cs ...
- HDU 4686 Arc of Dream(递归矩阵加速)
标题效果:你就是给你一程了两个递推公式公式,第一个让你找到n结果项目. 注意需要占用该公式的复发和再构造矩阵. Arc of Dream Time Limit: 2000/2000 MS (Java/ ...
- HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...
- HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4686 Arc of Dream(矩阵)
Arc of Dream [题目链接]Arc of Dream [题目类型]矩阵 &题解: 这题你做的复杂与否很大取决于你建的矩阵是什么样的,膜一发kuangbin大神的矩阵: 还有几个坑点: ...
- hdu 4686 Arc of Dream
思路:构造矩阵 a[i]*b[i]=ax*bx*a[i-1]*b[i-1]+ax*by*a[i-1]+ay*bx*b[i-1]+ay*by 代码如下: #include<iostream> ...
- hdu 4686 Arc of Dream(矩阵快速幂)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...
- HDU 4686 Arc of Dream(快速幂矩阵)
题目链接 再水一发,构造啊,初始化啊...wa很多次啊.. #include <cstring> #include <cstdio> #include <string&g ...
随机推荐
- xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
性能分析:出现如下错误: xcode profile Variable has incomplete type class “CC_DLL” 解决办法:在 xcode的Build Setting ...
- Discuz模版与插件 安装时提示“对不起,您安装的不是正版应用...”解决方法
关于出现“对不起,您安装的不是正版应用..”的解决办法 有些插件和风格在安装时出现不能安装的现象,出现以下提示: 对不起,您安装的不是正版应用,安装程序无法继续执行 点击这里安 ...
- html代码究竟什么用途
1.html代码,只能浏览器识别并读出.渲染出网页图形 2.html代码可以本地写,用浏览器渲染出.也可以服务器端通过http协议传送过来,在网页显示. 咱们上网看的网页都是服务器端通过http协议传 ...
- Qt4.6.2已编译二进制版本在VS2005中的问题
结论1:如果你想把Qt4.6.2安装在VS2005中,又不想花时间编译,请下载和安装qt-win-opensource-4.6.2-vs2008,并单独编译“QT安装路径/src/winmain/” ...
- 雷军北大演讲:除了聪明和勤奋我们还需要什么(关键是有了梦想以后,你能不能把这个东西付诸实践)good
雷军北大演讲:除了聪明和勤奋我们还需要什么 昨天我在乌镇参加了全球互联网峰会,在这个会议上有马云,也有苹果公司的高级副总裁,主持人抛出了一个问题,说雷军你说你有一个目标,要用5到10年的时间做智能手机 ...
- POJ3026——Borg Maze(BFS+最小生成树)
Borg Maze DescriptionThe Borg is an immensely powerful race of enhanced humanoids from the delta qua ...
- Consistent Hashing算法-搜索/负载均衡
在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括: 轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法(Respons ...
- node.js模块之util模块
util提供了各种使用的工具.require('util') to access them. Util.format(format,[..]) Returns a formatted string u ...
- 编写高效的C程序与C代码优化
本文地址:http://www.cnblogs.com/archimedes/p/writing-efficient-c-and-code-optimization.html,转载请注明源地址. 说明 ...
- [原]Unity3D深入浅出 - 物理引擎之刚体部件(Rigidbody)
在虚拟世界中,任何物体都是没有活力的,要想变的真实,Rigidbody是必不可少的组件,下面介绍Rigidbody的各个属性: Mass:质量 Drag:阻力,对象在运动时遇到的空气阻力,0表示没有空 ...