QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661753#8237. Sugar Sweet IISzangWA 4ms19436kbC++202.8kb2024-10-20 17:56:522024-10-20 17:56:54

Judging History

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

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

answer

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

#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];
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
            {
                ansx[u] = a[u];
                ansy[u] = 1;
                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
            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;
        cout << (ansx[i] * inv(ansy[i], MOD) % MOD + 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: 0
Wrong Answer
time: 4ms
memory: 19436kb

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:

249960448 5 5 6 
5 10 9 
0 0 6 
548894023 4 3 4 5 

result:

wrong answer 1st numbers differ - expected: '500000007', found: '249960448'