QOJ #1874. Goldberg Machine 2 题解

· · 个人记录

模拟赛考了,遂写题解以记之。

题意

有两个 n 行 m 列的网格,两个网格要么对应位置上都是洞,要么都是指针,指针指向下或右。定义一次操作是选择一个网格,从它的左上角开始放一个小球,沿着指针走,走到网格外或洞上为止,并翻转每个经过的指针的状态(下变成右,右变成下)。

$1\le n,m\le 100,q\le 10^5$,保证左上角的格子是指针,且每个网格的每个格子在足够多次操作后都会被经过。 ## 考场思路 容易验证,操作是一个网格到网格的双射(也就是说,它是可逆的)。因此,最优的方法要么是不断操作第一个网格,要么是不断操作第二个网格。只需计算从第一个网格到第二个网格的最少操作次数,反过来可以同样计算(事实上,之后存在更简单的计算方法),取较小值即可。 考虑设在第一个网格上操作了 $x$ 次,那么在固定两个网格的情况下,确定了 $x$ 的话,可以 dp 计算出每个点(和每条边)被经过的次数。而且它其实关于 $x$ 是一次函数(考察转移可知)。那么限制就是每条边上的 $px+q$ 是整数。而且,$p,q$ 的分母一定是 $2$ 的幂。 单次做的话,只需要找出 $p$ 的分母最大的一个,解同余方程求出 $x$,再逐一检验即可。考场上给的部分分是 $77$ 分。 ——其实可以更好一点。如果只要考虑求出 $x$ 不要考虑检验,那么因为 $p$ 不随着修改而变化,而修改对 $q$ 的变化也是线性的(考察转移可知),可以计算修改对最大分母 $p$ 对应的 $q$ 的系数,就可以 $O(1)$ 维护修改。然而检验仍然是困难的。 ## 解法 确定地检验每个数是否为整数是困难的,但是可以考虑随机化。随机一个集合 $S\subseteq T$,若 $T$ 中每个元素都是整数,则 $S$ 中元素的和也是整数,否则是整数的概率不超过 $\dfrac12$。那么我们随机几个 $S$,修改对 $S$ 中元素和的贡献也是线性的。那么就可以快速地检验一个解是否合法,此题得解。 假定高精度计算是 $O(1)$ 的,要求**单组询问**不超过 $2^{-B}$ 的错误率(总错误率不超过 $q2^{-B}$),时间复杂度 $O((nm+q)B)$。 ## 代码 ```cpp #include <bits/stdc++.h> #define rep(i,n) for(int i=0,del##i##verme=int(n);i<del##i##verme;++i) #define rep1(i,n) for(int i=1,parano##i##a=int(n);i<=parano##i##a;++i) #define per(i,n) for(int i=int(n)-1;i>=0;--i) #define per1(i,n) for(int i=int(n);i>=1;--i) typedef unsigned long long ull; using namespace std; unsigned time_related_rand() { return unsigned(std::chrono::steady_clock::now().time_since_epoch().count()); } struct uint256_t{ unsigned __int128 low,high; uint256_t() { low=0;high=0; } uint256_t(unsigned __int128 lw) { low=lw;high=0; } uint256_t(unsigned __int128 lw,unsigned __int128 hg) { low=lw;high=hg; } }; typedef unsigned __int128 UL; typedef uint256_t ULL; const UL MASK=ull(-1); ULL operator+(const ULL&x,const ULL&y) { UL lw=x.low+y.low,hg=x.high+y.high; if(lw<x.low) ++hg; return ULL(lw,hg); } ULL operator-(const ULL&x,const ULL&y) { UL lw=x.low-y.low,hg=x.high-y.high; if(lw>x.low) --hg; return ULL(lw,hg); } ULL operator*(const ULL&x,const ULL&y) { UL x0=x.low&MASK,x1=x.low>>64,x2=x.high&MASK; UL y0=y.low&MASK,y1=y.low>>64,y2=y.high&MASK; UL hg=((x0*y1)>>64)+((x1*y0)>>64)+(x0*y.high+x1*y1+x.high*y0)+((x1*y2+x2*y1)<<64); UL a=x0*y0,b=(x0*y1)<<64,c=(x1*y0)<<64; UL lw=a;lw+=b;if(lw<b)++hg; lw+=c;if(lw<c)++hg; return ULL(lw,hg); } ULL operator/(const ULL&x,const ull&y) { UL x0=x.low&MASK,x1=x.low>>64,x2=x.high&MASK,x3=x.high>>64; UL hg=((x3/y)<<64)+(((x3%y)<<64)+x2)/y; UL lw=((((((((x3%y)<<64)+x2)%y)<<64)+x1)/y)<<64)+(((((((((x3%y)<<64)+x2)%y)<<64)+x1)%y)<<64)+x0)/y; return ULL(lw,hg); } ull operator%(const ULL&x,const ull&y) { UL x0=x.low&MASK,x1=x.low>>64,x2=x.high&MASK,x3=x.high>>64; return ull((((((((((x3%y)<<64)+x2)%y)<<64)+x1)%y)<<64)+x0)%y); } ULL operator-(const ULL&x) { return 0-x; } bool operator<(const ULL&x,const ULL&y) { return x.high<y.high||(x.high==y.high&&x.low<y.low); } bool operator==(const ULL&x,const ULL&y) { return x.high==y.high&&x.low==y.low; } bool operator<=(const ULL&x,const ULL&y) { return x.high<y.high||(x.high==y.high&&x.low<=y.low); } bool operator>(const ULL&x,const ULL&y) { return x.high>y.high||(x.high==y.high&&x.low>y.low); } bool operator!=(const ULL&x,const ULL&y) { return x.high!=y.high||x.low!=y.low; } bool operator>=(const ULL&x,const ULL&y) { return x.high>y.high||(x.high==y.high&&x.low>=y.low); } ULL operator&(const ULL&x,const ULL&y) { return ULL(x.low&y.low,x.high&y.high); } ULL operator|(const ULL&x,const ULL&y) { return ULL(x.low|y.low,x.high|y.high); } ULL operator^(const ULL&x,const ULL&y) { return ULL(x.low^y.low,x.high^y.high); } ULL operator~(const ULL&x) { return ULL(~x.low,~x.high); } ULL operator<<(const ULL&x,unsigned y) { if(y<128) return ULL(x.low<<y,(x.low>>(128-y))|(x.high<<y)); return ULL(0,x.low<<(y-128)); } ULL operator>>(const ULL&x,unsigned y) { if(y<128) return ULL((x.low>>y)|(x.high<<(128-y)),x.high>>y); return ULL(x.high>>(y-128),0); } ULL&operator+=(ULL&x,const ULL&y) { return x=x+y; } ULL&operator-=(ULL&x,const ULL&y) { return x=x-y; } ULL&operator*=(ULL&x,const ULL&y) { return x=x*y; } ULL&operator/=(ULL&x,const ull&y) { return x=x/y; } ULL&operator&=(ULL&x,const ULL&y) { return x=x&y; } ULL&operator|=(ULL&x,const ULL&y) { return x=x|y; } ULL&operator^=(ULL&x,const ULL&y) { return x=x^y; } ULL&operator<<=(ULL&x,unsigned y) { return x=x<<y; } ULL&operator>>=(ULL&x,unsigned y) { return x=x>>y; } void write(const ULL&x) { if(x>9) write(x/10); putchar(48|int(x%10)); } void writeln(const ULL&x) { write(x);putchar('\n'); } void write(const __int128&x) { if(x>9) write(x/10); putchar(48|int(x%10)); } void writeln(const __int128&x) { write(x);putchar('\n'); } ULL inv(const ULL&x) { ULL y=x; y=y*(2-x*y); y=y*(2-x*y); y=y*(2-x*y); y=y*(2-x*y); y=y*(2-x*y); y=y*(2-x*y); y=y*(2-x*y); return y; } const ULL aaa=ULL(1)<<254,bbb=ULL(2)<<254,ccc=ULL(3)<<254; void resign(ULL&x) { if(x>=aaa&&x<bbb) x+=bbb; else if(x>=bbb&&x<ccc) x-=bbb; } mt19937 myrand(time_related_rand()); const int B=32; int n,m,q; const ULL INF=ULL(0,UL(1)<<88),MSK=INF-1;// INF = 2^216 int dif[105][105];// [a = >] - [b = >] bool ok[105][105];// false if . true if >v ULL f[105][105],g[105][105];// f/INF x + g/INF ULL fr[105][105],gr[105][105];// f/INF x + g/INF ULL fd[105][105],gd[105][105];// f/INF x + g/INF ULL mx;// max period need char s[105][105],t[105][105]; ULL sf[B+1],sg[B+1]; bool ind[B+1][105][105],inr[B+1][105][105]; ULL coef[B+1][105][105],value[B+1][105][105]; ULL qaq[B+1],rst[B+1],mod[B+1];int bt[B+1]; ULL mod0,inv0; void eval(int id) { for(int i=n;i>=1;--i) for(int j=m;j>=1;--j) { if(!ok[i][j]) { coef[id][i][j]=value[id][i][j]=0; } else { coef[id][i][j]= (value[id][i][j+1]+(inr[id][i][j]?INF:0)-value[id][i+1][j]-(ind[id][i][j]?INF:0))>>1; resign(coef[id][i][j]); value[id][i][j]= (value[id][i][j+1]+(inr[id][i][j]?INF:0)+value[id][i+1][j]+(ind[id][i][j]?INF:0))>>1; } if(inr[id][i][j]) { sf[id]+=fr[i][j];sg[id]+=gr[i][j]; } if(ind[id][i][j]) { sf[id]+=fd[i][j];sg[id]+=gd[i][j]; } } if((sf[id]&(MSK))==0) { qaq[id]=INF;mod[id]=1;bt[id]=216;rst[id]=0; return ; } qaq[id]=(sf[id]&-sf[id]); rep(i,256) if(qaq[id]==ULL(1)<<i) bt[id]=i; mod[id]=ULL(1)<<(216-bt[id]); rst[id]=(sf[id]>>bt[id])&(mod[id]-1); } int qx,qy,type;ULL res=INF<<1; void getresult() { rep(i,B+1) { if((sg[i]&(((ULL(1)<<bt[i])-1)))!=0) { cout<<"-1\n"; return ; } } ULL answer=(((-sg[0])>>bt[0])*inv0)&(mod0-1); rep1(i,B) { if(((rst[i]*answer+(sg[i]>>bt[i]))&(mod[i]-1))!=0) { cout<<"-1\n"; return ; } } answer=min(answer,mod0-answer); writeln(answer); } int main() { scanf("%d%d%d",&n,&m,&q); rep1(i,n) { scanf("%s",s[i]+1); rep1(j,m) { if(s[i][j]=='.') ok[i][j]=false; else { ok[i][j]=true; if(s[i][j]=='>') ++dif[i][j]; } } } rep1(i,n) { scanf("%s",t[i]+1); rep1(j,m) { if(t[i][j]=='>') --dif[i][j]; } } qx=1;qy=1;type=1; rep1(i,n) rep1(j,m) { if(i==1&&j==1) { f[i][j]=INF;g[i][j]=0; } else if(i==1) { f[i][j]=fr[i][j-1];g[i][j]=gr[i][j-1]; } else if(j==1) { f[i][j]=fd[i-1][j];g[i][j]=gd[i-1][j]; } else { f[i][j]=fr[i][j-1]+fd[i-1][j];g[i][j]=gr[i][j-1]+gd[i-1][j]; } if(!ok[i][j]) { fr[i][j]=0;gr[i][j]=0; fd[i][j]=0;gd[i][j]=0; } else { fr[i][j]=fd[i][j]=f[i][j]>>1; if(dif[i][j]==1) { gr[i][j]=(g[i][j]+INF)>>1;gd[i][j]=(g[i][j]-INF)>>1; } else if(dif[i][j]==-1) { gr[i][j]=(g[i][j]-INF)>>1;gd[i][j]=(g[i][j]+INF)>>1; } else { gr[i][j]=gd[i][j]=g[i][j]>>1; } resign(gr[i][j]); resign(gd[i][j]); } if(fr[i][j]!=0&&(fr[i][j]&-fr[i][j])<res) { qx=i;qy=j;type=1; res=(fr[i][j]&-fr[i][j]); } if(fd[i][j]!=0&&(fd[i][j]&-fd[i][j])<res) { qx=i;qy=j;type=2; res=(fd[i][j]&-fd[i][j]); } } if(type==1) inr[0][qx][qy]=true; else ind[0][qx][qy]=true; rep1(k,B) { rep1(i,n) rep1(j,m) { int x=int(myrand()%4); if(x&2) inr[k][i][j]=true; if(x&1) ind[k][i][j]=true; } } rep(i,B+1) eval(i); mod0=mod[0];inv0=inv(rst[0]); getresult(); while(q--) { cin>>type>>qx>>qy; if(type==1) { if(s[qx][qy]=='>') { s[qx][qy]='v'; rep(i,B+1) sg[i]-=coef[i][qx][qy]; } else { s[qx][qy]='>'; rep(i,B+1) sg[i]+=coef[i][qx][qy]; } } else { if(t[qx][qy]=='>') { t[qx][qy]='v'; rep(i,B+1) sg[i]+=coef[i][qx][qy]; } else { t[qx][qy]='>'; rep(i,B+1) sg[i]-=coef[i][qx][qy]; } } getresult(); } return 0; } ```