#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4005;
const int mod = 1e9 + 7;
int Add(int x, int y)
{
return x + y >= mod ? x + y - mod : x + y;
}
int Sub(int x, int y)
{
return x - y < 0 ? x - y + mod : x - y;
}
int Mul(int x, int y)
{
return 1ll * x * y % mod;
}
int BinPow(int n, int k)
{
int ans = 1, cur = n;
while(k)
{
if(k & 1)
{
ans = Mul(ans, cur);
}
cur = Mul(cur, cur);
k >>= 1;
}
return ans;
}
int n, m;
int cnt[MAXN], fact[MAXN], ifact[MAXN], dp[2][MAXN];
int C(int n, int k)
{
return Mul(fact[n], Mul(ifact[k], ifact[n - k]));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fact[0] = 1;
for(int i = 1; i < MAXN; i++)
{
fact[i] = Mul(fact[i - 1], i);
}
ifact[MAXN - 1] = BinPow(fact[MAXN - 1], mod - 2);
for(int i = MAXN - 2; i >= 0; i--)
{
ifact[i] = Mul(ifact[i + 1], i + 1);
}
cin >> n >> m;
for(int i = 0; i < 2 * n; i++)
{
cnt[i % m]++;
}
int cur = 0;
dp[0][0] = 1;
for(int i = 0; i < m; i++)
{
for(int j = 0; j <= cur; j++)
{
if(dp[0][j])
{
for(int newpair = 0; 2 * newpair <= cnt[i]; newpair++)
{
dp[1][j + newpair] = Add(dp[1][j + newpair], Mul(dp[0][j], Mul(fact[cnt[i]], Mul(ifact[cnt[i] - 2 * newpair], C(n - j, newpair)))));
}
}
}
cur += cnt[i];
for(int j = 0; j <= cur; j++)
{
dp[0][j] = dp[1][j];
dp[1][j] = 0;
}
}
for(int i = n; i >= 0; i--)
{
dp[0][i] = Mul(dp[0][i], fact[2 * n - 2 * i]);
for(int j = i + 1; j <= n; j++)
{
dp[0][i] = Sub(dp[0][i], Mul(C(j, i), dp[0][j]));
}
}
cout << dp[0][0] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
28 |
Correct |
4 ms |
332 KB |
Output is correct |
29 |
Correct |
6 ms |
332 KB |
Output is correct |
30 |
Correct |
6 ms |
332 KB |
Output is correct |
31 |
Correct |
4 ms |
332 KB |
Output is correct |
32 |
Correct |
7 ms |
332 KB |
Output is correct |
33 |
Correct |
8 ms |
364 KB |
Output is correct |
34 |
Correct |
15 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
332 KB |
Output is correct |
28 |
Correct |
4 ms |
332 KB |
Output is correct |
29 |
Correct |
6 ms |
332 KB |
Output is correct |
30 |
Correct |
6 ms |
332 KB |
Output is correct |
31 |
Correct |
4 ms |
332 KB |
Output is correct |
32 |
Correct |
7 ms |
332 KB |
Output is correct |
33 |
Correct |
8 ms |
364 KB |
Output is correct |
34 |
Correct |
15 ms |
332 KB |
Output is correct |
35 |
Correct |
25 ms |
380 KB |
Output is correct |
36 |
Correct |
50 ms |
384 KB |
Output is correct |
37 |
Correct |
26 ms |
364 KB |
Output is correct |
38 |
Correct |
37 ms |
372 KB |
Output is correct |
39 |
Correct |
64 ms |
364 KB |
Output is correct |
40 |
Correct |
83 ms |
368 KB |
Output is correct |