#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9+7;
int n,m;
int dp[2005][2005];
int cntr[2005];
int pref[2005];
int fact[10005];
int invf[5005];
int put2[5005];
int put(int a, int exp)
{
if(exp==0)
return 1;
if(exp%2==0)
return put((a*a)%MOD,exp/2);
else
return (put((a*a)%MOD,exp/2)*a)%MOD;
}
int comb(int x, int y)
{
if(x<y)
return 0;
return ((((fact[x]*invf[y])%MOD)*invf[x-y])%MOD);
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=2*n;i++)
cntr[i%m+1]++;
fact[0]=1;
put2[0]=1;
for(int i=1;i<=5001;i++)
{
fact[i]=(fact[i-1]*i)%MOD;
put2[i]=(put2[i-1]*2LL)%MOD;
}
invf[5001] = put(fact[5001], MOD-2);
for(int i=5000;i>=0;i--)
invf[i]=(invf[i+1]*(i+1))%MOD;
dp[0][0]=1;
for(int i=1;i<=m;i++)
{
pref[i]=(pref[i-1]+cntr[i])%MOD;
for(int cntp=0;cntp<=pref[i]/2;cntp++)
{
dp[i][cntp]=0;
for(int x=0;x<=min(cntp,cntr[i]);x++)
{
dp[i][cntp] += (((((dp[i-1][cntp-x] * fact[cntr[i]])%MOD) * comb(pref[i-1] - 2*(cntp-x), x))%MOD) * (put2[cntr[i]-x] * comb(n - (pref[i-1] - 2*(cntp-x) - x) - (cntp-x) - x, cntr[i]-x)%MOD))%MOD;
dp[i][cntp] %= MOD;
}
//cout<<i<<" "<<cntp<<" "<<dp[i][cntp]<<"\n";
}
}
cout<<dp[m][n];
return 0;
}
/**
dp[i][cntp] = numarul de moduri de a plasa pietre pe primele i resturi a.i. sa fi plasat cntp perechi de pietre
cntr[i] = nr de pozitii cu restul i
pref[i] = sum(cntr[x]), x = 1..i
dp[i][cntp] = sum(dp[i-1][cntp-x] * fact[cntr[i]] * comb(pref[i-1] - 2*cntp, x) * comb(n - (pref[i-1] - 2*cntp - x) - cntp - x, cntr[i]-x)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
3 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
3 ms |
6488 KB |
Output is correct |
28 |
Correct |
5 ms |
2396 KB |
Output is correct |
29 |
Correct |
8 ms |
6724 KB |
Output is correct |
30 |
Correct |
9 ms |
10588 KB |
Output is correct |
31 |
Correct |
5 ms |
2396 KB |
Output is correct |
32 |
Correct |
8 ms |
6724 KB |
Output is correct |
33 |
Correct |
12 ms |
10588 KB |
Output is correct |
34 |
Correct |
18 ms |
14932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
2 ms |
4444 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
3 ms |
6488 KB |
Output is correct |
28 |
Correct |
5 ms |
2396 KB |
Output is correct |
29 |
Correct |
8 ms |
6724 KB |
Output is correct |
30 |
Correct |
9 ms |
10588 KB |
Output is correct |
31 |
Correct |
5 ms |
2396 KB |
Output is correct |
32 |
Correct |
8 ms |
6724 KB |
Output is correct |
33 |
Correct |
12 ms |
10588 KB |
Output is correct |
34 |
Correct |
18 ms |
14932 KB |
Output is correct |
35 |
Correct |
27 ms |
2392 KB |
Output is correct |
36 |
Correct |
63 ms |
27244 KB |
Output is correct |
37 |
Correct |
30 ms |
2396 KB |
Output is correct |
38 |
Correct |
42 ms |
14936 KB |
Output is correct |
39 |
Correct |
49 ms |
25172 KB |
Output is correct |
40 |
Correct |
78 ms |
31828 KB |
Output is correct |