QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661723#8237. Sugar Sweet IISzangWA 192ms23448kbC++202.8kb2024-10-20 17:46:402024-10-20 17:46:40

Judging History

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

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

answer

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

#define int long long
#define endl '\n'

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

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

int inv(int a, int p) // 费马小定理求逆元
{
    return qpow(a, p - 2, p);
}
int fac[N];
int cnt = 0;
int dfs(int u)
{
    for (auto v : e[u])
    {
        // cout<<u<<" "<<v<<endl;
        if (a[u] < a[v])
        {
            ansx[u] = a[u] + w[u];
            ansy[u] = 1;
            f[u] = 1;
            return 1;
        }
        else if (a[u] >= a[v] + w[v])
        {
            ansx[u] = a[u];
            ansy[u] = 1;
            f[u] = 1;
            return 0;
        }
        else if (a[u] >= a[v])
        {
            int ok = dfs(v);
            cnt++;
            if (ok == 1)
            {
                // cout<<cnt<<" "<<n<<" "<<u<<endl;
                int x = fac[cnt];
                int y = fac[n];
                x = ((y * inv(x, MOD) % MOD) + MOD) % MOD;
                ansx[u] = ((a[u] * (y - x) % MOD + (w[u] + a[u]) * x % MOD) + MOD) % MOD;
                ansy[u] = y;
                // cout<<ansx[u]<<" "<<ansy[u]<<endl;
                f[u] = 1;
                return 1;
            }
            else
            {
                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
            ansx[i] = a[i], ansy[i] = 1;
    }
    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;
        ans[i] = (ansx[i] * inv(ansy[i], MOD) % MOD + MOD) % MOD;
        cout << (ans[i] + MOD) % MOD << " ";
        // 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();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 192ms
memory: 23172kb

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 
871186919 814243920 132646723 421298438 601054667 99438820...

result:

wrong answer 25th numbers differ - expected: '665922935', found: '871186919'