結果

問題 No.171 スワップ文字列(Med)
ユーザー yuustiyuusti
提出日時 2015-03-24 19:15:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,301 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 97,124 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 09:56:52
合計ジャッジ時間 1,594 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>
#include <climits>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <complex>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;


// 構築O(p), クエリO(log_p n)
struct mod_fact{
	int p;
	vector<long long> table;
	mod_fact(int _p) : p(_p){
		table.resize(p);
		table[0] = 1;
		for (int i = 1; i < p; ++i)
			table[i] = table[i - 1] * i % p;
	}

	// n! = a*p^e となる aを mod p で求める
	int fact(int n, int &e){
		e = 0;
		if (n == 0) return 1;

		int res = fact(n / p, e);
		e += n / p;

		if (n / p % 2 != 0) return res * (p - table[n%p]) % p;
		return res * table[n%p] % p;
	}

	int mod_pow(long long x, long long n){
		long long res = 1 % p;
		x %= p;
		while (n){
			if (n & 1) res = res*x%p;
			x = x*x%p;
			n /= 2;
		}
		return res;
	}

	int inv(long long x){
		return mod_pow(x, p - 2);
	}

	int comb(int n, int k){
		if (n < 0 || k < 0 || n < k) return 0;
		int e1, e2, e3;
		long long a1 = fact(n, e1), a2 = fact(k, e2), a3 = fact(n - k, e3);
		if (e1 > e2 + e3) return 0;
		return a1 * inv(a2*a3%p) % p;
	}
};


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(20);

	string s;
	cin >> s;

	int cnt[256] = {};
	for (auto c : s) cnt[c]++;

	int n = s.size();

	const int m = 573;
	vector<int> rem;
	vector<int> pf = { 3, 191 };
	for (int p : pf){
		mod_fact mf(p);

		int nn = n;
		ll x = 1;
		rep(i, 256){
			if (!nn) break;
			x = x*mf.comb(nn, cnt[i]) % p;
			nn -= cnt[i];
		}

		rem.push_back(x);
	}

	rep(i, m){
		if (i % pf[0] == rem[0] && i % pf[1] == rem[1]){
			cout << (i+m-1)%m << endl;
			break;
		}
	}
}
0