結果

問題 No.170 スワップ文字列(Easy)
ユーザー startcppstartcpp
提出日時 2016-01-17 21:29:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,387 bytes
コンパイル時間 1,018 ms
コンパイル使用メモリ 51,872 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 16:25:51
合計ジャッジ時間 1,434 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//バブルソートは任意の順列をソートできる→任意の順列を作ることができると言っても良い
//Sの順列の総数 - 1が答えとなる
//文字xの個数をa[x]とすれば、|S|!/Πa[x]! - 1 (mod 573) ('A' <= x <= 'Z')が答えになるが、
//573が素数ではないので、これを直接求めることはできない。
//
//上の式は、「n個の中からa[x]個を選び文字xにする」を繰り返して出来る順列の総数ともいえるので、
//C(n, a['A']) * C(n - a['A'], a['B']) * C(n - a['A'] - a['B'], a['C']) * … - 1と同じ結果になる。
//これは逐次modを取りながら計算できるので、これを計算する。コンビネーションは前処理で求めておく
//O(n^2) (nはSの長さ)

#include <iostream>
#include <string>
using namespace std;

string s;
int comb[1001][1001];
int cnt[26];

int main(){
	int i, j, n;
	
	cin >> s;
	n = s.length();
	
	comb[0][0] = 1;
	for( i = 1; i <= n; i++ ){
		for( j = 0; j <= n; j++ ){
			comb[i][j] = comb[i-1][j] + ((j > 0) ? comb[i-1][j-1] : 0);
			//comb[i][j] %= 573;
		}
	}
	
	for( i = 0; i < n; i++ ){
		cnt[s[i] - 'A']++;
	}
	
	int diff = n;
	int ans = 1;
	for( i = 0; i < 26; i++ ){
		ans *= comb[diff][cnt[i]];
		//ans %= 573;
		diff -= cnt[i];
	}
	//ans = (ans + 572) % 573;	//法573の元で-1する
	ans--;
	cout << ans << endl;
	return 0;
}
0