#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int MOD=1e9+7;
int binPow(ll b, int p=MOD-2) {
ll res=1;
while(p>0) {
if (p&1) res=res*b%MOD;
b=b*b%MOD;
p>>=1;
}
return res;
}
vector<ll> f={1}, iF={1};
ll C(int a, int b) {
if (a<0||b<0||b>a) return 0;
assert(a<=(int)5e6);
while(f.size()<=a) {
int x=f.size();
f.push_back(f.back()*x%MOD);
iF.push_back(iF.back()*binPow(x)%MOD);
}
return f[a]*iF[b]%MOD*iF[a-b]%MOD;
}
int n;
vector<vector<ll>> dp, last;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
dp.assign(n+1, vector<ll>(2, 0));
dp[0][0]=1;
for (int i=1; i<=n; ++i) {
last=dp;
for (int j=i; j<=n; ++j) {
dp[j][1]=(dp[j][1]+(last[j-i][0]+last[j-i][1])*C(n-j+i, i))%MOD;
}
for (int j=1; j<=n; ++j) {
for (int k=1; k<=j; ++k) if (k!=i) {
dp[j][1]=(dp[j][1]+last[j-k][1]*C(n-j+k, k))%MOD;
dp[j][0]=(dp[j][0]+last[j-k][0]*C(n-j+k, k))%MOD;
}
}
}
cout << dp[n][1] << "\n";
return 0;
}
Compilation message
zapina.cpp: In function 'long long int C(int, int)':
zapina.cpp:22:16: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
22 | while(f.size()<=a) {
| ~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
294 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
65 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
388 ms |
384 KB |
Output is correct |
22 |
Correct |
44 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
83 ms |
384 KB |
Output is correct |
25 |
Correct |
56 ms |
384 KB |
Output is correct |
26 |
Correct |
112 ms |
308 KB |
Output is correct |
27 |
Correct |
556 ms |
384 KB |
Output is correct |
28 |
Correct |
564 ms |
504 KB |
Output is correct |
29 |
Correct |
567 ms |
508 KB |
Output is correct |
30 |
Correct |
571 ms |
504 KB |
Output is correct |
31 |
Correct |
577 ms |
504 KB |
Output is correct |
32 |
Correct |
583 ms |
384 KB |
Output is correct |
33 |
Correct |
588 ms |
504 KB |
Output is correct |
34 |
Correct |
597 ms |
384 KB |
Output is correct |
35 |
Correct |
640 ms |
504 KB |
Output is correct |
36 |
Correct |
614 ms |
384 KB |
Output is correct |
37 |
Correct |
598 ms |
384 KB |
Output is correct |