This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 66, mod = 1e9 + 7;
int n, h[maxn][maxn][2], dp[maxn][maxn], a[maxn], pa[maxn], fact[maxn], inv[maxn];
inline void add(int &a, int b) {
a = a+b > mod ? a+b-mod : a+b;
}
inline int mul(int a, int b) {
return a*1ll*b%mod;
}
int bp(int a, int p) {
int r = 1;
while(p) {
if(p&1) r = mul(a, r);
a = mul(a, a), p>>=1;
}
return r;
}
void setup() {
for(int i = 0; i < maxn; i++)
if(i < 2) fact[i] = inv[i] = 1;
else fact[i] = i, inv[i] = mod - mul(mod/i, inv[mod%i]);
for(int* a : {fact, inv})
for(int i = 2; i < maxn; i++)
a[i] = mul(a[i-1], a[i]);
}
inline int nck(int n, int k) {
return n < 0 || k > n ? 0 : mul(fact[n], mul(inv[n-k], inv[k]));
}
inline int A(int n, int k) {
return n < 0 || k > n ? 0 : mul(fact[n], inv[n-k]);
}
void calch() {
h[0][0][0] = 1;
for(int i = 0; i < n; i++)
for(int j = i; j <= n; j++) {
for(int f = 0; f < 2; f++) {
add(h[i+1][j][f], mul(h[i][j][f], inv[2]));
add(h[i+1][j+1][f], h[i][j][f]);
add(h[i+1][j+2][f], mul(h[i][j][f], inv[2]));
}
add(h[i+1][j][1], mul(h[i][j][0], inv[2]));
add(h[i+1][j+1][1], mul(h[i][j][0], inv[2]));
}
}
inline int prod(int l, int r) {
if(r < l) return 1;
if(l <= 0) return 0;
return mul(fact[r], inv[l-1]);
}
inline int contrib(int lst, int i) {
int N = lst - (i>1?pa[i-2]:0);
return prod(N-a[i-1]+1, N);
}
void calcdp() {
dp[0][0] = 1;
for(int i = 0; i < n; i++)
for(int lst = 0; lst <= i; lst++) {
//if(dp[i][lst]) cout << i << " " << lst << " " << dp[i][lst] << " IS BEING PROCESSED\n";
if(!dp[i][lst]) continue;
int t = 1, q, v;
for(int ni = i+1; ni <= n; ni++) {
for(int nlst = lst+1; nlst <= n; nlst++) {
int dif = nlst-lst-1;
q = mul(t, contrib(nlst, ni));
//cout << q << '\n';
q = mul(q, mul(h[dif][dif][0] + h[dif][dif][1], A(ni-lst-1, dif)));
//cout << dif << " " << h[dif][dif][0] << " / " << h[dif][dif][1] << " / " << A(ni-lst-1, dif) << " " << ni-lst-1 << " " << dif << '\n';
//cout << ni << " " << nlst << " += " << q << '\n';
add(dp[ni][nlst], mul(q, dp[i][lst]));
}
//cout << ni << " dsalkjfslak " << contrib(lst, ni) << '\n';
t = mul(t, contrib(lst, ni));
if(t == 0) break;
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
setup();
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
reverse(a, a+n);
if(a[0] != 2*n) return cout << 0, 0;
for(int i = 0; i < n; i++) a[i] = a[i] - a[i+1] - 1;
pa[0] = a[0];
for(int i = 1; i < n; i++) pa[i] = pa[i-1] + a[i];
calch();
calcdp();
//cout << h[1][1][0] << " / " << h[1][1][1] << '\n';
//cout << h[1][2][0] << " / " << h[1][2][1] << '\n';
//cout << h[2][2][0] << " / " << h[2][2][1] << " " << contrib(3, 3) << '\n';
//cout << h[2][3][0] << " / " << h[2][3][1] << '\n';
//cout << h[2][4][0] << " / " << h[2][4][1] << '\n';
cout << dp[n][n] << '\n';
}
Compilation message (stderr)
ruins3.cpp: In function 'void calcdp()':
ruins3.cpp:62:18: warning: unused variable 'v' [-Wunused-variable]
int t = 1, q, v;
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |