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 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); 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 < ) 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 = ;
while(b) {
if(b & ) ans = 1LL * ans * a % mod;
a = 1LL * a * a % mod; b >>= ;
}
return ans;
} int inv[N], Finv[N], F[N];
void init() {
inv[] = F[] = Finv[] = ;
for(int i = ; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
for(int i = ; i < N; i++) F[i] = 1LL * F[i - ] * i % mod;
for(int i = ; i < N; i++) Finv[i] = 1LL * Finv[i - ] * inv[i] % mod;
}
int comb(int n, int m) {
if(n < m || n < ) return ;
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 - ] == prefix[R]) return power(, R - L);
int p, q;
for(p = L; p <= R; p++) if(a[p] != ) break;
for(q = R; q >= L; q--) if(a[q] != ) break;
if(p == q && a[p] == ) return comb(R - L, p - L);
int ans = ;
if(a[p] >= a[q]) {
int L2 = p, R2 = L2 + a[p] - ;
if(R2 >= q && R2 <= R) add(ans, 1LL * solve(L2 + , R2) * comb(R - L - R2 + L2, L2 - L) % mod);
}
if(a[q] >= a[p]) {
int R2 = q, L2 = R2 - a[q] + ;
if(L2 >= L && L2 <= p) add(ans, 1LL * solve(L2, R2 - ) * comb(R - L - R2 + L2, L2 - L) % mod);
}
return ans;
} int main() {
init();
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= n; i++) prefix[i] = prefix[i - ] + (a[i] != );
printf("%d\n", solve(, n));
return ;
} /*
*/

Codeforces 380D Sereja and Cinema (看题解)的更多相关文章

  1. Codeforces 1017F The Neutral Zone (看题解)

    这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces 513E2 Subarray Cuts dp (看题解)

    我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...

  3. Codeforces 822E Liar dp + SA (看题解)

    Liar 刚开始感觉只要开个dp[ i ][ j ][ 0 / 1 ]表示处理了s的前 i 个用了 k 段, i 是否是最后一段的最后一个字符 的 t串最长匹配长度, 然后wa24, 就gg了.感觉这 ...

  4. Codeforces 196E Opening Portals MST (看题解)

    Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...

  5. Codeforces 725E Too Much Money (看题解)

    Too Much Money 最关键的一点就是这个贪心可以在sqrt(n)级别算出答案. 因为最多有sqrt(n)个不同的数值加入. 我们可以发现最优肯定加入一个. 然后维护一个当前可以取的最大值, ...

  6. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  7. codeforces 425C Sereja and Two Sequences(DP)

    题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...

  8. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

  9. Codeforces Round #668 (Div. 2)A-C题解

    A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...

随机推荐

  1. ASP.NET MVC5高级编程 之 路由

    每个ASP.NET MVC应用程序都需要路由来定义自己处理请求的方式.路由是MVC应用程序的入口点.路由的核心工作是将一个请求映射到一个操作 路由主要有两种用途: 匹配传入的请求(该请求不匹配服务器文 ...

  2. [Linux]php+apache 和 php+nginx的区别

    apache是通过mod_php来解析php nginx是通过php-fpm(fast-cgi)来解析php1. PHP 解释器是否嵌入 Web 服务器进程内部执行mod_php 通过嵌入 PHP 解 ...

  3. C语言-社保工资查询系统

    一.简述 此次程序没有涉及函数,完成工资.保险和住房公积金税前税后的查询.工资和社保公积金算法是依据最新的北京标准计算. 五险一金标准: 税率: 1.输入编号1~6查询保险,然后再选择是依据税前工资还 ...

  4. Modbus库开发笔记:Modbus ASCII Slave开发

    与Modbus RTU在串行链路上分为Slave和Master一样,Modbus ASCII也分为Slave和Master,这一节我们就来开发Slave.对于Modbus ASCII从站来说,需要实现 ...

  5. Oracle 高水位说明和释放表空间,加快表的查询速度

    高水位的介绍 数据库运行了一段时间,经过一些列的删除.插入.更改操作有些表的高水位线就有可能和实际的表存储数据的情况相差特别多,为了提高检索该表的效率,建议对这些表进行收缩: 查找高水位线的表 查找表 ...

  6. oracle数据库定时任务dbms_job的用法详解

    本文来源:Ruthless <oracle数据库定时任务dbms_job的用法详解> 一.dbms_job涉及到的知识点   1.创建job: variable jobno number; ...

  7. |"|&|<|>等html字符转义

    本文来源:d4shman  <&nbsp|&quot|&amp|&lt|&gt等html字符转义> 提示:请直接按CTRL+F搜索您要查找的转义字符 ...

  8. extjs中store的reload事件异步问题解决

    转载自:http://blog.sina.com.cn/s/blog_8f8b7fc10100zd75.html store0.reload({params:{start:0, limit:10}}) ...

  9. 【ES】学习10-聚合3

    聚合是在查询匹配的文档中做统计的 不指定查询语句时,从所有文档中匹配. 下面两个语句等价: GET /cars/transactions/_search { , "aggs" : ...

  10. HTML&javaSkcript&CSS&jQuery&ajax(十)

    HTML 1.SVG直接嵌入HTML网页 ,SVG 是使用XML描述2D图像的语言,Canvas通过JavaScript来绘制2D <svg xmlns="http://www.w3. ...