zoukankan      html  css  js  c++  java
  • Codeforces 380D Sereja and Cinema (看题解)

    Sereja and Cinema

    首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边。

    难点在于计算方案数。。 我们可以从外往里把确定的人用组合数算上去,然后缩小范围。

    #include<bits/stdc++.h>
    #define LL long long
    #define LD long double
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    
    using namespace std;
    
    const int N = 1e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
    template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
    template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
    template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
    
    int power(int a, int b) {
        int ans = 1;
        while(b) {
            if(b & 1) ans = 1LL * ans * a % mod;
            a = 1LL * a * a % mod; b >>= 1;
        }
        return ans;
    }
    
    int inv[N], Finv[N], F[N];
    void init() {
        inv[1] = F[0] = Finv[0] = 1;
        for(int i = 2; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
        for(int i = 1; i < N; i++) F[i] = 1LL * F[i - 1] * i % mod;
        for(int i = 1; i < N; i++) Finv[i] = 1LL * Finv[i - 1] * inv[i] % mod;
    }
    int comb(int n, int m) {
        if(n < m || n < 0) return 0;
        return 1LL * F[n] * Finv[m] % mod * Finv[n - m] % mod;
    }
    
    int n, a[N], prefix[N];
    
    int solve(int L, int R) {
        if(prefix[L - 1] == prefix[R]) return power(2, R - L);
        int p, q;
        for(p = L; p <= R; p++) if(a[p] != 0) break;
        for(q = R; q >= L; q--) if(a[q] != 0) break;
        if(p == q && a[p] == 1) return comb(R - L, p - L);
        int ans = 0;
        if(a[p] >= a[q]) {
            int L2 = p, R2 = L2 + a[p] - 1;
            if(R2 >= q && R2 <= R) add(ans, 1LL * solve(L2 + 1, R2) * comb(R - L - R2 + L2, L2 - L) % mod);
        }
        if(a[q] >= a[p]) {
            int R2 = q, L2 = R2 - a[q] + 1;
            if(L2 >= L && L2 <= p) add(ans, 1LL * solve(L2, R2 - 1) * comb(R - L - R2 + L2, L2 - L) % mod);
        }
        return ans;
    }
    
    int main() {
        init();
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++) prefix[i] = prefix[i - 1] + (a[i] != 0);
        printf("%d
    ", solve(1, n));
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    初识Java内存结构
    eclipse的安装与配置
    关于android客户端使用ksoap2调用wcf(.svc)的总结
    ie下jpg不显示问题
    Android学习笔记(1)
    HTML标签语义化
    【转】android模拟机不能上网
    WCF IIS 寄宿问题
    C# 参数传递
    Wcf IIS 寄宿
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10715785.html
Copyright © 2011-2022 走看看