#include <bits/stdc++.h>
#define INF 1000000005
#define INFL 1000000000000000005ll
#define EPS 1e-6
#define pb push_back
#define pause system("pause")
#define exit exit(0)
#define endl '\n'
using namespace std;
using ull = unsigned long long;
using ll = long long;
typedef pair<ll, ll> pii;
const int N = 2005, MOD = 1e9 + 7;
int n, m, f[N], c[N][N], dp[N][N];
inline int add(int a, int b) {
return a + b < MOD ? (a + b) : (a + b - MOD);
}
inline int mul(int a, int b) {
return ((ll)a * b) % MOD;
}
inline void upd(int &a, int b) {
a = add(a, b);
}
inline int bpow(int a, int b) {
int ans = 1;
while (b != 0) {
if (b & 1)
ans = mul(ans, a);
a = mul(a, a), b >>= 1;
}
return ans;
}
int fact(int n) {
int ans = 1;
for (int i = 2; i <= n; ++i) {
ans = mul(ans, i);
}
return ans;
}
int naive(int n, int m) {
vector<int> p(2 * n);
for (int i = 0; i < 2 * n; ++i) {
p[i] = i + 1;
}
int ans = 0;
do {
bool ok = 1;
for (int i = 0; ok && i < 2 * n; ++i) {
for (int j = i + 1; j < 2 * n; ++j) {
if (abs(p[i] - p[j]) == n) {
ok &= ((j - i) % m) != 0;
break;
}
}
}
ans += ok;
} while (next_permutation(p.begin(), p.end()));
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
//cout << naive(n, m) << endl;
for (int i = 0; i <= n; ++i) {
c[i][0] = 1;
for (int j = 1; j <= i; ++j) {
c[i][j] = add(c[i - 1][j - 1], c[i - 1][j]);
}
}
for (int i = 0; i < 2 * n; ++i) {
++f[i % m];
}
int sm = f[0];
dp[0][f[0]] = mul(c[n][f[0]], bpow(2, f[0]));
for (int i = 0; i < m - 1; ++i) {
for (int j = 0; j <= sm; ++j) {
for (int k = 0; k <= j && k <= f[i + 1]; ++k) {
if (j - k + f[i + 1] - k > n || dp[i][j] == 0)
continue;
upd(dp[i + 1][j - k + f[i + 1] - k],
mul(
dp[i][j],
mul(
c[j][k],
mul(
c[n - (((sm - j) / 2) + j)][f[i + 1] - k],
bpow(2, f[i + 1] - k)
)
)
)
);
}
}
sm += f[i + 1];
}
int ans = dp[m - 1][0];
for (int i = 0; i < m; ++i) {
ans = mul(ans, fact(f[i]));
}
cout << ans << endl;
//pause;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
744 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
744 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
584 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
3 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
3 ms |
1492 KB |
Output is correct |
27 |
Correct |
3 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
744 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
584 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
3 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
3 ms |
1492 KB |
Output is correct |
27 |
Correct |
3 ms |
3028 KB |
Output is correct |
28 |
Correct |
8 ms |
3668 KB |
Output is correct |
29 |
Correct |
8 ms |
5460 KB |
Output is correct |
30 |
Correct |
13 ms |
6836 KB |
Output is correct |
31 |
Correct |
9 ms |
3668 KB |
Output is correct |
32 |
Correct |
9 ms |
5768 KB |
Output is correct |
33 |
Correct |
10 ms |
7632 KB |
Output is correct |
34 |
Correct |
17 ms |
10704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
584 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
744 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
584 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
1492 KB |
Output is correct |
24 |
Correct |
3 ms |
2260 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
3 ms |
1492 KB |
Output is correct |
27 |
Correct |
3 ms |
3028 KB |
Output is correct |
28 |
Correct |
8 ms |
3668 KB |
Output is correct |
29 |
Correct |
8 ms |
5460 KB |
Output is correct |
30 |
Correct |
13 ms |
6836 KB |
Output is correct |
31 |
Correct |
9 ms |
3668 KB |
Output is correct |
32 |
Correct |
9 ms |
5768 KB |
Output is correct |
33 |
Correct |
10 ms |
7632 KB |
Output is correct |
34 |
Correct |
17 ms |
10704 KB |
Output is correct |
35 |
Correct |
60 ms |
11244 KB |
Output is correct |
36 |
Correct |
60 ms |
23932 KB |
Output is correct |
37 |
Correct |
65 ms |
11092 KB |
Output is correct |
38 |
Correct |
43 ms |
16836 KB |
Output is correct |
39 |
Correct |
43 ms |
20916 KB |
Output is correct |
40 |
Correct |
71 ms |
27936 KB |
Output is correct |