题解 (by;zjvarphi)
直接暴力 dfs,如果一个点被扫过了,那么它的子树也一定被扫过了。
因为每个点最多只会被扫一次,复杂度 (mathcal{O m(n+m)}),需要卡常。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
static const int N=5e6+7;
struct edge{int v,nxt;}e[N];
int first[N],t=1,fa=1,n,m,a,b,q,x,y,ans,lft;
bool vis[N];
auto add=[](int u,int v) {e[t]={v,first[u]},first[u]=t++;};
func(void(int)) dfs=[](int x) {
--lft;
vis[x]=true;
for (ri i(first[x]),v;i;i=e[i].nxt) {
if (vis[v=e[i].v]) continue;
dfs(v);
}
};
inline int main() {
FI=freopen("tree.in","r",stdin);
FO=freopen("tree.out","w",stdout);
cin >> n >> m >> a >> b >> q >> x >> y;
add(fa,2);
for (ri i(3);i<=n;pd(i)) fa=((1ll*fa*a+b)^19760817ll)%(i-1)+1,add(fa,i);
lft=n;
dfs(q);
if (!lft) return printf("%d
",ans),0;
ans^=lft;
for (ri i(2);i<=m;pd(i)) {
q=(((1ll*q*x+y)^19760817ll)^(i<<1))%(n-1)+2;
if (!vis[q]) {
dfs(q);
if (!lft) return printf("%d
",ans),0;
ans^=lft;
} else ans^=lft;
}
printf("%d
",ans);
return 0;
}
}
int main() {return nanfeng::main();}