QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695455#8237. Sugar Sweet IIshenchuan_fanWA 177ms11500kbC++202.0kb2024-10-31 20:04:472024-10-31 20:04:48

Judging History

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

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

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long

//  The 2023 ICPC Asia Hangzhou Regional Contest

constexpr int mod = 1E9 + 7;
const int N = 500005;

int qmi(int a, int b){
    int res = 1;
    while(b){
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int fact[N], ifact[N];

void init(int n){
    fact[0] = 1, fact[1] = 1, ifact[1] = 1;
    for(int i = 2;i<=n;i++){
        fact[i] = fact[i - 1] * i % mod;
        ifact[i] = ifact[i - 1] * qmi(i, mod - 2) % mod;
    }
}

void sol(){
    int n; std::cin>>n;
    
    std::vector<int>a(n + 1), b(n + 1), w(n + 1);
    std::vector<int>du(n + 1), ans(n + 1), vis(n + 1);
    std::vector G(n + 1, std::vector<int>(0));

    for(int i = 1;i<=n;i++) std::cin>>a[i];
    for(int i = 1;i<=n;i++) std::cin>>b[i];
    for(int i = 1;i<=n;i++) std::cin>>w[i];
    
    for(int i = 1;i<=n;i++){
        if(i != b[i]){
            if(a[b[i]] > a[i]){
                ans[i] = a[i] + w[i];
                vis[i] = 1;
            }else if(a[i] >= a[b[i]] + w[b[i]]){
                ans[i] = a[i];
                vis[i] = 1;
            }else{
                du[b[i]]++;
                G[i].push_back(b[i]);
            }
        }else{
            vis[i] = 1;
            ans[i] = a[i];
        }
    }

    auto dfs = [&](auto& self, int u)->int{
        vis[u] = 1;
        if(G[u].size() == 0) return 1;
        int kk = self(self, G[u].back()) + 1;
        ans[u] = (ifact[kk] * ((a[u] + w[u])%mod)%mod+ ((1ll - ifact[kk]) * a[u] +mod)%mod)%mod;
        ans[u] = (ans[u] % mod + mod) % mod;
        return kk;
    };

    for(int u = 1;u<=n;u++){
        if(vis[u] || du[u]) continue ;
        dfs(dfs, u);
    }

    for(int i = 1;i<=n;i++){
        std::cout<<ans[i]<<" \n"[i == n];
    }
}

signed main(){
    ios::sync_with_stdio(0),cin.tie(0), cout.tie(0);
    init(N - 1);
    int t = 1; std::cin>>t;
    while(t--) sol();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 50ms
memory: 11380kb

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: 177ms
memory: 11500kb

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
902284038 240323331 132646723 421298438 601054667 10994388...

result:

wrong answer 10th numbers differ - expected: '206455474', found: '1206455481'