QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#617988 | #8237. Sugar Sweet II | ukuk | WA | 340ms | 19484kb | C++20 | 1.9kb | 2024-10-06 18:00:22 | 2024-10-06 18:00:22 |
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 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);
fac[0]=1;
for(int i=1;i<N;i++)fac[i]=fac[i-1]*i%mod;
infac[N-1]=qmi(fac[N-1],mod-2);
for(int i=N-2;i>=0;i--)infac[i]=infac[i+1]*(i+1)%mod;
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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 19484kb
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: 340ms
memory: 18552kb
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'