QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#661771#8237. Sugar Sweet IISzangWA 147ms17948kbC++202.7kb2024-10-20 18:03:412024-10-20 18:03:42

Judging History

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

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

answer

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

using ll = long long;
#define endl '\n'

const int MOD = 1e9 + 7;
const int N = 5e5 + 5;
int a[N], b[N], w[N], f[N];
ll ans[N], fac[N];
vector<int> e[N];
int n;

ll qpow(ll a, ll b, ll p)
{
    ll tmp = 1;
    a = a % p;
    while (b)
    {
        if (1 & b)
            tmp = tmp * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return tmp % p;
}

ll inv(ll a, ll p) // 费马小定理求逆元
{
    return qpow(a, p - 2, p);
}

int cnt;

int dfs(int u)
{
    for (auto v : e[u])
    {
        // cout<<u<<" "<<v<<endl;
        if (a[u] < a[v])
        {
            ans[u] = a[u] + w[u];
            f[u] = 1;
            return 1;
        }
        else if (a[u] >= a[v] + w[v])
        {
            ans[u] = a[u];
            f[u] = 1;
            return 0;
        }
        else if (a[u] >= a[v])
        {

            int ok = dfs(v);
            cnt++;
            if (ok == 1)
            {
                // cout<<cnt<<" "<<n<<" "<<u<<endl;
                ll x = fac[cnt];
                ll y = fac[n];
                x = ((y * inv(x, MOD) % MOD) + MOD) % MOD;
                ll xx = ((a[u] * (y - x) % MOD + (w[u] + a[u]) * x % MOD) + MOD) % MOD;
                ll yy = y;
                ans[u] = (xx * inv(yy, MOD) % MOD + MOD) % MOD;
                // cout<<ansx[u]<<" "<<ansy[u]<<endl;
                f[u] = 1;
                return 1;
            }
            else
            {
                ans[u] = a[u];
                f[u] = 1;
                return 0;
            }
        }
    }
    return 0;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> w[i];
        e[i].clear();
        f[i] = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        if (i != b[i])
            e[i].push_back(b[i]);
        else
            ans[i] = a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        // cout << "111" << endl;
        cnt = 1;
        if (f[i] == 0)
            dfs(i);
    }
    // cout << "111" << endl;
    for (int i = 1; i <= n; i++)
    {
        // cout<<i<<endl;
        cout << ans[i] << " ";
        // cout << ansx[i] << " " << ansy[i] << endl;
    }
    cout << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    fac[0] = 1;
    for (int i = 1; i <= N; ++i)
        fac[i] = ((1ll * fac[i - 1] * i) % MOD + MOD) % MOD;

    int t = 1;
    cin >> t;

    while (t--)
    {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 15932kb

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: 147ms
memory: 17948kb

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 
665922935 577926775 132646723 421298438 601054667 10994...

result:

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