//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo m,n;
const llo mod=1e9+7;
llo ee(llo aa,llo bb){
if(bb==0){
return 1;
}
llo ac=ee(aa,bb/2);
ac=(ac*ac)%mod;
if(bb%2==1){
ac=(ac*aa)%mod;
}
return ac;
}
llo fac[4002];
llo fac2[4002];
llo cal(llo x,llo y){
llo cur=(fac[x]*fac2[y])%mod;
cur=(cur*fac2[x-y])%mod;
return cur;
}
llo dp[2001][2001];
llo co[2001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fac[0]=1;
llo ma=4000;
for(llo i=1;i<=ma;i++){
fac[i]=(fac[i-1]*i)%mod;
//cout<<fac[i]<<":";
}
// cout<<endl;
for(llo i=ma;i>=0;i--){
if(i==ma){
fac2[i]=ee(fac[i],mod-2);
}
else{
fac2[i]=(fac2[i+1]*(i+1))%mod;
}
//cout<<fac2[i]<<":";
}
cin>>n>>m;
for(llo j=0;j<2*n;j++){
co[(j%m)+1]++;
}
dp[0][0]=1;
for(llo i=1;i<=m;i++){
for(llo j=0;j<=n;j++){
llo mi=max((j-(co[i]/2)),(llo)0);
for(llo k=j;k>=mi;k--){
llo cur=cal(co[i],j-k)*cal(co[i]-(j-k),j-k);
cur%=mod;
cur=(cur*fac[j-k])%mod;
dp[i][j]=(dp[i][j]+dp[i-1][k]*cur)%mod;
}
}
}
llo ans=fac[2*n];
//cout<<ans<<":"<<endl;
for(llo i=1;i<=n;i++){
llo cur=dp[m][i];
//cout<<i<<":"<<dp[m][i]<<endl;
cur=(cur*cal(n,i))%mod;
cur=(cur*fac[i])%mod;
cur=(cur*fac[2*n-2*i])%mod;
//cout<<i<<","<<cur<<endl;
if(i%2==1){
//cout<<11<<endl;
ans=(ans-cur+mod)%mod;
}
else{
ans=(ans+cur)%mod;
}
//cout<<ans<<endl;
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
388 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
388 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
388 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
3 ms |
1868 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
4 ms |
1228 KB |
Output is correct |
25 |
Correct |
4 ms |
1776 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
6 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
388 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
3 ms |
1868 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
4 ms |
1228 KB |
Output is correct |
25 |
Correct |
4 ms |
1776 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
6 ms |
2252 KB |
Output is correct |
28 |
Correct |
5 ms |
460 KB |
Output is correct |
29 |
Correct |
11 ms |
3340 KB |
Output is correct |
30 |
Correct |
16 ms |
5588 KB |
Output is correct |
31 |
Correct |
6 ms |
460 KB |
Output is correct |
32 |
Correct |
13 ms |
3532 KB |
Output is correct |
33 |
Correct |
21 ms |
6476 KB |
Output is correct |
34 |
Correct |
29 ms |
10224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
388 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
640 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
3 ms |
1868 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
4 ms |
1228 KB |
Output is correct |
25 |
Correct |
4 ms |
1776 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
6 ms |
2252 KB |
Output is correct |
28 |
Correct |
5 ms |
460 KB |
Output is correct |
29 |
Correct |
11 ms |
3340 KB |
Output is correct |
30 |
Correct |
16 ms |
5588 KB |
Output is correct |
31 |
Correct |
6 ms |
460 KB |
Output is correct |
32 |
Correct |
13 ms |
3532 KB |
Output is correct |
33 |
Correct |
21 ms |
6476 KB |
Output is correct |
34 |
Correct |
29 ms |
10224 KB |
Output is correct |
35 |
Correct |
35 ms |
628 KB |
Output is correct |
36 |
Correct |
97 ms |
26692 KB |
Output is correct |
37 |
Correct |
34 ms |
544 KB |
Output is correct |
38 |
Correct |
65 ms |
13084 KB |
Output is correct |
39 |
Correct |
82 ms |
23920 KB |
Output is correct |
40 |
Correct |
134 ms |
31664 KB |
Output is correct |