Sereja and Cinema

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

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

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. #define LD long double
  4. #define fi first
  5. #define se second
  6. #define mk make_pair
  7. #define PLL pair<LL, LL>
  8. #define PLI pair<LL, int>
  9. #define PII pair<int, int>
  10. #define SZ(x) ((int)x.size())
  11. #define ull unsigned long long
  12.  
  13. using namespace std;
  14.  
  15. const int N = 1e5 + ;
  16. const int inf = 0x3f3f3f3f;
  17. const LL INF = 0x3f3f3f3f3f3f3f3f;
  18. const int mod = 1e9 + ;
  19. const double eps = 1e-;
  20. const double PI = acos(-);
  21.  
  22. template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
  23. template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
  24. template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
  25. template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}
  26.  
  27. int power(int a, int b) {
  28. int ans = ;
  29. while(b) {
  30. if(b & ) ans = 1LL * ans * a % mod;
  31. a = 1LL * a * a % mod; b >>= ;
  32. }
  33. return ans;
  34. }
  35.  
  36. int inv[N], Finv[N], F[N];
  37. void init() {
  38. inv[] = F[] = Finv[] = ;
  39. for(int i = ; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
  40. for(int i = ; i < N; i++) F[i] = 1LL * F[i - ] * i % mod;
  41. for(int i = ; i < N; i++) Finv[i] = 1LL * Finv[i - ] * inv[i] % mod;
  42. }
  43. int comb(int n, int m) {
  44. if(n < m || n < ) return ;
  45. return 1LL * F[n] * Finv[m] % mod * Finv[n - m] % mod;
  46. }
  47.  
  48. int n, a[N], prefix[N];
  49.  
  50. int solve(int L, int R) {
  51. if(prefix[L - ] == prefix[R]) return power(, R - L);
  52. int p, q;
  53. for(p = L; p <= R; p++) if(a[p] != ) break;
  54. for(q = R; q >= L; q--) if(a[q] != ) break;
  55. if(p == q && a[p] == ) return comb(R - L, p - L);
  56. int ans = ;
  57. if(a[p] >= a[q]) {
  58. int L2 = p, R2 = L2 + a[p] - ;
  59. if(R2 >= q && R2 <= R) add(ans, 1LL * solve(L2 + , R2) * comb(R - L - R2 + L2, L2 - L) % mod);
  60. }
  61. if(a[q] >= a[p]) {
  62. int R2 = q, L2 = R2 - a[q] + ;
  63. if(L2 >= L && L2 <= p) add(ans, 1LL * solve(L2, R2 - ) * comb(R - L - R2 + L2, L2 - L) % mod);
  64. }
  65. return ans;
  66. }
  67.  
  68. int main() {
  69. init();
  70. scanf("%d", &n);
  71. for(int i = ; i <= n; i++) scanf("%d", &a[i]);
  72. for(int i = ; i <= n; i++) prefix[i] = prefix[i - ] + (a[i] != );
  73. printf("%d\n", solve(, n));
  74. return ;
  75. }
  76.  
  77. /*
  78. */

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. HBase的replication原理及部署

    一.hbase replication原理 hbase 的复制方式是 master-push 方式,即主集群推的方式,主要是因为每个rs都有自己的WAL. 一个master集群可以复制给多个从集群,复 ...

  2. 分布式服务管理框架 ZooKeeper

      核心功能   统一命名服务(Name Service)   通过有层次的目录结构产生唯一的名称,同时可以将名称关联到特定资源   配置管理(Configuration Management)   ...

  3. [C][代码实例]交换指向常量的二级指针的位置

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ...

  4. 异步io模块

    '''客户端''' import socket,select #####进行封装,将socket对象和要返回的url,和回调函数进行一下封装 class Httprequest(): def __in ...

  5. GridView的stretchMode属性

    stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充剩余空间.参数stretchMode 可选值为:none,spacingWidth,columnWidth, spa ...

  6. flask 面试题

    1,什么是Flask,有什么优点?概念解释Flask是一个Web框架,就是提供一个工具,库和技术来允许你构建一个Web应用程序.这个Web应用程序可以是一些Web页面,博客,wiki,基于Web的日里 ...

  7. 微信小程序简单介绍 一

    一 组件及api网址: 组件 :https://developers.weixin.qq.com/miniprogram/dev/component/view.html api:https://dev ...

  8. SpringMVC文件下载与JSON格式

    点击查看上一章 现在JSON这种数据格式是被使用的非常的广泛的,SpringMVC作为目前最受欢迎的框架,它对JSON这种数据格式提供了非常友好的支持,可以说是简单到爆. 在我们SpringMVC中只 ...

  9. noip 2018.10.14 模拟赛 砍树

    数学问题... 根据题意,有: 移项,整理,得: 记 于是 那么 可以看到,最多只会有2*个取值(显而易见) 于是对应的,可能产生效果的d也只会有个,于是我们把他们找出来,扔进一个数组里然后排序,去重 ...

  10. flower

    问题 D: flower 时间限制: 1 Sec  内存限制: 128 MB 题目描述 又是一年教师节,有n个小朋友打算去看望花老师.去看花老师当然要买花咯,不过某些土(xue)豪(ba)买了太多的花 ...