Submission #61392143


Source Code Expand

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

using ll = long long;

const int MAXN = 2025;
const ll MOD = 998244353LL;

int n, m, p[MAXN+2], viz[MAXN+2], real[MAXN+2];
long long ans = 1;
vector<int> stk, ch[MAXN+2];
ll dp[MAXN+2][MAXN+2];

void dfs(int node)
{
    for (int j = 1; j <= m; j++) {
        dp[node][j] = 1;
    }
    for (auto x : ch[node]) {
        dfs(x);
        ll suma = 0;
        for (int j = 1; j <= m; j++) {
            suma = (suma + dp[x][j]) % MOD;
            dp[node][j] = (dp[node][j] * suma) % MOD;
        }
    }
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        real[i] = i;
    }
    for (int i = 1; i <= n; i++) {
        if (!viz[i]) {
            int node = i;
            viz[node] = 1;
            stk.push_back(node);
            while (!viz[p[node]]) {
                node = p[node];
                viz[node] = 1;
                stk.push_back(node);
            }
            if (viz[p[node]] == 1) {
                int x = p[node];
                while (stk.back() != x) {
                    real[stk.back()] = x;
                    viz[stk.back()] = 2;
                    stk.pop_back();
                }
            }
            while (!stk.empty()) {
                viz[stk.back()] = 2;
                stk.pop_back();
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        p[i] = real[p[i]];
        if (real[i] == i && p[i] != i) {
            ch[p[i]].push_back(i);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (p[i] == i) {
            dfs(i);
            ll suma = 0;
            for (int j = 1; j <= m; j++) {
                suma = (suma + dp[i][j]) % MOD;
            }
            ans = (ans * suma) % MOD;
        }
    }
    cout << (ans % MOD + MOD) % MOD;
    return 0;
}

Submission Info

Submission Time
Task F - Count Arrays
User Andrei_ierdnA
Language C++ 17 (gcc 12.2)
Score 550
Code Size 1982 Byte
Status AC
Exec Time 29 ms
Memory 35844 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 3
AC × 37
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3492 KiB
00_sample_01.txt AC 1 ms 3492 KiB
00_sample_02.txt AC 1 ms 3560 KiB
01_random_00.txt AC 5 ms 10112 KiB
01_random_01.txt AC 15 ms 22476 KiB
01_random_02.txt AC 2 ms 5724 KiB
01_random_03.txt AC 1 ms 4028 KiB
01_random_04.txt AC 2 ms 4920 KiB
01_random_05.txt AC 1 ms 3672 KiB
01_random_06.txt AC 3 ms 6908 KiB
01_random_07.txt AC 3 ms 5940 KiB
01_random_08.txt AC 3 ms 5632 KiB
01_random_09.txt AC 1 ms 4488 KiB
01_random_10.txt AC 2 ms 5812 KiB
01_random_11.txt AC 3 ms 7228 KiB
01_random_12.txt AC 27 ms 35476 KiB
01_random_13.txt AC 27 ms 35520 KiB
01_random_14.txt AC 28 ms 35236 KiB
01_random_15.txt AC 18 ms 25020 KiB
01_random_16.txt AC 20 ms 26820 KiB
01_random_17.txt AC 16 ms 22940 KiB
01_random_18.txt AC 28 ms 34896 KiB
01_random_19.txt AC 14 ms 20404 KiB
01_random_20.txt AC 28 ms 35468 KiB
01_random_21.txt AC 11 ms 16968 KiB
01_random_22.txt AC 12 ms 17472 KiB
01_random_23.txt AC 19 ms 26544 KiB
02_handmade_00.txt AC 29 ms 35844 KiB
02_handmade_01.txt AC 28 ms 35772 KiB
02_handmade_02.txt AC 27 ms 35564 KiB
02_handmade_03.txt AC 29 ms 35608 KiB
02_handmade_04.txt AC 28 ms 35724 KiB
02_handmade_05.txt AC 29 ms 35652 KiB
02_handmade_06.txt AC 1 ms 3412 KiB
02_handmade_07.txt AC 1 ms 3480 KiB
02_handmade_08.txt AC 1 ms 3576 KiB
02_handmade_09.txt AC 1 ms 3644 KiB