QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#325026 | #8237. Sugar Sweet II | ucup-team2000# | WA | 254ms | 13296kb | C++20 | 2.8kb | 2024-02-11 03:21:17 | 2024-02-11 03:21:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define lep(i,a,b) for(int i=(a); i <= (b); i++)
#define pb push_back
const int maxn = 6e5 + 5;
int fact[maxn], ifact[maxn];
const int mod = 1e9 + 7;
int reduce(int a) {
return (a %= mod) < 0 ? a + mod : a;
}
int add(int x, int y) {
return reduce(x+y);
}
int prod(int x, int y) {
return reduce(x * y);
}
int modpow(int a, int pw) {
if (a == 0) return 0;
if (pw == 0) return 1;
if (pw % 2 == 0) {
int res = modpow(a,pw/2);
return prod(res, res);
}
return prod(a, modpow(a,pw-1));
}
int inv(int x) {
return modpow(x,mod-2);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
fact[0] = ifact[0] = 1;
lep(i,1,maxn-1) {
fact[i] = prod(i, fact[i-1]);
ifact[i] = inv(fact[i]);
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n+1), b(n+1), w(n+1);
vector<int> typ(n+1);
vector<vector<int>> adj(n+1);
vector<int> depth(n+1, -1);
lep(i,1,n) {
cin >> a[i];
}
lep(i,1,n) {
cin >> b[i];
}
lep(i,1,n) {
cin >> w[i];
}
lep(i,1,n) {
if (i == b[i] or a[i] >= a[b[i]] + w[b[i]]) typ[i] = 2;
else if (a[i] < a[b[i]]) typ[i] = 0;
else typ[i] = 1;
}
lep(i,1,n) {
// consider (b[i], i)
int u = b[i];
int v = i;
if (typ[u] <= 1 and typ[v] == 1) adj[u].pb(v);
}
auto dfs = [&](auto &self, int u, int p) -> void {
for (int v: adj[u]) {
if (v != p) {
depth[v] = depth[u] + 1;
self(self, v, u);
}
}
};
lep(i,1,n) {
if (typ[i] == 0) {
depth[i] = 0;
dfs(dfs,i,0);
}
}
vector<int> ans(n+1);
lep(i,1,n) {
// cout << "i: " << i << ", typ[i]: " << typ[i] << ", depth[i]: " << depth[i] << "\n";
if (typ[i] == 0) {
ans[i] = a[i] + w[i];
} else if (typ[i] == 1) {
if (depth[i] != -1) ans[i] = add(a[i], prod(inv(fact[depth[i]+1]), w[i]));
else ans[i] = a[i];
} else {
ans[i] = a[i];
}
}
lep(i,1,n) {
cout << ans[i] << " ";
}
cout << "\n";
}
}
// Output for sample:
// 5 5 5 6
// 5 10 9
// 500000009 6 6
// 3 4 3 4 5
// Third case:
// 1
// 3
// 5 4 3
// 2 3 1
// 1 2 3
// i: 1, typ[i]: 1, depth[i]: 2
// i: 2, typ[i]: 1, depth[i]: 1
// i: 3, typ[i]: 0, depth[i]: 0
// 333333341 5 6
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 107ms
memory: 12900kb
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: 254ms
memory: 13296kb
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 1206455481 17527050 1449261420 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 1113628633 977360756 482247153 963066959 665922935 577926775 132646723 421298438 601054667 10994...
result:
wrong answer 10th numbers differ - expected: '206455474', found: '1206455481'