QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#618030 | #8237. Sugar Sweet II | ukuk | WA | 6ms | 15896kb | C++20 | 1.9kb | 2024-10-06 18:13:35 | 2024-10-06 18:13:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define debug(x) cout<<#x<<" = "<<x<<'\n'
const int mod=1e9+7;
const int N = 5e5 + 10;
int n;
int fac[N],infac[N];
bool vis[N];
int a[N],w[N];
int fa[N];
int cnt[N];
int p=mod;
int qmi(int a,int b){
int ret=1;
for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod;
return ret;
}
int C(int a,int b){
if(a<b||a<0||b<0)return 0;
return fac[a]*infac[b]%mod*infac[a-b]%mod;
}
void dfs(int u){
// debug(u);
vis[u]=1;
if(a[fa[u]]>a[u]){
cnt[u]=-1;
vis[u]=0;
return;
}
if(a[fa[u]]+w[fa[u]]<=a[u]){
cnt[u]=-2;
vis[u]=0;
return;
}
if(vis[fa[u]]){
cnt[u]=-2;
vis[u]=0;
return;
}
if(!cnt[fa[u]])dfs(fa[u]);
if(cnt[fa[u]]>0)cnt[u]=cnt[fa[u]]+1;
else if(cnt[fa[u]]==-1)cnt[u]=1;
else cnt[u]=-2;
vis[u]=0;
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>fa[i];
for(int i=1;i<=n;i++)cin>>w[i];
for(int i=1;i<=n;i++)cnt[i]=0;
for(int i=1;i<=n;i++)assert(!vis[i]);
for(int i=1;i<=n;i++){
if(!cnt[i])dfs(i);
// debug(cnt[i]);
}
// cout << cnt[5] << '\n';
for(int i=1;i<=n;i++){
if(cnt[i]==-2){
cout<<a[i]<<' ';
}
else if(cnt[i]==-1){
cout<<a[i]+w[i]<<' ';
}
else{
while(n-cnt[i]-1<0);
int t=w[i]*infac[cnt[i]+1]%mod;
// int t=C(n,cnt[i]+1)*w[i]%mod*fac[n-cnt[i]-1]%mod*infac[n]%mod;
t=((t+mod)%mod+a[i])%mod;
cout<<t<<' ';
}
}
cout<<'\n';
}
signed main(){
// freopen("1.in", "r", stdin);
// freopen("1.ans", "w", stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);
infac[0]=infac[1]=1;
for(int i=2;i<N;++i)infac[i]=infac[p%i]*(-p/i);
for(int i=2;i<N;++i)infac[i]*=infac[i-1];
int _=1;
cin>>_;
while(_--)solve();
return 0;
}
/*
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
1
3
5 4 3
2 3 1
1 2 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 15896kb
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 445487537 5 6 500000006 4 3 4 5
result:
wrong answer 8th numbers differ - expected: '166666673', found: '445487537'