UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#147657#11. Aympc2005100684ms13892kbC++111.2kb2022-03-29 10:03:232022-03-29 13:31:30

answer

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

const int N = 1e6 + 10, mod = 998244353;

int n, fac[N], inv[N], ans, p[N], c[N];

bool vis[N];

int fpow_(int a, int b, int res = 1) {
    for (; b; b >>= 1, a = 1ll*a*a%mod) {
        if (b&1) {
            res = 1ll*res*a%mod;
        }
    }

    return res;
}

int main() {
    scanf("%d", &n);
    fac[0] = 1;
    
    for (int i = 1; i <= n; i++) {
        fac[i] = 1ll*fac[i - 1]*i%mod;
    }

    inv[n] = fpow_(fac[n], mod - 2);

    for (int i = n; i; i--) {
        inv[i - 1] = 1ll*inv[i]*i%mod;
    }

    for (int i = 1; i <= n; i++) {
        scanf("%d", &p[i]);
    }

    for (int i = 1; i <= n; i++) {
        if (vis[i]) {
            continue;
        }

        int cnt = 0;

        for (int j = i; !vis[j]; j = p[j]) {
            vis[j] = 1;
            cnt++;
        }

        c[cnt]++;
    }

    ans = fac[n];

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= c[i]; j++) {
            ans = 1ll*ans*inv[i]%mod*fac[i - 1]%mod;
        }

        ans = 1ll*ans*inv[c[i]]%mod;
    }

    printf("%d\n", (ans%mod + mod)%mod);
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 1208kb

input:

10
1 3 2 5 6 4 8 9 7 10

output:

50400

result:

ok 1 number(s): "50400"

Test #2:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

10
1 2 4 3 6 5 8 9 7 10

output:

25200

result:

ok 1 number(s): "25200"

Test #3:

score: 10
Accepted
time: 0ms
memory: 1216kb

input:

1000
1 2 4 3 6 7 5 9 10 8 12 13 11 15 16 17 14 19 20 21 22 18 24 25 26 27 28 23 30 31 32 33 34 29 36...

output:

456475462

result:

ok 1 number(s): "456475462"

Test #4:

score: 10
Accepted
time: 0ms
memory: 1220kb

input:

1000
1 3 2 5 4 7 8 6 10 11 9 13 14 15 12 17 18 19 16 21 22 23 24 20 26 27 28 29 30 25 32 33 34 35 36...

output:

163270710

result:

ok 1 number(s): "163270710"

Test #5:

score: 10
Accepted
time: 106ms
memory: 13888kb

input:

1000000
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3...

output:

595392237

result:

ok 1 number(s): "595392237"

Test #6:

score: 10
Accepted
time: 104ms
memory: 13888kb

input:

1000000
1 3 2 5 6 4 8 9 10 7 12 13 14 15 11 17 18 19 20 21 16 23 24 25 26 27 28 22 30 31 32 33 34 35...

output:

649890033

result:

ok 1 number(s): "649890033"

Test #7:

score: 10
Accepted
time: 110ms
memory: 13892kb

input:

1000000
1 3 2 5 6 4 8 9 10 7 12 13 14 15 11 17 18 19 20 21 16 23 24 25 26 27 28 22 30 31 32 33 34 35...

output:

649890033

result:

ok 1 number(s): "649890033"

Test #8:

score: 10
Accepted
time: 122ms
memory: 13892kb

input:

1000000
1 2 3 5 4 7 6 9 10 8 12 13 11 15 16 17 14 19 20 21 18 23 24 25 22 27 28 29 30 26 32 33 34 35...

output:

600361264

result:

ok 1 number(s): "600361264"

Test #9:

score: 10
Accepted
time: 127ms
memory: 13888kb

input:

1000000
1 2 3 5 4 7 8 6 10 11 12 9 14 15 16 17 13 19 20 21 22 18 24 25 26 27 23 29 30 31 32 28 34 35...

output:

409553302

result:

ok 1 number(s): "409553302"

Test #10:

score: 10
Accepted
time: 114ms
memory: 13888kb

input:

1000000
1 3 2 5 4 7 8 6 10 11 9 13 14 12 16 17 18 15 20 21 22 23 19 25 26 27 28 24 30 31 32 33 29 35...

output:

572565451

result:

ok 1 number(s): "572565451"

Extra Test:

score: 0
Extra Test Passed