贪心并不难想,然而恶心的是卡内存,数组要重复利用…
这题我一开始写了快速乘,怒T半天,最后发现用ll就可以了,欲哭无泪…
AC code:
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=25000001;
const int M=10010;
int a,b,c,d,n,m,q,tot;
int x[N],T[N],path[M];
bool h[N];
int mul(int x,int y){
if(!y) return 0;
int z=mul(x,y>>1);
z=(z<<1)%d;
if(y&1) z=(z+x)%d;
return z;
}
int nx(int num){
return (num-1)/m+1;
}
int ny(int num){
int t=num%m;
if(!t) t=m;
return t;
}
int main(){
scanf("%d%d%d%d%d%d%d%d",&x[0],&a,&b,&c,&d,&n,&m,&q);
for(int i=1;i<=n*m;i++) x[i]=((ll)a*x[i-1]*x[i-1]+(ll)b*x[i-1]+c)%d;
for(int i=1;i<=n*m;i++) T[i]=i;
for(int i=1;i<=n*m;i++){int t=T[i];T[i]=T[(x[i]%i)+1];T[(x[i]%i)+1]=t;}
for(int i=1;i<=q;i++){
int u,v,t;
scanf("%d%d",&u,&v);
t=T[u];T[u]=T[v];T[v]=t;
}
for(int i=1;i<=n*m;i++) x[T[i]]=i;
for(int i=1;i<=n*m;i++) h[i]=1;
path[++tot]=T[1];path[++tot]=T[n*m];h[T[1]]=h[T[n*m]]=0;
for(int i=1;i<=n*m&&tot!=n+m-1;i++){
if(!h[i]) continue;
int j=tot++;
while(nx(x[i])<=nx(x[path[j]])&&ny(x[i])<=ny(x[path[j]])) j--;
for(int k=tot;k>j+1;k--) path[k]=path[k-1];
path[j+1]=i;
for(int k=nx(x[path[j+1]])+1;k<=nx(x[path[j+2]]);k++)
for(int l=ny(x[path[j]]);l<=ny(x[path[j+1]])-1;l++) h[T[m*(k-1)+l]]=0;
for(int k=nx(x[path[j]]);k<=nx(x[path[j+1]])-1;k++)
for(int l=ny(x[path[j+1]])+1;l<=ny(x[path[j+2]]);l++) h[T[m*(k-1)+l]]=0;
}
sort(path+1,path+tot+1);
for(int i=1;i<=tot;i++) printf("%d%c",path[i],i==tot?'\n':' ');
return 0;
}