-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting Reorders.cpp
More file actions
executable file
·55 lines (52 loc) · 1.02 KB
/
Copy pathCounting Reorders.cpp
File metadata and controls
executable file
·55 lines (52 loc) · 1.02 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
LL C[5005][5005];
LL dp[29][5005];
LL cnt[29];
int main() {
ios_base::sync_with_stdio(false);
string s;
cin >> s;
LL n = s.size();
for (auto c : s) {
cnt[c - 'a']++;
}
vector<int> v;
v.push_back(0);
for (int i = 0; i < 26; i++) {
if (cnt[i]) {
v.push_back(cnt[i]);
}
}
C[0][0] = 1;
for (int i = 1; i <= n; i++) {
C[i][0] = 1;
for (int j = 1; j <= n; j++) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
}
}
dp[0][0] = 1;
int start = 0;
for (int i = 1; i < int(v.size()); i++) {
for (int j = 0; j <= start; j++) {
for (int k = 1; k <= v[i]; k++) {
dp[i][j + k] += dp[i - 1][j] * C[v[i] - 1][k - 1] % MOD * C[k + j][k];
dp[i][j + k] %= MOD;
}
}
start += v[i];
}
LL res = 0;
for (int i = 0; i <= n; i++) {
if ((i + n) % 2 == 0) {
res += dp[v.size() - 1][i];
} else {
res -= dp[v.size() - 1][i];
}
res = (res + MOD) % MOD;
}
cout << (res + MOD) % MOD << "\n";
return 0;
}