QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#618046#8237. Sugar Sweet IIukukWA 122ms19440kbC++201.8kb2024-10-06 18:18:312024-10-06 18:18:32

Judging History

你现在查看的是最新测评结果

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-06 18:18:32]
  • 评测
  • 测评结果:WA
  • 用时:122ms
  • 内存:19440kb
  • [2024-10-06 18:18:31]
  • 提交

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 + 5;

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;
		return;
	}
	if(a[fa[u]]+w[fa[u]]<=a[u]){
		cnt[u]=-2;
		return;
	}
	if(vis[fa[u]]){
		cnt[u]=-2;
		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]=vis[i]=0;
	
	for(int i=1;i<=n;i++){
		if(!cnt[i])dfs(i);
//		debug(cnt[i]);
	}
	
	for(int i=1;i<=n;i++){
		if(cnt[i]==-2){
			cout<<a[i]<<' ';
		}
		else if(cnt[i]==-1){
			cout<<(a[i]+w[i])%mod<<' ';
		}
		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(){
	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

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 19176kb

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: 122ms
memory: 19440kb

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 892620793 578579275 859123744 
863886789 
871186919 814243920 968784984 206455474 694033700 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 512133948 963066959 
665922935 577926775 132646723 421298438 601054667 9943882...

result:

wrong answer 3rd numbers differ - expected: '590444253', found: '892620793'