#include <bits/stdc++.h>
#define ll long long
const ll MOD = 1e9+7LL;
const int MAX = 4010;
using namespace std;
int N, M;
int qtdMod[MAX], moduloHere[MAX];
ll dp[MAX][MAX][2]; //flag pra falar se devo escolher livres ou duplas
ll bin[MAX][MAX];
ll fat[MAX];
void calcBin(){
bin[0][0] = 1;
for(int i = 1; i <= 2*N; i++){
bin[i][0]=1;
for(int j = 1; j <= 2*N; j++){
bin[i][j] = (bin[i-1][j]+bin[i-1][j-1]);
if(bin[i][j] >= MOD)
bin[i][j] -= MOD;
}
}
}
void calcFat(){
fat[0] = 1;
for(int i = 1; i <= 2*N; i++)
fat[i] = (fat[i-1] * i) % MOD;
}
int getQuantity(int x){
return qtdMod[moduloHere[x]%M]-x;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
calcBin();
calcFat();
for(int i = 0; i < 2*N; i++)
qtdMod[i%M]++;
int ant = 0;
for(int i = 0; i < M; i++){
for(int q = qtdMod[i]; q > 0; q--,ant++ )
moduloHere[ant] = i;
}
for(int i = 1; i < M; i++)
qtdMod[i] += qtdMod[i-1];
dp[2*N][0][0] = dp[2*N][0][1] = 1;
for(int i = 2*N-1; i >= 0; i--)
for(int j = 0; j <= min(i,N); j++){
int foram = ((i-j)>>1)+j;
if(foram > N)
continue;
int faltam = N-foram;
int qtdModLeft = getQuantity(i);
if(faltam >= qtdModLeft){
ll jeito = (bin[faltam][qtdModLeft] * fat[qtdModLeft] ) % MOD;
jeito *= ( (1LL<<qtdModLeft) ) % MOD;
jeito %= MOD;
ll qMod = qtdMod[moduloHere[i]]-(moduloHere[i] == 0 ? 0 : qtdMod[moduloHere[i]-1]);
qMod = bin[qMod][qtdModLeft];
(jeito *= qMod) %= MOD;
dp[i][j][1] = jeito * dp[qtdMod[moduloHere[i]]][j+qtdModLeft][0];
dp[i][j][1] %= MOD;
}
dp[i][j][0] = dp[i][j][1];
if(j){
dp[i][j][0] += ( dp[i+1][j-1][0] * j ) % MOD;
if(dp[i][j][0] >= MOD)
dp[i][j][0] -= MOD;
}
}
cout << dp[0][0][0] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 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 |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
1608 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1736 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1620 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
1608 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1736 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1620 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
4308 KB |
Output is correct |
21 |
Correct |
3 ms |
3924 KB |
Output is correct |
22 |
Correct |
5 ms |
8020 KB |
Output is correct |
23 |
Correct |
7 ms |
6868 KB |
Output is correct |
24 |
Correct |
9 ms |
9300 KB |
Output is correct |
25 |
Correct |
5 ms |
7496 KB |
Output is correct |
26 |
Correct |
5 ms |
7636 KB |
Output is correct |
27 |
Correct |
7 ms |
9428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
1608 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1736 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1620 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
4308 KB |
Output is correct |
21 |
Correct |
3 ms |
3924 KB |
Output is correct |
22 |
Correct |
5 ms |
8020 KB |
Output is correct |
23 |
Correct |
7 ms |
6868 KB |
Output is correct |
24 |
Correct |
9 ms |
9300 KB |
Output is correct |
25 |
Correct |
5 ms |
7496 KB |
Output is correct |
26 |
Correct |
5 ms |
7636 KB |
Output is correct |
27 |
Correct |
7 ms |
9428 KB |
Output is correct |
28 |
Incorrect |
23 ms |
28884 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
2388 KB |
Output is correct |
12 |
Correct |
1 ms |
1608 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1736 KB |
Output is correct |
16 |
Correct |
2 ms |
2132 KB |
Output is correct |
17 |
Correct |
1 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1620 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
4308 KB |
Output is correct |
21 |
Correct |
3 ms |
3924 KB |
Output is correct |
22 |
Correct |
5 ms |
8020 KB |
Output is correct |
23 |
Correct |
7 ms |
6868 KB |
Output is correct |
24 |
Correct |
9 ms |
9300 KB |
Output is correct |
25 |
Correct |
5 ms |
7496 KB |
Output is correct |
26 |
Correct |
5 ms |
7636 KB |
Output is correct |
27 |
Correct |
7 ms |
9428 KB |
Output is correct |
28 |
Incorrect |
23 ms |
28884 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |