제출 #246277

#제출 시각아이디문제언어결과실행 시간메모리
246277kostia244유적 3 (JOI20_ruins3)C++17
100 / 100
767 ms6752 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 666, mod = 1e9 + 7; int n, h[maxn][maxn][2], dp[maxn][maxn], pre[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++) { int q; for(int olst = 0; olst < lst; olst++) { int dif = lst-olst-1; q = pre[i][olst]; q = mul(q, mul(h[dif][dif][0] + h[dif][dif][1], A(i-olst-1, dif))); q = mul(q, contrib(lst, i)); add(dp[i][lst], q); } if(i == n || !dp[i][lst]) continue; q = 1; for(int ni = i+1; ni <= n; ni++) { add(pre[ni][lst], mul(q, dp[i][lst])); q = mul(q, contrib(lst, ni)); } } } 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 << dp[n][n] << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...