QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#694231 | #8237. Sugar Sweet II | wang_xy# | WA | 174ms | 11468kb | C++20 | 2.1kb | 2024-10-31 17:31:04 | 2024-10-31 17:31:04 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll JC[500005];
ll inv[500005];
bool vis[500005];
ll n;
ll power(ll x,ll y){
ll res=1;
while(y>0){
if(y&1)
res=(res*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return res;
}
void dfs(ll u,vector<ll> &a,vector<ll> &b,vector<ll> &w,vector<ll> &cnt,vector<ll> &num){
ll nex=b[u];
vis[u]=true;
if(a[nex]>a[u]){
cnt[u]=JC[n];
vis[u]=false;
return;
}
else if(a[nex]+w[nex]<=a[u]){
cnt[u]=0;
vis[u]=false;
return;
}
else {
if(vis[nex]){
cnt[u]=0;
num[u]=0;
vis[u]=false;
return;
}
dfs(nex,a,b,w,cnt,num);
num[u]=num[nex]+1;
num[u]%=mod;
ll tol=num[u]+1;
tol%=mod;
cnt[u]=(((JC[n-tol]*inv[n-tol])%mod*JC[n]%mod)*inv[tol]%mod);
}
vis[u]=false;
}
void solve() {
cin >> n;
vector<ll> a(n + 1), b(n + 1), w(n + 1);
ll sum=0;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
sum+=a[i];
sum%=mod;
}
for (ll i = 1; i <= n; i++) {
cin >> b[i];
}
for (ll i = 1; i <= n; i++) {
cin >> w[i];
}
vector<ll> cnt(n + 1, -1);
vector<ll> num(n + 1, 0);
for (ll i = 1; i <= n; i++) {
if (cnt[i] != -1)
continue;
if (a[i] < a[b[i]]) {
cnt[i] = JC[n];
}
else if(a[i]>a[b[i]]+w[b[i]]){
cnt[i]=0;
}
else{
dfs(i,a,b,w,cnt,num);
}
}
for(ll i=1;i<=n;i++){
ll all=(JC[n]-cnt[i]+mod)%mod;
ll ans=(all*a[i]%mod+cnt[i]*((w[i]+a[i])%mod)%mod)*inv[n]%mod;
cout<<ans<<" ";
}
cout<<endl;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
JC[0]=1;
inv[0]=1;
for(ll i=1;i<=500000;i++){
JC[i]=(JC[i-1]*i)%mod;
inv[i]=power(JC[i],mod-2);
}
ll t;
cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 50ms
memory: 11300kb
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: 174ms
memory: 11468kb
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 902284038 240323331 132646723 421298438 601054667 99438820...
result:
wrong answer 25th numbers differ - expected: '665922935', found: '902284038'