QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#628075#8237. Sugar Sweet IIwpoemWA 129ms3804kbC++204.6kb2024-10-10 18:28:312024-10-10 18:28:36

Judging History

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

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

answer

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#ifdef LOCAL
template <class T, size_t size = std::tuple_size<T>::value>
std::string to_debug(T, std::string s = "")
    requires(not std::ranges::range<T>);
std::string to_debug(auto x)
    requires requires(std::ostream& os) { os << x; }
{
    return static_cast<std::ostringstream>(std::ostringstream() << x).str();
}
std::string to_debug(std::ranges::range auto x, std::string s = "")
    requires(not std::is_same_v<decltype(x), std::string>)
{
    for (auto xi : x) {
        s += ", " + to_debug(xi);
    }
    return "[" + s.substr(s.empty() ? 0 : 2) + "]";
}
template <class T, size_t size>
std::string to_debug(T x, std::string s)
    requires(not std::ranges::range<T>)
{
    [&]<size_t... I>(std::index_sequence<I...>) { ((s += ", " + to_debug(get<I>(x))), ...); }(std::make_index_sequence<size>());
    return "(" + s.substr(s.empty() ? 0 : 2) + ")";
}
#define debug(...) std::cerr << __LINE__ << ": (" #__VA_ARGS__ ") = " << to_debug(std::tuple(__VA_ARGS__)) << "\n"
#else
#define debug(x...)
#endif

#define int long long

using i64 = long long;

const int mod = 1e9 + 7;

inline i64 inv(i64 z)
{
    int p = mod - 2;
    i64 ans = 1;
    while (p) {
        if (p & 1)
            ans = (ans * z) % mod;
        z = (z * z) % mod;
        p >>= 1;
    }
    return ans;
}

void solve()
{
#define tests
    auto norm = [&](auto x) {x %= mod; if (x < 0) {x += mod;}; x %= mod; return x; };
    // 64 num;
    // std::cin >> num;
    // std::cout << ((i64)31 * inv(6))%mod;
    int n;
    std::cin >> n;
    std::vector<int> a(n + 1), b(n + 1), wi(n + 1), ans(n + 1, -1);
    std::vector<int> tag(n + 1);
    std::vector<int> rev_to(n + 1);
    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 >> wi[i];
    }

    // make tags
    for (int i = 1; i <= n; i++) {
        if (a[b[i]] > a[i]) {
            tag[i] = 1; // tag 3
            rev_to[b[i]]=i;
            continue;
        } else if (a[b[i]] + wi[b[i]] <= a[i]) {
            tag[i] = 0; // tag 1
            continue;
        }

        else if (a[b[i]] + wi[b[i]] > a[i] ) {
            tag[i] = 2;
            rev_to[b[i]]=i;
        } else if (a[b[i]] + wi[b[i]] <= a[i] and a[b[i]] <= a[i]) {
            tag[i] = 3;
            // rev_to[b[i]] = i;
        }
    }

    // build reverse graph
    // for (int i = 1; i <= n; i++) {
    //     rev_to[b[i]] = i;
    // }

    std::vector<int> fac(n + 1), invfac(n + 1);
    fac[0] = 1;
    for (int i = 1; i <= n; i++) {
        fac[i] = norm(fac[i - 1] * i);
    }
    invfac[n] = inv(fac[n]);
    for (int i = n - 1; i >= 1; i--) {
        invfac[i] = norm(invfac[i + 1] * (i + 1));
    }

    for (int i = 1; i <= n; i++) {
        if (tag[i] == 0) {
            auto dfs1 = [&](auto self, int pt) -> void {
                ans[pt] = a[pt];
                if (tag[rev_to[pt]] == 3) {
                    tag[rev_to[pt]] = 5;
                    self(self, rev_to[pt]);
                }
            };
            dfs1(dfs1, i);
        } else if (tag[i] == 1) {
            auto dfs2 = [&](auto self, int pt, int dep) -> void {
                ans[pt] = norm(invfac[dep] * norm(a[pt] + wi[pt]));
                ans[pt] = norm(ans[pt] + norm(norm(1 - invfac[dep]) * a[pt]));
                if (tag[rev_to[pt]] == 2) {
                    tag[rev_to[pt]] = 5;
                    self(self, rev_to[pt], dep + 1);
                }
            };
            dfs2(dfs2, i, 1);
        }
    }

    for (int i = 1; i <= n; i++) {
        if (ans[i] == -1) {
            ans[i] = a[i];
        }
        // if (tag[i] == 1) {
        //     ans[i] = norm(a[i] + wi[i]);
        // }
        // if (tag[i] == 0) {
        //     ans[i] = a[i];
        // }
        std::cout << ans[i] << " ";
    }
    std::cout << "\n";
}

signed main()
{
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int _ { 1 };
#ifdef tests
    std::cin >> _;
#endif
    while (_--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3440kb

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: 129ms
memory: 3804kb

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 168068923 421298438 601054667 99438820...

result:

wrong answer 27th numbers differ - expected: '132646723', found: '168068923'