CF385E 翻
[[math]] [[matrices]] [[fast_exponentiation]]
link
Consider ignoring the modulo, then calculate at the same time.
\[\begin{aligned}
dx&=x+y+dx+t\\
dy&=x+y+dy+t\\
x&=dx+2x+y+t\\
y&=dy+x+2y+t\\
t&=t+1
\end{aligned}
\]
Matrix fast exponentiation
\[\begin{aligned}
dx=1*dx+0*dy+1*x+1*y+1*t+0*1\\
dy=0*dx+1*dy+1*x+1*y+1*t+0*1\\
x=1*dx+0*dy+2*x+1*y+1*t+0*1\\
y=0*dx+1*dy+1*x+2*y+1*t+0*1\\
t=0*dx+0*dy+0*x+0*y+1*t+1*1\\
1=0*dx+0*dy+0*x+0*y+0*t+1*1\end{aligned}
\]
We can use matrices to describe it.
\[\left[\begin{array}{}
dx\\dy\\x\\y\\t\\1
\end{array}
\right]
\left[\begin{array}{}
1&0&1&0&0&0\\
0&1&0&1&0&0\\
1&1&2&1&0&0\\
1&1&1&2&0&0\\
1&1&1&1&1&0\\
0&0&0&0&1&1
\end{array}
\right]=\left[\begin{array}{}
x+y+dx+t\\x+y+dy+t\\dx+2x+y+t\\dy+x+2y+t\\t+1\\1
\end{array}
\right]
\]
Notice the 1.
Then we can use matrix fast exponentiation.
It consists of fast exponentiation, multiplication and square.
Multiplication is a multiplication of matrices of (n,n) and (1,n), whose answer is a matrix of (n,1)
Square is a multiplication of two matrices of (n,n), whose answer is also a matrix of (n,n)
Fast exponentiation is the same of the fast exponentiation of numbers.
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,x,y,dx,dy,t,ret;
ll _[6][6]={
{1,0,1,0,0,0},
{0,1,0,1,0,0},
{1,1,2,1,0,0},
{1,1,1,2,0,0},
{1,1,1,1,1,0},
{0,0,0,0,1,1}
},o[6][6],a[6],b[6];
void mult(){
int i,j;
for(i=0;i<6;i++)
for(b[i]=0,j=0;j<6;j++)(b[i]+=_[j][i]*a[j])%=n;
for(i=0;i<6;i++)a[i]=b[i];
}
void square(){
int i,j,k;
for(i=0;i<6;i++)
for(j=0;j<6;j++)
for(o[i][j]=0,k=0;k<6;k++)
(o[i][j]+=_[i][k]*_[k][j])%=n;
for(i=0;i<6;i++)
for(j=0;j<6;j++)_[i][j]=o[i][j];
}
void fpw(ll a){
for(;a;a>>=1,square())if(a&1)mult();
}
int main(){
scanf("%lld%lld%lld%lld%lld%lld",&n,&x,&y,&dx,&dy,&t);
if(t==0)printf("%lld %lld",x,y),exit(0);
dx=(dx%n+n)%n;dy=(dy%n+n)%n;
a[0]=dx,a[1]=dy,a[2]=x,a[3]=y;a[5]=1;
fpw(t);
if(a[2]==0)a[2]=n;
if(a[3]==0)a[3]=n;
printf("%lld %lld",a[2],a[3]);
}

浙公网安备 33010602011771号