QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#318842#7607. The Doubling Game 2karunaWA 3ms82216kbC++204.3kb2024-02-01 00:21:242024-02-01 00:21:24

Judging History

你现在查看的是最新测评结果

  • [2024-02-01 00:21:24]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:82216kb
  • [2024-02-01 00:21:24]
  • 提交

answer

#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 303030;
const int B = 21;

const int MOD = 1e9 + 7;
struct mint {
	int x;
	mint() : x(0) {}
	mint(int x) : x(x) {}
	mint operator+(int ot) const { return x + ot >= MOD ? x + ot - MOD : x + ot; }
	mint operator-(int ot) const { return x - ot < 0 ? x - ot + MOD : x - ot; }
	mint operator*(int ot) const { return 1ll * x * ot % MOD; }
	mint operator+=(int ot) { return *this = *this + ot; }
	mint operator-=(int ot) { return *this = *this - ot; }
	mint operator*=(int ot) { return *this = *this * ot; }
	operator int() { return x; }
};

int n, sub[MAXN];
vector<int> g[MAXN];

mint cnt[MAXN], dp[2][B][MAXN];

mint prod[3][1 << B];
void dfs(int p, int v) {
	sub[v] = 1;
	for (int x : g[v]) if (p != x) {
		dfs(v, x);
		sub[v] += sub[x];
	}
	for (int i = 0; i < g[v].size(); i++) {
		if (g[v][i] == p) {
			swap(g[v][i], g[v].back());
			g[v].pop_back();
			break;
		}
	}
	sort(g[v].begin(), g[v].end(), [&](int i, int j) {
		return sub[i] > sub[j];
	});
	int sz = g[v].size();
	if (sz == 0) {
		cnt[v] = 1;
		dp[0][0][v] = 1;
		dp[1][0][v] = 1;
		return;
	}
	
	for (int r = 0; r <= __lg(sub[v]); r++) {
		// base case
		int x = g[v][0];
		prod[0][0] = cnt[x];
		prod[1][0] = cnt[x];
		prod[2][0] = cnt[x];
		for (int i = 0; i < min(r, 1 + __lg(sub[x])); i++) {
			prod[0][1 << i] = dp[i == r - 1][i][x];
			prod[1][1 << i] = dp[0][i][x];
		}
		for (int i = 0; i <= min(r, __lg(sub[x])); i++) {
			prod[2][1 << i] = dp[i == r][i][x];
		}
		// dp transition
		for (int i = 1; i < sz; i++) {
			int x = g[v][i];
			int y = min(r, 1 + __lg(sub[x]));
			int z = (1 << r) - 1;
			z &= ~((1 << y) - 1); // base mask for case 0, case 1

			for (int msk = z + (1 << y) - 1; msk >= z; msk--) {
				for (int j = 0; j < y; j++) {
					if (~msk >> j & 1) { // can you use 2^j here?
						prod[0][msk ^ (1 << j)] += prod[0][msk] * dp[j == r - 1][j][x];
						prod[1][msk ^ (1 << j)] += prod[1][msk] * dp[0][j][x];
					}
				}
				// choose not to use anything
				prod[0][msk] *= cnt[x];
				prod[1][msk] *= cnt[x];
			}
			int o = min(1 + r, 1 + __lg(sub[x]));
			for (int e = min(r, __lg(sub[x])); e <= r; e++) {
				int w = ((2 << r) - (1 << e) - 1);
				w &= ~((1 << o) - 1);
				for (int msk = w + (1 << o) - 1; msk >= w; msk--) {
					for (int j = 0; j < o; j++) {
						if (~msk >> j & 1) { // can you use 2^j here?
							prod[2][msk ^ (1 << j)] += prod[2][msk] * dp[j == r + 1][j][x];
						}
					}
					// choose not to use anything
					prod[2][msk] *= cnt[x];
				}
			}
		}
		cnt[v] += prod[0][(1 << r) - 1] * (1 + (r != 0));
		dp[0][r][v] = prod[1][(1 << r) - 1];
		for (int i = 0; i < r; i++) {
			dp[1][i][v] += prod[2][(2 << r) - (1 << i) - 1];
		}

		// initialize
		// for (int i = 0; i < 2000; i++) {
		// 	prod[0][i] = prod[1][i] = prod[2][i] = 0;
		// }
		prod[0][0] = 0;
		prod[1][0] = 0;
		prod[2][0] = 0;
		for (int i = 0; i < min(r, __lg(sub[x])); i++) {
			prod[0][1 << i] = 0;
			prod[1][1 << i] = 0;
		}
		for (int i = 0; i < min(r + 2, 1 + __lg(sub[x])); i++) {
			prod[2][1 << i] = 0;
		}
		if (sz >= 2) {
			int x = g[v][1];
			int y = min(r, 1 + __lg(sub[x]));
			int z = (1 << r) - 1;
			z &= ~((1 << y) - 1); // base mask for case 0, case 1

			for (int msk = z + (1 << y) - 1; msk >= z; msk--) {
				prod[0][msk] = 0;
				prod[1][msk] = 0;
			}
			int o = min(1 + r, 1 + __lg(sub[x]));
			for (int e = min(r, __lg(sub[x])); e <= r; e++) {
				int w = ((2 << r) - (1 << e) - 1);
				w &= ~((1 << o) - 1);
				for (int msk = w + (1 << o) - 1; msk >= w; msk--) {
					prod[2][msk] = 0;
				}
			}
		}
	}
	for (int i = 0; i <= __lg(sub[v]); i++) {
		dp[1][i][v] += dp[0][i][v];
	}

	// cout << "vertex " << v << '\n';
	// cout << cnt[v] << '\n';
	// for (int r = 0; r <= __lg(sub[v]); r++) {
	// 	cout << dp[0][r][v] << ' ';
	// }
	// cout << '\n';

	// for (int r = 0; r <= __lg(sub[v]); r++) {
	// 	cout << dp[1][r][v] << ' ';
	// }
	// cout << '\n';
}
int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v;
		cin >> u >> v;

		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(-1, 1);
	cout << cnt[1] << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 82148kb

input:

5
1 2
1 3
1 4
4 5

output:

21

result:

ok single line: '21'

Test #2:

score: 0
Accepted
time: 3ms
memory: 82216kb

input:

1

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 79524kb

input:

128
11 32
116 81
65 4
117 47
5 81
104 30
61 8
82 59
95 20
92 29
29 127
97 39
123 33
59 128
115 33
83 67
74 16
77 33
64 73
124 123
8 127
61 51
101 122
35 90
119 116
112 27
81 93
109 123
54 1
119 100
116 16
65 47
67 27
22 105
76 87
36 39
27 96
72 31
91 123
21 105
118 12
110 48
121 72
14 115
24 16
106 ...

output:

588585672

result:

wrong answer 1st lines differ - expected: '508800953', found: '588585672'