题意:
问你树上到每个节点的距离小于等于k的点的节点个数的异或和,树形DP
代码:
//
// Created by CQU_CST_WuErli
// Copyright (c) 2015 CQU_CST_WuErli. All rights reserved.
//
// #include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <sstream>
#define CLR(x) memset(x,0,sizeof(x))
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define For_UVa if (kase!=1) cout << endl
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define SI(a) scanf("%d",&a)
#define SII(a,b) scanf("%d%d",&a,&b)
#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)
#define Rep(flag,start,end) for(int flag=start;flag>=end;flag--)
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
#define Root 1,n,1
#define BigInteger bign
#define null NULL
template <typename T> T max(T& a,T& b) {return a>b?a:b;}
template <typename T> T min(T& a,T& b) {return a<b?a:b;}
int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
long long gcd (long long a,long long b) {return b==0LL?a:gcd(b,a%b);}
const int MAX_L=2005;// For BigInteger
const int INF_INT=0x3f3f3f3f;
const long long INF_LL=0x7fffffff;
const int MOD=1e9+7;
const double eps=1e-9;
const double pi=acos(-1);
typedef long long ll;
using namespace std;
const int N=5e5+100;
ll dp[N][11];
vector<int> g[N];
ll A,B;
int n,k;
void DP(int fa,int u) {
for (int i=1;i<=k;i++) dp[u][i]=0;
dp[u][0]=1;
rep(i,0,g[u].size()-1) {
int v=g[u][i];
if (v==fa) continue;
DP(u,v);
for (int i=1;i<=k;i++) dp[u][i]+=dp[v][i-1];
}
}
void dfs(int fa,int u) {
for (int i=0;i<g[u].size();i++) {
int v=g[u][i];
if(v==fa) continue;
for (int j=k;j>=2;j--) dp[v][j]+=dp[u][j-1]-dp[v][j-2];
dp[v][1]++;
dfs(u,v);
}
}
int main(){
#ifdef LOCAL
freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
int t;SI(t);
while (t--) {
SII(n,k);
SII(A,B);
rep(i,0,n) g[i].clear();
rep(i,1,n) {
ll fa;
if (i==1) fa=0;
else fa=(A*i+B)%(i-1)+1;
g[i].push_back(fa);
g[fa].push_back(i);
}
DP(0,1);
dfs(0,1);
ll ans=0;
rep(i,1,n) {
ll tmp=0;
rep(j,0,k) tmp+=dp[i][j];
ans^=tmp;
}
cout << ans << endl;
}
return 0;
}