QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#624288 | #8237. Sugar Sweet II | NULL_SF | WA | 168ms | 22032kb | C++23 | 1.9kb | 2024-10-09 15:26:58 | 2024-10-09 15:26:58 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
#define int long long
using namespace std;
const int mod=1e9+7;
int a[500001],b[500001],w[500001],c[500001],root[500001],depth[500001],vis[500001],pre_inv[500001],root_num;
vector<int> son[500001];
int quick_pow(int base,int index)
{
int ans=1,tmp=base;
while(index){
if(index&1){
ans*=tmp; ans%=mod;
}
tmp*=tmp; tmp%=mod;
index>>=1;
}
return ans;
}
int get_inv(int x)
{
return quick_pow(x,mod-2);
}
void dfs(int u,int d)
{
depth[u]=d;
for(auto v:son[u])
{
dfs(v,d+1);
}
return;
}
void solve()
{
int n;
cin>>n;
root_num=0;
for(int i=1;i<=n;i++){
cin>>a[i];
vector<int>().swap(son[i]);
depth[i]=c[i]=root[i]=vis[i]=0;
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
for(int i=1;i<=n;i++){
cin>>w[i];
}
for(int i=1;i<=n;i++)
{
for(int now=i;!vis[now];now=b[now])
{
vis[now]=1;
if(a[now]<a[b[now]])
{
c[now]=a[now]+w[now]%mod;
root[++root_num]=now;
break;
}
else if(a[now]>=a[b[now]]+w[b[now]])
{
c[now]=a[now];
root[++root_num]=now;
break;
}
c[now]=a[now]+w[now]%mod;
son[b[now]].push_back(now);
if(b[now]==i)
{
now=i;
while(1)
{
c[now]=a[now];
son[now].pop_back();
root[++root_num]=now;
now=b[now];
if(now==i) break;
}
break;
}
}
}
for(int i=1;i<=root_num;i++)
{
dfs(root[i],1);
}
for(int i=1;i<=n;i++)
{
cout<<(pre_inv[depth[i]]*c[i]+(1-pre_inv[depth[i]]+mod)*a[i])%mod<<' ';
}
cout<<'\n';
return;
}
signed main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
pre_inv[1]=1;
for(int i=2;i<=500000;i++){
pre_inv[i]=pre_inv[i-1]*get_inv(i); pre_inv[i]%=mod;
}
int 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: 47ms
memory: 22028kb
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: 168ms
memory: 22032kb
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 665922935 577926775 132646723 421298438 601054667 99438820...
result:
wrong answer 53rd numbers differ - expected: '909065903', found: '791253499'