ZYB's Tree
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 126 Accepted Submission(s): 39
Problem Description
ZYB
has a tree with
N
nodes,now he wants you to solve the numbers of nodes distanced no more than
K
for each node.
the distance between two nodes (x,y) is defined the number of edges on their shortest path in the tree.
To save the time of reading and printing,we use the following way:
For reading:we have two numbers A and B ,let fai be the father of node i , fa1=0 , fai=(A∗i+B)%(i−1)+1 for i∈[2,N] .
For printing:let ansi be the answer of node i ,you only need to print the xor sum of all ansi .
the distance between two nodes (x,y) is defined the number of edges on their shortest path in the tree.
To save the time of reading and printing,we use the following way:
For reading:we have two numbers A and B ,let fai be the father of node i , fa1=0 , fai=(A∗i+B)%(i−1)+1 for i∈[2,N] .
For printing:let ansi be the answer of node i ,you only need to print the xor sum of all ansi .
Input
In the first line there is the number of testcases
T
.
For each teatcase:
In the first line there are four numbers N , K , A , B
1≤T≤5 , 1≤N≤500000 , 1≤K≤10 , 1≤A,B≤1000000
For each teatcase:
In the first line there are four numbers N , K , A , B
1≤T≤5 , 1≤N≤500000 , 1≤K≤10 , 1≤A,B≤1000000
Output
For
T
lines,each line print the ans.
Please open the stack by yourself.
N≥100000 are only for two tests finally.
Please open the stack by yourself.
N≥100000 are only for two tests finally.
Sample Input
1 3 1 1 1
Sample Output
3
题意是给出一棵树,树的边权都是1,求出每个点,和它的距离小于k的点的数量,最后求所有的这些结果的异或和.
刚开始写的时候以为是树分治,写了一半卡住了,一看50W的数据量nlgnlgn的复杂度简直自杀,然后发现k只有10=.=
那就很简单了一遍dfs,dp[i][j]表示i为根的子树中距离它为j的有多少个节点,然后往上的距离为j的也很简单,假设i的父亲是u,dp[u][j]-dp[i][j-1]就是不在i为根的子树上的点满足的个数,然后u不断的用u的父亲更新,向上k次或者到根.
复杂度O(n*k).
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 511111
#define maxm 1111111
long long n, a, b;
int k;
long long dp[maxn][15];
struct node {
int u, v, next;
}edge[maxm];
int cnt, head[maxn], fa[maxn];
long long ans[maxn];
void add_edge (int u, int v) {
edge[cnt].u = u, edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;
return ;
}
int d[maxn];
void dfs (int u, int deep) {
d[u] = deep;
dp[u][0] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
dfs (v, deep+1);
for (int i = 1; i <= k; i++) {
dp[u][i] += dp[v][i-1];
}
}
}
long long cal (int u) {
int pre = u;
long long ans = 0;
for (int i = 0; i <= k; i++) {
ans += dp[u][i];
}
int tot = 1;
while (u != 0 && tot <= k) {
int father = fa[u];
if (father == 0)
break;
for (int i = 1; i <= k-tot; i++) {
ans += dp[father][i] - dp[u][i-1];
}
u = father; tot++;
}
ans += d[pre]-d[u];
return ans;
}
void solve () {
memset (d, 0, sizeof d);
memset (dp, 0, sizeof dp);
dfs (1, 1);
memset (ans, 0, sizeof ans);
for (int i = 1; i <= n; i++) {
ans[i] = cal (i);
}
long long num = 0;
for (int i = 1; i <= n; i++)
num^= ans[i];
printf ("%lld\n", num);
}
int main () {
//freopen ("in", "r", stdin);
int t;
scanf ("%d", &t);
while (t--) {
scanf ("%lld%lld%lld%lld", &n, &k, &a, &b);
memset (head, -1, sizeof head);
cnt = 0;
fa[1] = 0;
for (int i = 2; i <= n; i++) {
fa[i] = (a*i+b)%(i-1)+1;
add_edge (fa[i], i);
}
solve ();
}
return 0;
}