Codeforces 380D 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 (看题解)的更多相关文章
- Codeforces 1017F The Neutral Zone (看题解)
这题一看就筛质数就好啦, 可是这怎么筛啊, 一看题解, 怎么会有这么骚的操作. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces 513E2 Subarray Cuts dp (看题解)
我们肯定要一大一小间隔开来所以 把式子拆出来就是类似这样的形式 s1 - 2 * s2 + 2 * s3 + ...... + sn 然后把状态开成四个, 分别表示在顶部, 在底部, 在顶部到底部的中 ...
- Codeforces 822E Liar dp + SA (看题解)
Liar 刚开始感觉只要开个dp[ i ][ j ][ 0 / 1 ]表示处理了s的前 i 个用了 k 段, i 是否是最后一段的最后一个字符 的 t串最长匹配长度, 然后wa24, 就gg了.感觉这 ...
- Codeforces 196E Opening Portals MST (看题解)
Opening Portals 我们先考虑如果所有点都是特殊点, 那么就是对整个图求个MST. 想在如果不是所有点是特殊点的话, 我们能不能也 转换成求MST的问题呢? 相当于我们把特殊点扣出来, 然 ...
- Codeforces 725E Too Much Money (看题解)
Too Much Money 最关键的一点就是这个贪心可以在sqrt(n)级别算出答案. 因为最多有sqrt(n)个不同的数值加入. 我们可以发现最优肯定加入一个. 然后维护一个当前可以取的最大值, ...
- Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...
- codeforces 425C Sereja and Two Sequences(DP)
题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- Codeforces Round #668 (Div. 2)A-C题解
A. Permutation Forgery 题目:http://codeforces.com/contest/1405/problem/A 题解:这道题初看有点吓人,一开始居然想到要用全排序,没错我 ...
随机推荐
- (常用)os模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cdos.curdi ...
- zookeeper权限问题
对cdrwa的说明如上,网上一些博客乱写误导 参考链接:http://zookeeper.apache.org/doc/r3.1.2/zookeeperProgrammers.html 这是在使用zk ...
- Freemaker:操作集合
<#if (id?index_of('Base') >= 0)> <choose> <when test="rootOrgID !=null and ro ...
- Ex 2_14 去掉数组中所有重复的元素..._第二次作业
首先利用归并排序算法对数组进行排序,时间复杂度为O(nlogn),接着再利用时间复杂度为O(n) 的去重复算法去掉数组中的重复元素.总的时间复杂度为O(nlogn). (这题应该用分支算法解决)以下为 ...
- [PHP]session回收机制及php.ini session生命期
由于PHP的工作机制,它并没有一个daemon线程,来定时地扫描session信息并判断其是否失效.当一个有效请求发生时,PHP会根据全局变量 session.gc_probability/sessi ...
- jQuery-lazyload参数
1.placeholder placeholder:'图片地址',用图片提前占据将要加载的图片的位置. 2.effect effect:‘fadeIn’ 表示用何种效果载入,eg:show()直接加载 ...
- swift 学习- 19 -- 可选链式调用
// 可选链式调用 是一种在当前值可能为 nil 的可选值上请求 和 调用属性, 方法以及下标, 如果 可选值有值, 那么调用就会成功, 如果可选值是 nil, 那么就会将返回 nil , // 多个 ...
- FTP判断ftp上是否有文件目录,没有就创建的具体案例
/// <summary> /// 判断ftp上是否有指定的文件目录,没有创建 /// </summary> /// <param name="ftpPath& ...
- setenforce: SELinux is disabled解决办法
如果在使用setenforce命令设置selinux状态的时候出现这个提示:setenforce: SELinux is disabled 那么说明selinux已经被彻底的关闭了 如果需要重新开启s ...
- 使用open live writer客户端写博客
注:Windows Live Writer 已经停止更新,建议安装 Open Live Writer,下载地址: http://openlivewriter.org/ 使用open live writ ...