QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#678728 | #8237. Sugar Sweet II | tsogsummit | WA | 1519ms | 24636kb | C++14 | 2.2kb | 2024-10-26 15:54:45 | 2024-10-26 15:54:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int N = 5e5 + 5;
ll fac[N], inv[N];
bool vis[N];
vector<int> adj[N];
vector<int> order;
int d[N];
ll binpow(ll a, ll b, ll mod){
ll res = 1;
while(b){
if(b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
void dfs(int u, bool is_later){
for(int v: adj[u]){
if(!vis[v]){
vis[v] = true;
if(is_later)d[v] = d[u] + 1;
dfs(v, is_later);
}
}
if(!is_later)order.push_back(u);
}
ll mul(ll a, ll b){
return a * b % mod;
}
ll sub(ll a, ll b){
a -= b;
if(a < 0) a += mod;
return a;
}
ll add(ll a, ll b){
a += b;
return a % mod;
}
void init(int n){
for(int i = 0; i < n; i++){
adj[i].clear();
vis[i] = false;
}
order.clear();
}
void solve(){
int n;
cin >> n;
vector<int> a(n), b(n), w(n), ans(n);
for(auto &x: a)cin>>x;
for(int i = 0; i < n; i++){
cin >> b[i]; b[i]--;
}
for(auto &x: w)cin>>x;
for(int i = 0; i < n; i++){
if(a[i] >= a[b[i]] + w[b[i]] || i == b[i]){
ans[i] = a[i];
continue;
}
if(a[i] < a[b[i]]){
ans[i] = a[i] + w[i];
continue;
}
adj[b[i]].push_back(i);
}
for(int i = 0; i < n; i++){
if(!vis[i]){
vis[i] = true;
dfs(i, 0);
}
}
reverse(order.begin(), order.end());
for(int i = 0; i < n; i++)vis[i] = false;
for(auto &x: order){
if(!vis[x]){
vis[x] = true;
dfs(x, 1);
}
}
for(int i = 0; i < n; i++){
if(!ans[i]){
ans[i] = add(mul(inv[d[i] + 1], a[i] + w[i]), mul(sub(1, inv[d[i] + 1]), a[i]));
}
}
for(int i = 0; i < n; i++){
cout << ans[i] << " \n"[i == n - 1];
}
}
int main() {
int t;
cin >> t;
fac[0] = 1;
for(int i = 1; i < N; i++){
fac[i] = (fac[i - 1] * i) % mod;
inv[i] = binpow(fac[i], mod - 2, mod);
}
while(t--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 53ms
memory: 24636kb
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: 1519ms
memory: 24204kb
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 341020407 1449261420 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 1113628633 977360756 482247153 963066959 78043298 132058958 156261523 421298438 601054667 10994388...
result:
wrong answer 10th numbers differ - expected: '206455474', found: '1206455481'