結果

問題 No.171 スワップ文字列(Med)
ユーザー xuzijian629xuzijian629
提出日時 2018-11-02 21:00:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 1,000 ms
コード長 1,416 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 214,700 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-30 17:35:38
合計ジャッジ時間 2,850 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
11,136 KB
testcase_01 AC 8 ms
11,264 KB
testcase_02 AC 8 ms
11,264 KB
testcase_03 AC 7 ms
11,264 KB
testcase_04 AC 8 ms
11,264 KB
testcase_05 AC 8 ms
11,264 KB
testcase_06 AC 7 ms
11,264 KB
testcase_07 AC 7 ms
11,136 KB
testcase_08 AC 7 ms
11,264 KB
testcase_09 AC 8 ms
11,264 KB
testcase_10 AC 7 ms
11,136 KB
testcase_11 AC 7 ms
11,136 KB
testcase_12 AC 7 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
constexpr i64 MOD = 573;

struct Combination {
    int n;
    vvi dp;
    Combination(int n) : n(n) {}
    
    void build() {
        dp = vvi(n + 1, vi(n + 1));
        for (int i = 0; i <= n; i++) {
            dp[i][0] = 1;
            dp[i][i] = 1;
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j < i; j++) {
                dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                dp[i][j] %= MOD;
            }
        }
    }
    
    i64 built_ncr(int n, int r) {
        return dp[n][r];
    }
    
    // avoid MLE
    i64 ncr(int n, int r) {
        if (n < 2) return 1;
        vi cur(2, 1);
        for (int i = 2; i <= n; i++) {
            vi nex(n + 1, 1);
            for (int j = 1; j < i; j++) {
                nex[j] = cur[j - 1] + cur[j];
                nex[j] %= MOD;
            }
            cur = move(nex);
        }
        return cur[r];
    }
};

int main() {
    string s;
    cin >> s;
    Combination comb(1000);
    comb.build();

    map<char, int> cnt;
    for (char c: s) {
        cnt[c]++;
    }

    i64 ans = 1;
    int tot = s.size();
    for (auto& p: cnt) {
        ans *= comb.built_ncr(tot, p.second);
        ans %= MOD;
        tot -= p.second;
    }
    assert(tot == 0);
    cout << (ans - 1 + MOD) % MOD << endl;
}
0