QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#637936 | #8237. Sugar Sweet II | MIS_T__ | WA | 145ms | 3884kb | C++23 | 1.9kb | 2024-10-13 14:25:39 | 2024-10-13 14:25:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 mod = 1000000007;
i64 qpow(i64 x, i64 p) {
i64 ans = 1;
while ( p ) {
if ( p&1 ) ans *= x;
p >>= 1;
x *= x;
x %= mod;
ans %= mod;
}
return ans%mod;
}
i64 inv(i64 x) {
return qpow(x,mod-2);
}
void solve() {
int n;
cin >> n;
vector<int>a(n),b(n),w(n),num(n),vis(n);
for ( auto &i : a ) cin >> i;
for ( auto &i : b ) {
cin >> i;
i--;
}
for ( auto &i : w ) cin >> i;
vector<pair<i64,i64>>p(n);
vector g(n,vector<int>(0));
for ( int i = 0 ; i < n ; i++ ) {
if( b[i] != i ) {
if ( a[i] < a[b[i]] ) {
vis[i] = 1;
} else if ( a[i] < a[b[i]]+w[b[i]] ){
g[b[i]].emplace_back(i);
num[i]++;
}
}
}
queue<pair<int,int>>q;
for ( int i = 0 ; i < n ; i++ ) {
if ( num[i] == 0 ) {
q.emplace(i,1);
if(!vis[i]) p[i] = {1,0};
else p[i] = {0,1};
}
}
vector<int>sum(n+1);
sum[0] = 1;
for ( int i = 1 ; i <= n ; i++ ) {
sum[i] = (i+1)*(sum[i-1]);
sum[i] %= mod;
}
while ( q.size() ) {
auto [u,cnt] = q.front();
q.pop();
vis[u] = 1;
for ( auto v : g[u] ) {
num[v]--;
if ( a[u] >= a[v] ) {
p[v].first += p[u].first;
}
p[v].first %= mod;
p[v].second %= mod;
if ( b[u] != u && a[u]+w[u] > a[v] ) {
i64 t = inv(sum[cnt]);
p[v].second += t%mod;
p[v].first += t%mod*(sum[cnt]-1)%mod;
} else {
p[v].first += p[u].second%mod;
}
p[v].first %= mod;
p[v].second %= mod;
if ( num[v] == 0 ) q.emplace(v,cnt+1);
}
}
for ( int i = 0 ; i < n ; i++ ) {
if ( !vis[i] ) cout << a[i] << " \n"[i == n-1];
else cout << (a[i]*p[i].first%mod+(a[i]+w[i])*p[i].second%mod)%mod << " \n"[i == n-1];
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while ( T-- ) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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: 145ms
memory: 3884kb
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 0 0 132646723 421298438 601054667 99438820 94575413 819542612...
result:
wrong answer 25th numbers differ - expected: '665922935', found: '0'