QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#318851 | #7607. The Doubling Game 2 | karuna | WA | 0ms | 74740kb | C++20 | 2.8kb | 2024-02-01 00:42:48 | 2024-02-01 00:42:48 |
Judging History
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[2][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];
for (int i = 0; i < r; i++) {
prod[0][1 << i] = dp[i == r - 1][i][x];
prod[1][1 << i] = dp[0][i][x];
}
// dp transition
for (int i = 1; i < sz; i++) {
int x = g[v][i];
for (int msk = (1 << r) - 1; msk >= 0; msk--) {
for (int j = 0; j < r; 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];
}
}
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 - 1; i++) {
dp[1][i][v] += prod[0][(1 << r) - (1 << i) - 1];
}
// initialize
for (int i = 0; i < (1 << r); i++) {
prod[0][i] = prod[1][i] = 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';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 74200kb
input:
5 1 2 1 3 1 4 4 5
output:
21
result:
ok single line: '21'
Test #2:
score: 0
Accepted
time: 0ms
memory: 71676kb
input:
1
output:
1
result:
ok single line: '1'
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 74740kb
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:
79651834
result:
wrong answer 1st lines differ - expected: '508800953', found: '79651834'