标签:
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 354 Accepted Submission(s): 100
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 500005; typedef long long ll; ll dp1[N][12], dp2[N][12]; int head[N], tot; int n, k, A, B; struct Edge{ int u, v, next; Edge() {} Edge(int u, int v, int next) : u(u), v(v), next(next) {} }e[N]; void init() { memset(head, -1, sizeof head); memset(dp1, 0, sizeof dp1); memset(dp2, 0, sizeof dp2); tot = 0; } void addegde(int u, int v) { e[tot] = Edge(u, v, head[u]); head[u] = tot++; } void dfs(int u) { dp1[u][0] = 1; for(int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; dfs(v); for(int j = 1; j <= k; ++j) dp1[u][j] += dp1[v][j - 1]; } } void dfs2(int u) { for(int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; dp2[v][0] = 0; dp2[v][1] = 1; for(int j = 2; j <= k; ++j) dp2[v][j] = dp1[u][j - 1] - dp1[v][j - 2] + dp2[u][j - 1]; dfs2(v); } } ll solve() { dfs(1); for(int i = 1; i <= n; ++i) for(int j = 1; j <= k; ++j) dp1[i][j] += dp1[i][j - 1]; dfs2(1); ll ans = 0; for(int i = 1; i <= n; ++i) ans ^= (dp1[i][k] + dp2[i][k]); return ans; } int main() { int _; scanf("%d", &_); while(_ --) { scanf("%d%d%d%d", &n, &k, &A, &B); init(); for(int i = 2; i <= n; ++i) { int f = (int)((1ll * A * i + B) % (i - 1) + 1); addegde(f, i); } printf("%lld\n", solve()); } return 0; }
Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/5026818.html