#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
const int N = 4005;
int n , m;
int fact[N] , inv[N] , cnt[N] , pre[N];
int C[N][N] , dp[N][N];
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 x * y % mod;
}
int binpow(int n , int k)
{
int ret = 1;
while(k)
{
if (k & 1) ret = Mul(ret , n);
n = Mul(n , n);
k >>= 1;
}
return ret;
}
void build()
{
fact[0] = 1;
for (int i = 1 ; i < N ; ++i) fact[i] = Mul(fact[i - 1] , i);
inv[0] = 1;
for (int i = 1 ; i < N ; ++i) inv[i] = binpow(fact[i] , mod - 2);
for (int i = 0 ; i < 2 * n ; ++i) cnt[i % m + 1]++;
// for (int i = 1 ; i < m ; ++i) pre[i] = pre[i - 1] + 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] = Add(C[i - 1][j] , C[i - 1][j - 1]);
}
}
signed main()
{
// freopen("input.inp","r",stdin);
// freopen("output.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
build();
dp[0][0] = 1;
for (int i = 1 ; i <= m ; ++i)
{
for (int j = 0 ; j <= n ; ++j)
{
for (int k = 0 ; 2 * k <= cnt[i] && k <= j ; ++k)
{
int cur = dp[i - 1][j - k];
cur = Mul(cur , fact[cnt[i]]);
cur = Mul(cur , inv[cnt[i] - k * 2]);
cur = Mul(cur , C[j][k]);
dp[i][j] = Add(dp[i][j] , cur);
}
}
}
int ans = 0;
for (int j = 0 ; j <= n ; ++j)
{
if (j & 1) ans = Sub(ans , Mul(fact[n * 2 - j * 2] , Mul(C[n][j] , dp[m][j])));
else ans = Add(ans , Mul(fact[n * 2 - j * 2] , Mul(C[n][j] , dp[m][j])));
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
125832 KB |
Output is correct |
2 |
Correct |
62 ms |
125820 KB |
Output is correct |
3 |
Correct |
66 ms |
125816 KB |
Output is correct |
4 |
Correct |
71 ms |
125856 KB |
Output is correct |
5 |
Correct |
60 ms |
126020 KB |
Output is correct |
6 |
Correct |
62 ms |
125836 KB |
Output is correct |
7 |
Correct |
59 ms |
125836 KB |
Output is correct |
8 |
Correct |
61 ms |
125872 KB |
Output is correct |
9 |
Correct |
59 ms |
125796 KB |
Output is correct |
10 |
Correct |
60 ms |
125816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
125832 KB |
Output is correct |
2 |
Correct |
62 ms |
125820 KB |
Output is correct |
3 |
Correct |
66 ms |
125816 KB |
Output is correct |
4 |
Correct |
71 ms |
125856 KB |
Output is correct |
5 |
Correct |
60 ms |
126020 KB |
Output is correct |
6 |
Correct |
62 ms |
125836 KB |
Output is correct |
7 |
Correct |
59 ms |
125836 KB |
Output is correct |
8 |
Correct |
61 ms |
125872 KB |
Output is correct |
9 |
Correct |
59 ms |
125796 KB |
Output is correct |
10 |
Correct |
60 ms |
125816 KB |
Output is correct |
11 |
Correct |
61 ms |
126080 KB |
Output is correct |
12 |
Correct |
60 ms |
126020 KB |
Output is correct |
13 |
Correct |
59 ms |
125888 KB |
Output is correct |
14 |
Correct |
60 ms |
126008 KB |
Output is correct |
15 |
Correct |
59 ms |
126160 KB |
Output is correct |
16 |
Correct |
60 ms |
125936 KB |
Output is correct |
17 |
Correct |
59 ms |
125936 KB |
Output is correct |
18 |
Correct |
72 ms |
126112 KB |
Output is correct |
19 |
Correct |
63 ms |
125884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
125832 KB |
Output is correct |
2 |
Correct |
62 ms |
125820 KB |
Output is correct |
3 |
Correct |
66 ms |
125816 KB |
Output is correct |
4 |
Correct |
71 ms |
125856 KB |
Output is correct |
5 |
Correct |
60 ms |
126020 KB |
Output is correct |
6 |
Correct |
62 ms |
125836 KB |
Output is correct |
7 |
Correct |
59 ms |
125836 KB |
Output is correct |
8 |
Correct |
61 ms |
125872 KB |
Output is correct |
9 |
Correct |
59 ms |
125796 KB |
Output is correct |
10 |
Correct |
60 ms |
125816 KB |
Output is correct |
11 |
Correct |
61 ms |
126080 KB |
Output is correct |
12 |
Correct |
60 ms |
126020 KB |
Output is correct |
13 |
Correct |
59 ms |
125888 KB |
Output is correct |
14 |
Correct |
60 ms |
126008 KB |
Output is correct |
15 |
Correct |
59 ms |
126160 KB |
Output is correct |
16 |
Correct |
60 ms |
125936 KB |
Output is correct |
17 |
Correct |
59 ms |
125936 KB |
Output is correct |
18 |
Correct |
72 ms |
126112 KB |
Output is correct |
19 |
Correct |
63 ms |
125884 KB |
Output is correct |
20 |
Correct |
62 ms |
125980 KB |
Output is correct |
21 |
Correct |
60 ms |
126248 KB |
Output is correct |
22 |
Correct |
60 ms |
127420 KB |
Output is correct |
23 |
Correct |
62 ms |
125936 KB |
Output is correct |
24 |
Correct |
62 ms |
126716 KB |
Output is correct |
25 |
Correct |
71 ms |
127412 KB |
Output is correct |
26 |
Correct |
68 ms |
125892 KB |
Output is correct |
27 |
Correct |
80 ms |
127744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
125832 KB |
Output is correct |
2 |
Correct |
62 ms |
125820 KB |
Output is correct |
3 |
Correct |
66 ms |
125816 KB |
Output is correct |
4 |
Correct |
71 ms |
125856 KB |
Output is correct |
5 |
Correct |
60 ms |
126020 KB |
Output is correct |
6 |
Correct |
62 ms |
125836 KB |
Output is correct |
7 |
Correct |
59 ms |
125836 KB |
Output is correct |
8 |
Correct |
61 ms |
125872 KB |
Output is correct |
9 |
Correct |
59 ms |
125796 KB |
Output is correct |
10 |
Correct |
60 ms |
125816 KB |
Output is correct |
11 |
Correct |
61 ms |
126080 KB |
Output is correct |
12 |
Correct |
60 ms |
126020 KB |
Output is correct |
13 |
Correct |
59 ms |
125888 KB |
Output is correct |
14 |
Correct |
60 ms |
126008 KB |
Output is correct |
15 |
Correct |
59 ms |
126160 KB |
Output is correct |
16 |
Correct |
60 ms |
125936 KB |
Output is correct |
17 |
Correct |
59 ms |
125936 KB |
Output is correct |
18 |
Correct |
72 ms |
126112 KB |
Output is correct |
19 |
Correct |
63 ms |
125884 KB |
Output is correct |
20 |
Correct |
62 ms |
125980 KB |
Output is correct |
21 |
Correct |
60 ms |
126248 KB |
Output is correct |
22 |
Correct |
60 ms |
127420 KB |
Output is correct |
23 |
Correct |
62 ms |
125936 KB |
Output is correct |
24 |
Correct |
62 ms |
126716 KB |
Output is correct |
25 |
Correct |
71 ms |
127412 KB |
Output is correct |
26 |
Correct |
68 ms |
125892 KB |
Output is correct |
27 |
Correct |
80 ms |
127744 KB |
Output is correct |
28 |
Correct |
73 ms |
125996 KB |
Output is correct |
29 |
Correct |
81 ms |
128784 KB |
Output is correct |
30 |
Correct |
78 ms |
131200 KB |
Output is correct |
31 |
Correct |
68 ms |
125908 KB |
Output is correct |
32 |
Correct |
67 ms |
129096 KB |
Output is correct |
33 |
Correct |
70 ms |
131992 KB |
Output is correct |
34 |
Correct |
77 ms |
135800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
125832 KB |
Output is correct |
2 |
Correct |
62 ms |
125820 KB |
Output is correct |
3 |
Correct |
66 ms |
125816 KB |
Output is correct |
4 |
Correct |
71 ms |
125856 KB |
Output is correct |
5 |
Correct |
60 ms |
126020 KB |
Output is correct |
6 |
Correct |
62 ms |
125836 KB |
Output is correct |
7 |
Correct |
59 ms |
125836 KB |
Output is correct |
8 |
Correct |
61 ms |
125872 KB |
Output is correct |
9 |
Correct |
59 ms |
125796 KB |
Output is correct |
10 |
Correct |
60 ms |
125816 KB |
Output is correct |
11 |
Correct |
61 ms |
126080 KB |
Output is correct |
12 |
Correct |
60 ms |
126020 KB |
Output is correct |
13 |
Correct |
59 ms |
125888 KB |
Output is correct |
14 |
Correct |
60 ms |
126008 KB |
Output is correct |
15 |
Correct |
59 ms |
126160 KB |
Output is correct |
16 |
Correct |
60 ms |
125936 KB |
Output is correct |
17 |
Correct |
59 ms |
125936 KB |
Output is correct |
18 |
Correct |
72 ms |
126112 KB |
Output is correct |
19 |
Correct |
63 ms |
125884 KB |
Output is correct |
20 |
Correct |
62 ms |
125980 KB |
Output is correct |
21 |
Correct |
60 ms |
126248 KB |
Output is correct |
22 |
Correct |
60 ms |
127420 KB |
Output is correct |
23 |
Correct |
62 ms |
125936 KB |
Output is correct |
24 |
Correct |
62 ms |
126716 KB |
Output is correct |
25 |
Correct |
71 ms |
127412 KB |
Output is correct |
26 |
Correct |
68 ms |
125892 KB |
Output is correct |
27 |
Correct |
80 ms |
127744 KB |
Output is correct |
28 |
Correct |
73 ms |
125996 KB |
Output is correct |
29 |
Correct |
81 ms |
128784 KB |
Output is correct |
30 |
Correct |
78 ms |
131200 KB |
Output is correct |
31 |
Correct |
68 ms |
125908 KB |
Output is correct |
32 |
Correct |
67 ms |
129096 KB |
Output is correct |
33 |
Correct |
70 ms |
131992 KB |
Output is correct |
34 |
Correct |
77 ms |
135800 KB |
Output is correct |
35 |
Correct |
80 ms |
126228 KB |
Output is correct |
36 |
Correct |
159 ms |
156172 KB |
Output is correct |
37 |
Correct |
79 ms |
126108 KB |
Output is correct |
38 |
Correct |
103 ms |
139720 KB |
Output is correct |
39 |
Correct |
124 ms |
150428 KB |
Output is correct |
40 |
Correct |
164 ms |
165256 KB |
Output is correct |