NOI 2013 矩阵游戏

时间:2021-12-25 23:11:57

http://uoj.ac/problem/124

矩阵乘法。

十进制快速幂。

刚开始还傻傻地写二进制快速幂,然后陈老师一语点醒梦中人......

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int sgn(DB x){if(abs(x)<EPS)return ;return(x>)?:-;}
const DB Pi=acos(-1.0); inline void clear(vector<int> *A,int a,int b){int i,j;A->clear();re(i,,a)re(j,,b)A[i].push_back();} inline int gint()
{
int res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
}
inline LL gll()
{
LL res=;bool neg=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){neg=;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return (neg)?-res:res;
} const LL Mod=;
const int maxlen=; char N[maxlen+],M[maxlen+];
LL a,b,c,d; struct Tmatrix
{
LL v[][];
inline void clear(){mmst(v,);}
inline friend Tmatrix operator *(const Tmatrix &a,const Tmatrix &b)
{
Tmatrix c;
c.v[][]=(a.v[][]*b.v[][]+a.v[][]*b.v[][])%Mod;
c.v[][]=(a.v[][]*b.v[][]+a.v[][]*b.v[][])%Mod;
c.v[][]=(a.v[][]*b.v[][]+a.v[][]*b.v[][])%Mod;
c.v[][]=(a.v[][]*b.v[][]+a.v[][]*b.v[][])%Mod;
return c;
}
}; inline void minus1(char *A)
{
int i,t;
for(t=;A[t]=='';t++);
re(i,,t-)A[i]='';
A[t]--;
} Tmatrix f1,f2,f1n,g; Tmatrix A[maxlen+];
inline Tmatrix power2(Tmatrix a,int k)
{
Tmatrix x,y=a;
x.v[][]=;x.v[][]=;x.v[][]=;x.v[][]=;
while(k!=){if(k&)x=x*y;y=y*y;k>>=;}
return x;
}
inline Tmatrix power(Tmatrix a,char *K)
{
int i,l=strlen(K+);
A[]=a;re(i,,l)A[i]=power2(A[i-],);
Tmatrix x;x.v[][]=;x.v[][]=;x.v[][]=;x.v[][]=;
re(i,,l)x=x*power2(A[i],K[i]-'');
return x;
} int main()
{
freopen("matrix.in","r",stdin);
freopen("matrix.out","w",stdout);
int i,l;
SF("%s",N+);
l=strlen(N+);re(i,,l/)swap(N[i],N[l-i+]);
SF("%s",M+);
l=strlen(M+);re(i,,l/)swap(M[i],M[l-i+]);
a=gint();b=gint();c=gint();d=gint();
f1.v[][]=a;f1.v[][]=b;f1.v[][]=;f1.v[][]=;
f2.v[][]=c;f2.v[][]=d;f2.v[][]=;f2.v[][]=;
minus1(M);
minus1(N);
f1n=power(f1,M);
g=f2*f1n;
g=power(g,N);
g=f1n*g;
cout<<(g.v[][]+g.v[][])%Mod<<endl;
}