QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#696607 | #8237. Sugar Sweet II | Catreap | WA | 0ms | 3852kb | C++23 | 2.4kb | 2024-10-31 23:47:01 | 2024-10-31 23:47:03 |
Judging History
answer
#include <bits/stdc++.h>
using ll = long long;
const ll M = 1e9 + 7;
ll qpow(ll a, ll b) {
ll ret = 1;
while (b) {
if (b & 1) {
ret = (ret * a) % M;
}
a = (a * a) % M;
b >>= 1;
}
return ret;
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
ll T;
std::cin >> T;
ll inv = qpow(2, M - 2);
while (T--) {
ll n;
std::cin >> n;
std::vector<ll> a(n + 1);
std::vector<ll> b(n + 1);
std::vector<ll> w(n + 1);
std::vector<bool> vis(n + 1);
std::vector<ll> dp(n + 1), p(n + 1);
std::vector<ll> stk;
ll top = 0;
for (ll i = 1; i <= n; i++) {
std::cin >> a[i];
}
for (ll i = 1; i <= n; i++) {
std::cin >> b[i];
}
for (ll i = 1; i <= n; i++) {
std::cin >> w[i];
}
auto work = [&](auto&& self, ll i) -> void {
if (vis[i]) {
while (top--) {
dp[stk[top]] = a[stk[top]];
p[stk[top]] = 0;
}
return;
}
vis[i] = true;
if (a[i] < a[b[i]]) {
p[i] = 1;
dp[i] = a[i] + w[i];
} else if (a[i] >= a[b[i]] + w[b[i]]) {
p[i] = 0;
dp[i] = a[i];
} else {
self(self, b[i]);
p[i] = ((p[b[i]] * (a[b[i]] + w[b[i]] > a[i] ? 1 : 0) +
((1 - p[b[i]] + M) % M * (a[b[i]] > a[i] ? 1 : 0)))) %
M;
dp[i] =
a[i] +
inv *
(p[b[i]] * (a[b[i]] + w[b[i]] > a[i] ? w[i] : 0) +
((1 - p[b[i]] + M) % M * (a[b[i]] > a[i] ? w[i] : 0)));
}
};
for (ll i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
if (b[i] == i) {
vis[i] = true;
dp[i] = a[i];
p[i] = 0;
continue;
}
work(work, i);
}
for (ll i = 1; i <= n; i++) {
printf(" %lld" + !(i - 1), dp[i]);
}
printf("\n");
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3852kb
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:
1500000014 5 5 6 5 10 9 500000009 1000000012 6 500000006 4 3 4 5
result:
wrong answer 1st numbers differ - expected: '500000007', found: '1500000014'