QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#648423#8237. Sugar Sweet IIyixuanoct#WA 383ms7652kbC++201.8kb2024-10-17 18:55:582024-10-17 18:55:58

Judging History

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

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

answer

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;


ll qsm(ll x, ll n) {
    ll res = 1;
    while (n) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

ll inv(ll a) { return qsm(a, mod - 2); }

ll fac[500003];

void solve() {
    ll n;cin >> n;
    vector<ll> a(n + 1), b(n + 1), w(n + 1), ans(n + 1);
    for (ll i = 1; i <= n; i++) cin >> a[i];
    for (ll i = 1; i <= n; i++) cin >> b[i];
    for (ll i = 1; i <= n; i++) cin >> w[i];
    vector<vector<ll>> g(n + 1);
    for (ll i = 1; i <= n; i++) {
        if (a[i] < a[b[i]]) ans[i] = a[i] + w[i];
        else if (a[i] >= a[b[i]] + w[b[i]]) ans[i] = a[i];
        else if (i == b[i]) ans[i] = a[i];
        else {
            g[i].push_back(b[i]);
            //cout << i << " ->" << b[i] << endl;
        }
    }
    vector<ll> sz(n + 1, 1);
    auto dfs = [&](ll u, auto&& dfs) {
        if (g[u].size() == 0) return;
        for (auto i : g[u]) {
            //cout << u << "---" << i << endl;
            dfs(i, dfs);
            sz[u] = sz[i] + 1;
            //ans[u] = 1/sz[u]! * (a[u] + w[u]) + (1 - 1/sz[u]!) * a[u];
            //cout << u << ' ' << sz[u] << ' ' << fac[sz[u]] << endl;  
            //cout << a[u] << endl;
            if (a[i] <= a[u] && a[i] + w[i] > a[u])
                ans[u] = (inv(fac[sz[u]]) * (a[u] + w[u]) % mod + ((fac[sz[u]] - 1 + mod) % mod * a[u]) % mod * inv(fac[sz[u]]) % mod) % mod;
            else ans[u] = a[u];
        }
        };

    for (ll i = 1; i <= n; i++) {
        if (!ans[i]) dfs(i, dfs);
        cout << ans[i] << ' ';
    }
    cout << endl;

}

int main() {
    fac[1] = 1;
    for (ll i = 2; i <= 500000; i++) fac[i] = fac[i - 1] * i % mod;
    ll t;cin >> t;
    while (t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 383ms
memory: 7652kb

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 10994...

result:

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