QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546319 | #8237. Sugar Sweet II | Chugg | WA | 167ms | 3712kb | C++20 | 2.3kb | 2024-09-03 23:14:01 | 2024-09-03 23:14:02 |
Judging History
answer
#include <bits/stdc++.h>
const int64_t MOD = 1E9 + 7;
int64_t power(int64_t a, int64_t b, int64_t p)
{
int64_t res = 1;
while (b > 0) {
if (b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
int64_t inv(int64_t a, int64_t p)
{
return power(a, p - 2, p);
}
void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n), b(n), w(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
for (int i = 0; i < n; ++i) {
std::cin >> b[i];
b[i]--;
}
for (int i = 0; i < n; ++i) {
std::cin >> w[i];
}
std::vector<std::vector<int>> adj(n);
std::vector<int64_t> ans(n, -1);
for (int i = 0; i < n; ++i) {
adj[b[i]].push_back(i);
if (a[i] < a[b[i]]) {
ans[i] = (a[i] + w[i]) % MOD;
} else if (a[i] >= (a[b[i]] + w[b[i]]) % MOD) {
ans[i] = a[i];
} else if (i == b[i]) {
ans[i] = a[i];
}
}
std::vector<bool> vis(n);
auto fix = [&](auto self, int u) -> void {
vis[u] = true;
for (const auto &v : adj[u]) {
if (ans[v] != -1) continue;
if (a[v] >= a[u]) {
ans[v] = a[v];
self(self, v);
} else if (a[v] < a[u]) {
ans[v] = (a[v] + w[v]) % MOD;
}
}
};
for (int i = 0; i < n; ++i) {
if (!vis[i] && ans[i] == a[i] && i != b[i]) fix(fix, i);
}
auto cal = [&](auto self, int u, int len, int64_t p) -> void {
if (ans[u] == -1) {
ans[u] = p * ((a[u] + w[u]) % MOD) % MOD + (1 - p + MOD) % MOD * a[u] % MOD;
}
int64_t np = p * inv(len + 1, MOD) % MOD;
for (const auto &v : adj[u]) {
if (ans[v] != -1) continue;
self(self, v, len + 1, np);
}
};
for (int i = 0; i < n; ++i) {
if (ans[i] == (a[i] + w[i]) % MOD) {
cal(cal, i, 1, 1);
}
}
for (int i = 0; i < n; ++i) {
std::cout << ans[i] << " \n"[i == n - 1];
}
}
int main()
{
std::cin.tie(nullptr)->std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while(t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
input:
4 4 2 5 5 2 4 2 1 3 3 2 1 4 3 5 4 3 1 1 1 6 6 6 3 5 4 3 2 3 1 1 2 3 5 2 1 3 2 1 5 1 1 3 4 1 3 4 2 4
output:
500000007 5 5 6 5 10 9 166666673 5 6 500000006 4 3 4 5
result:
ok 15 numbers
Test #2:
score: -100
Wrong Answer
time: 167ms
memory: 3712kb
input:
50000 5 508432375 168140163 892620793 578579275 251380640 3 4 4 1 3 346232959 736203130 186940774 655629320 607743104 1 863886789 1 364158084 18 864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...
output:
854665334 904343293 892620793 906393935 859123744 863886789 871186919 814243920 968784984 206455474 1017527057 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959 665922935 577926775 168068923 421298438 601054667 99438820 ...
result:
wrong answer 3rd numbers differ - expected: '590444253', found: '892620793'