QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91311#5820. 置换skittles14120 9ms3804kbC++173.7kb2023-03-28 12:47:582023-03-28 12:48:07

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-28 12:48:07]
  • 评测
  • 测评结果:0
  • 用时:9ms
  • 内存:3804kb
  • [2023-03-28 12:47:58]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

struct mint {
    static constexpr int MOD = 998244353;

    static constexpr int norm(long x) {
        x %= MOD;
        if (x < 0) {
            x += MOD;
        }
        return int(x);
    }

    static constexpr mint pow(mint base, long exp) {
        mint ans = 1;
        while (exp) {
            if (exp & 1) {
                ans *= base;
            }
            base *= base;
            exp >>= 1;
        }
        return ans;
    }

    static constexpr mint inv(mint base) {
        return pow(base, MOD - 2);
    }

    int x;

    constexpr mint() : x(0) {}
    constexpr mint(long x) : x(norm(x)) {}

    friend ostream& operator<<(ostream& out, const mint& m) {
        return out << m.x;
    }

    mint& operator+=(const mint& m) {
        x += m.x;
        if (x >= MOD) {
            x -= MOD;
        }
        return *this;
    }

    mint& operator*=(const mint& m) {
        x = int(long(x) * m.x % MOD);
        return *this;
    }
    mint& operator/=(const mint& m) {
        return *this *= inv(m);
    }

    friend mint operator+(mint a, const mint& b) {
        return a += b;
    }
    friend mint operator*(mint a, const mint& b) {
        return a *= b;
    }
    friend mint operator/(mint a, const mint& b) {
        return a /= b;
    }
};

struct M {
    static constexpr int maxn = 1e4;

    mint fact[maxn], ifact[maxn];

    M() {
        fact[0] = ifact[0] = 1;
        for (int i = 1; i < maxn; i++) {
            fact[i] = i * fact[i - 1];
            ifact[i] = mint::inv(fact[i]);
        }
    }

    mint choose(int n, int k) {
        return fact[n] * ifact[n - k] * ifact[k];
    }
} M;

mint solve(int len, int m, const vector<int>& arr) {
    if (!m) {
        return 1;
    }
    mint dp[m + 1];
    dp[m] = 1;
    for (int i = m - 1; i >= 0; i--) {
        for (auto& a : arr) {
            if (i + a <= m) {
                dp[i] += M.choose(m - i - 1, a - 1) * M.fact[a - 1] * M.fact[len - 1] *
                         mint::pow(M.fact[len], a - 1) * dp[i + a];
            }
        }
    }
    dbg(dp[0], dp[1], dp[2]);
    return dp[0];
}

void solve() {
    int n, kv;
    cin >> n >> kv;
    int iarr[n];
    for (auto& a : iarr) {
        cin >> a;
        a--;
    }
    int comps[n + 1] {};
    bool vis[n] {};
    for (int i = 0; i < n; i++) {
        if (vis[i]) {
            continue;
        }
        int u = i, cnt = 0;
        while (!vis[u]) {
            vis[u] = true;
            u = iarr[u];
            cnt++;
        }
        comps[cnt]++;
    }
    vector<int> clens[n + 1];
    for (int i = 1; i <= n; i++) {
        int g = gcd(i, kv);
        clens[i / g].push_back(g);
    }
    mint ans = 1;
    for (int i = 1; i <= n; i++) {
        int m = comps[i];
        auto& arr = clens[i];
        ans *= solve(i, m, arr);
    }
    cout << ans << endl;
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    int tcs;
    cin >> tcs;
    while (tcs--) {
        solve();
    }
}

详细

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 3580kb

input:

10
6 5
1 2 6 3 4 5
5 8
1 2 3 4 5
7 5
1 2 3 4 5 6 7
4 4
1 2 3 4
7 7
1 2 3 4 5 6 7
4 4
1 2 3 4
5 4
1 2 4 3 5
8 8
1 2 3 4 5 6 7 8
4 5
1 3 2 4
6 6
1 2 3 4 5 6

output:

6
56
505
16
721
16
0
11264
1
396

result:

wrong answer 1st lines differ - expected: '1', found: '6'

Test #2:

score: 0
Wrong Answer
time: 3ms
memory: 3584kb

input:

10
4 6
3 1 2 4
5 8
2 1 3 4 5
8 4
1 2 3 4 5 6 7 8
6 5
4 1 2 3 5 6
5 5
1 2 3 4 5
5 5
1 2 3 4 5
6 7
1 2 3 4 5 6
6 4
1 2 3 4 5 6
6 7
6 1 2 3 4 5
7 6
1 2 3 4 5 6 7

