HDU 5593 ZYB's Tree

Problem Description

ZYBZYB has a tree with NN nodes,now he wants you to solve the numbers of nodes distanced no more than KK for each node. the distance between two nodes(x,y)(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 AA and BB,let fa_ifai be the father of node ii,fa_1=0fa1=0,fa_i=(A*i+B)\%(i-1)+1fai=(Ai+B)%(i1)+1 for i \in [2,N]i[2,N] .

For printing:let ans_iansi be the answer of node ii,you only need to print the xorxor sumsum of all ans_iansi.

Input

In the first line there is the number of testcases TT.

For each teatcase:

In the first line there are four numbers NN,KK,AA,BB

1 \leq T \leq 51T5,1 \leq N \leq 5000001N500000,1 \leq K \leq 101K10,1 \leq A,B \leq 10000001A,B1000000

Output

For TT lines,each line print the ans.

Please open the stack by yourself.

N \geq 100000N100000 are only for two tests finally.

Sample Input
1
3 1 1 1
Sample Output

3


树形dp

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 5 * 1e5 + 10;
int T, n, K, A, B, fa[maxn];
int dp[maxn][11];

int find(int x)
{
    int ans = 0, sum[11] = {0};
    for (int i = 0, j = x, k = 0; j && i <= K; i++)
    {
        sum[i] += 1;
        for (int u = 1; u <= K - i; u++)
        {
            sum[u + i] += dp[j][u] - dp[k][u - 1];
        }
        k = j;
        j = fa[j];
    }
    for (int i = 0; i <= K; i++) ans += sum[i];
    return ans;
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        memset(dp, 0, sizeof(dp));
        scanf("%d%d%d%d", &n, &K, &A, &B);
        fa[1] = 0;
        for (int i = 2; i <= n; i++)
        {
            fa[i] = ((LL)A*i + B) % (i - 1) + 1;
            for (int j = i, k = 0; j&&k <= K; j = fa[j], k++) dp[j][k]++;
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) ans ^= find(i);
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值