QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640139 | #2918. Tree Number Generator | enze114514 | WA | 2ms | 9796kb | C++20 | 2.1kb | 2024-10-14 07:28:46 | 2024-10-14 07:28:47 |
Judging History
answer
#include <iostream>
#include <vector>
#include <cmath>
#include <thread>
using namespace std;
const int MAXN = 200005;
const int LOGN = 20;
int n, m, q;
vector<int> edges[MAXN];
int digits[MAXN];
int up[MAXN][LOGN];
int depth[MAXN];
int len_u[MAXN];
int f[MAXN];
int pow10[MAXN];
void dfs(int u, int p) {
up[u][0] = p;
for (int k = 1; k < LOGN; ++k) {
up[u][k] = up[up[u][k - 1]][k - 1];
}
if (p == 0) {
f[u] = digits[u] % m;
len_u[u] = 1;
depth[u] = 0;
} else {
f[u] = (1LL * f[p] * 10 + digits[u]) % m;
len_u[u] = len_u[p] + 1;
depth[u] = depth[p] + 1;
}
for (int v : edges[u]) {
if (v != p) {
dfs(v, u);
}
}
}
int lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
for (int k = LOGN - 1; k >= 0; --k) {
if (depth[u] - (1 << k) >= depth[v]) {
u = up[u][k];
}
}
if (u == v) return u;
for (int k = LOGN - 1; k >= 0; --k) {
if (up[u][k] != up[v][k]) {
u = up[u][k];
v = up[v][k];
}
}
return up[u][0];
}
void solve() {
cin >> n >> m >> q;
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
edges[x].emplace_back(y);
edges[y].emplace_back(x);
}
for (int i = 1; i <= n; ++i) {
char c;
cin >> c;
digits[i] = c - '0';
}
pow10[0] = 1 % m;
for (int i = 1; i <= n; ++i) {
pow10[i] = (1LL * pow10[i - 1] * 10) % m;
}
dfs(1, 0);
while (q--) {
int a, b;
cin >> a >> b;
int L = lca(a, b);
int len1 = len_u[a];
int len2 = len_u[b] - len_u[L];
int num1 = f[a];
int num2 = (f[b] - (1LL * f[L] * pow10[len_u[b] - len_u[L]]) % m + m) % m;
int result = (1LL * num1 * pow10[len2] + num2) % m;
cout << result << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9736kb
input:
2 1 4 1 2 1 3 1 2 2 1 1 1 2 2
output:
0 0 0 0
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 9796kb
input:
3 10 5 1 2 2 3 0 0 0 1 3 3 1 2 2 2 3 2 1
output:
0 0 0 0 0
result:
ok 5 lines
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 7896kb
input:
2000 2 2000 937 1471 1672 937 356 1672 976 356 1257 356 1503 1257 783 1503 1783 937 1284 976 1955 1503 802 1257 583 1471 526 356 701 1783 393 1955 307 1955 386 1955 1070 937 1724 802 1397 1724 1140 937 422 526 1941 1955 1638 937 1469 526 1800 526 1035 1800 1009 1140 1195 1800 142 1471 1225 1469 1524...
output:
0 1 1 1 0 0 1 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 0 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 0 0 1 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 1 1 1 1 0 0 1 1 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 0 1 0 0 1 1 1 0 1 1 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 0 0 1 1 0 1 1 0 1 1 0 1 1 0 0 0 1 1 0 0 1 ...
result:
wrong answer 181st lines differ - expected: '1', found: '0'