output:

0
0
6224
6
25
25
1
256
120
2052

result:

wrong answer 4th lines differ - expected: '1', found: '6'

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 3492kb

input:

10
6 602552
1 2 3 4 5 6
4 775694
1 2 4 3
6 668467
1 4 2 3 5 6
6 558385
1 2 6 3 4 5
7 832183
4 1 2 3 5 6 7
6 631375
1 2 3 4 5 6
8 519340
1 2 3 5 4 6 7 8
4 636124
1 2 3 4
4 759099
3 1 2 4
7 977752
1 2 3 4 5 6 7

output:

256
0
2
6
6
145
0
16
0
1072

result:

wrong answer 3rd lines differ - expected: '1', found: '2'

Test #4:

score: 0
Wrong Answer
time: 3ms
memory: 3472kb

input:

10
43 725761
1 2 3 4 5 6 7 8 10 9 11 12 13 15 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
37 542860
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 26 28 29 30 31 32 33 34 36 35 37
27 793967
2 1 4 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

output:

1
0
1
942172269
0
932936954
449319403
668622514
0
374933738

result:

wrong answer 4th lines differ - expected: '656150888', found: '942172269'

Test #5:

score: 0
Wrong Answer
time: 3ms
memory: 3516kb

input:

10
40 535121
3 1 2 4 5 6 7 8 9 11 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
43 660193
1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 15 19 17 18 20 26 21 22 23 24 25 27 28 29 30 34 31 32 33 35 36 37 38 39 40 41 42 43
38 596459
1 2 4 3 5 6 7 8 9 10 11 12 13 15 14 ...

output:

2
836441008
266135717
152238854
0
0
0
0
0
982061271

result:

wrong answer 1st lines differ - expected: '1', found: '2'

Test #6:

score: 0
Wrong Answer
time: 3ms
memory: 3520kb

input:

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

output:

0
0
48
0
860677875
767648771
643070244
677985168
17280
728028170

result:

wrong answer 3rd lines differ - expected: '1', found: '48'

Test #7:

score: 0
Wrong Answer
time: 3ms
memory: 3708kb

input:

10
2899 540778
3 1 2 4 5 6 9 7 8 10 11 12 13 14 15 16 17 18 19 20 21 22 24 23 27 25 26 28 29 30 31 32 33 34 35 37 36 38 39 40 41 42 44 43 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 70 68 69 71 72 73 74 75 76 77 78 80 79 81 82 83 89 84 85 86 87 88 90 91 92 93 94 95 96 97 98 ...

output:

0
827728765
0
0
118673224
240082602
0
944156932
0
326095391

result:

wrong answer 2nd lines differ - expected: '786737927', found: '827728765'

Test #8:

score: 0
Wrong Answer
time: 6ms
memory: 3804kb

input:

10
2310 568163
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 24 25 27 28 30 29 31 32 33 34 35 36 37 38 39 40 41 46 42 43 44 45 47 48 49 50 51 52 57 53 54 55 56 58 59 61 60 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...

output:

423614289
0
201299510
0
608374798
598653792
0
0
0
386210651

result:

wrong answer 1st lines differ - expected: '1', found: '423614289'

Test #9:

score: 0
Wrong Answer
time: 9ms
memory: 3800kb

input:

10
1564 511376
1 2 3 4 5 6 8 7 9 10 11 12 13 14 15 16 20 17 18 19 21 22 23 24 25 26 28 27 29 30 31 35 32 33 34 36 41 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 76 74 75 77 78 82 79 80 81 83 84 85 86 87 88 89 90 91 92 93 94 97 95 96 98 ...

output:

0
670996689
0
0
584519156
0
0
0
0
920016655

result:

wrong answer 2nd lines differ - expected: '207595358', found: '670996689'

Test #10:

score: 0
Wrong Answer
time: 7ms
memory: 3664kb

input:

10
1749 969801
1 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 34 31 32 33 36 35 37 38 39 40 41 42 43 44 45 46 48 47 49 50 51 54 52 53 55 56 57 58 60 59 61 62 63 64 65 66 67 68 69 70 71 72 73 75 74 76 77 78 79 80 81 82 83 84 85 86 89 87 88 90 91 92 93 94 95 96 97 99 ...

output:

0
0
576494365
381547507
562834323
0
0
0
0
790479273

result:

wrong answer 3rd lines differ - expected: '1', found: '576494365'