QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#548301 | #8237. Sugar Sweet II | enar | WA | 126ms | 3756kb | C++20 | 1.5kb | 2024-09-05 16:57:51 | 2024-09-05 16:57:52 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 P = 1E9 + 7;
i64 qpow(i64 a, i64 b) {
i64 res = 1;
while(b) {
if(b & 1) {
res = res * a % P;
}
a = a * a % P;
b >>= 1;
}
return res;
}
void solve() {
int n;
std::cin >> n;
std::vector<i64> a(n + 1), b(n + 1), w(n + 1);
for(int i = 1; i <= n; ++i) {
std::cin >> a[i];
}
for(int i = 1; i <= n; ++i) {
std::cin >> b[i];
}
for(int i = 1; i <= n; ++i) {
std::cin >> w[i];
}
std::vector<i64> ans(n + 1), d(n + 1), fac(n + 1, 1);
std::vector<std::vector<int>> v(n + 1);
for(int i = 1; i <= n; ++i) {
fac[i] = fac[i - 1] * i % P;
if(a[b[i]] + w[b[i]] <= a[i] || b[i] == i) {
ans[i] = a[i];
} else if(a[b[i]] > a[i]) {
ans[i] = (a[i] + w[i]) % P;
} else {
v[b[i]].push_back(i);
d[i]++;
}
}
auto dfs = [&](auto self, int id, int dep) ->void {
ans[id] = ((a[id] + w[id]) + a[id] * (fac[dep] - 1)) % P * qpow(fac[dep], P - 2) % P;
for(auto nxt : v[id]) {
self(self, nxt, dep + 1);
}
};
for(int i = 1; i <= n; ++i) {
if(v[i].size() && d[i] == 0) {
for(auto nxt : v[i]) {
if(ans[i] > a[nxt]) {
dfs(dfs, nxt, 2);
}
}
}
}
for(int i = 1; i <= n; ++i) {
ans[i] = (ans[i] == 0 ? a[i] : ans[i]);
std::cout << ans[i] % P << " \n"[i == n];
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T;
std::cin >> T;
while(T--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
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: 126ms
memory: 3756kb
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 590444253 906393935 859123744 863886789 871186919 814243920 968784984 206455474 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959 665922935 577926775 168068923 421298438 601054667 99438820 30...
result:
wrong answer 27th numbers differ - expected: '132646723', found: '168068923'