给一棵树,对于每一个点,求出离每个点距离不超过K的点的个数. 1≤N≤500000,1≤K≤10 树分治太慢了,直接统计多好 #includecstdio #includecstring #includecstdlib #includealgorithm #includefunctional #incl
给一棵树,对于每一个点,求出离每个点距离不超过K的点的个数.
1≤N≤500000,1≤K≤10
树分治太慢了,直接统计多好
#include<cstdio>#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
typedef long long ll;
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
#define
int Pre[MAXN],Next[MAXN*2],edge[MAXN*2],siz=1;
void addedge(int u,int v){
edge[++siz]=v;
Next[siz]=Pre[u];
Pre[u]=siz;
}
void addedge2(int u,int v) {addedge(u,v),addedge(v,u);}
ll c[MAXN];
int f[MAXN];
int n,K,A,B;
int g[MAXN][MAXK];
void solve(int root)
{
Rep(i,K+1) g[root][i]=1;
Forp(root) {
int v=edge[p];
solve(v);
For(i,K) g[root][i] += g[v][i-1];
}
}
void calc(int root,int k2)
{
c[root]=g[root][k2];
int p=root;
while ((p^1) && (k2--) ) {
c[root]+=g[f[p]][k2];
if (k2) c[root]-=g[p][k2-1];
p=f[p];
}
}
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
while(T--) {
n=read(),K=read(),A=read(),B=read();
For(i,n) Pre[i]=c[i]=0; siz=1;
f[1]=0;
Fork(i,2,n) {
f[i]=((ll)A*(ll)i+B)%(i-1)+1;
addedge(f[i],i);
}
solve(1);
For(i,n) calc(i,K);
ll t=0;
For(i,n) t^=c[i];
cout<<t<<endl;
}
return 0;
}