HDU-4686 Arc of Dream 构造矩阵

时间:2021-09-14 07:27:51

  题目链接: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,因此我们可以构造一个如下的转移矩阵:

HDU-4686 Arc of Dream 构造矩阵

  然后矩阵乘法优化就可以了。。。

  注意此题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 ;
}