Codeforces 11194F (组合数学)

传送门:https://codeforces.com/contest/1194/problem/F

题意:

你有n个事件,你需要按照1~n的顺序完成这些事件,每个事件需要\(t_i\)的时间完成,你现在一共有T的时间去做这些事情,每做一件事情的时候,你有0.5的概率花费\(t_i\)的时间完成他,也有0.5的概率花费\(t_i+1\)的时间去完成他,如果在做这个事件的时候时间花完了,你就相当于没有做成这个事件,现在问你在T的时间内完成的事件的个数的期望是多少

题解:

读完题后想到的应该是一个概率dp,但是T有\(2e14\)的大小,我们现在着手于如何优化他

首先,我的每一个事件都只有两种情况,如果是情况1 ,就按照原本的时间去完成他,如果是情况2,我就要在原本的时间的基础上加上一个1,那么我完成k个事件时,我始终是要花费sum[k]的时间去做这些事件的,这里的sum是t的前缀和

所以我们其实不用管时间这个限制条件了,我们只需要得到,前i个事件中我有多少个事件需要花费+1的这个时间去完成他的事件的个数,那么这样我们就变成了一个组合数的问题了

我们可以得到在我们完成k个事件时,有i个事件出现了+1的情况

那么这个概率就是\((\frac{1}{2})^k*C_k^{i}\)这个就很容易理解,如果求期望那么乘上一个k就可以了

那么我总的期望就是\(\sum_{i=1}^{posmax}(\frac{1}{2})^(posmax)*C_k^{i}*posmax\)

posmax表示,我当前最多可以完成的事件

我们用一个副本posmaxtmp来表示posmax完成这些事件的时间

那么在循环中posmaxtmp每次+1时就可以使得完成一个事件的时间+1

如果posmaxtmp大于T了,我们后面的事件就完不成,posmax就得往前走,这里需要处理一下下

并且,当posmaxtmp大于T时,我也需要算出这一部分的期望即完成posmax-1个事件时对答案的贡献

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
typedef long long ll; ll F[maxn], invF[maxn];
ll power(ll a, ll b) {
ll ret = 1;
while(b) {
if(b & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ret;
}
void init() {
F[0] = 1;
for(int i = 1; i < maxn; i++) {
F[i] = (F[i - 1] * i) % mod;
}
invF[maxn - 1] = power(F[maxn - 1], mod - 2);
for(int i = maxn - 2; i >= 0; i--) {
invF[i] = invF[i + 1] * (i + 1) % mod;
}
} ll C(int n, int m) {
if(n < 0 || m < 0 || m > n) return 0;
if(m == 0 || m == n) return 1;
return F[n] * invF[n - m] % mod * invF[m] % mod;
}
LL t[maxn];
LL sum[maxn]; int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
IO;
init();
int n;
LL T;
cin >> n >> T;
for(int i = 1; i <= n; i++) {
cin >> t[i];
sum[i] = sum[i - 1] + t[i];
}
// debug1(T);
int posmax = n; for(int i = 1; i <= n; i++) {
// debug1(sum[i]);
if(sum[i] > T) {
posmax = i - 1;
break;
}
} // debug1(posmax);
int num;
int inv2 = invF[2];
LL res = 0;
LL posmaxtmp = sum[posmax] - 1;
// debug1(inv2);
for(int i = 0; i <= posmax; i++) {
++posmaxtmp;
// debug3(i, posmaxtmp, C(posmax, i));
// debug1(posmax);
while(posmaxtmp > T) {
res += power(inv2, posmax) * C(posmax - 1, i - 1) % mod * (posmax - 1) % mod;
res %= mod;
posmaxtmp -= t[posmax];
--posmax;
}
if(i > posmax) break;
res += power(inv2, posmax) % mod * C(posmax, i) % mod * posmax % mod;
res %= mod;
}
cout << res << endl; return 0;
}

codeforces 1194F (组合数学)的更多相关文章

  1. Codeforces - 1194F - Crossword Expert - 组合数学

    https://codeforc.es/contest/1194/problem/F 下面是错的. 看起来有点概率dp的感觉? 给你T秒钟时间,你要按顺序处理总共n个事件,每个事件处理花费的时间是ti ...

  2. CodeForces 407C 组合数学(详解)

    题面: http://codeforces.com/problemset/problem/407/C 一句话题意:给一个长度为n的序列g,m次操作,每次操作(l,r,k)表示将g[l]~g[r]的每个 ...

  3. Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学

    — This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...

  4. Codeforces 722E 组合数学 DP

    题意:有一个n * m的棋盘,你初始在点(1, 1),你需要去点(n, m).你初始有s分,在这个棋盘上有k个点,经过一次这个点分数就会变为s / 2(向上取整),问从起点到终点的分数的数学期望是多少 ...

  5. Crossword Expert CodeForces - 1194F (期望)

    大意: $n$个题, 按照第$i$题随机$t_i$或$t_i+1$秒钟完成, 最多做$T$秒, 求做题数期望. 期望转为做题数$\ge x$的方案数之和最后再除以总方案数 这是因为$\sum\limi ...

  6. Codeforces 1194F. Crossword Expert

    传送门 考虑每一个位置的期望贡献 $P[i]$ 对于第 $k$ 个位置,设 $sum=\sum_{i=1}^{k}t[k]$,那么 $T-sum$ 即为用最短时间完成完位置 $k$ 后多出来的空闲时间 ...

  7. Codeforces 1172B(组合数学)

    题面 给出一棵n个点的树,要求把它画在圆上,且边不相交,画法与排列一一对应(即旋转后相同的算不同种),求方案数.如下图是4个点的树\(T:V=\{1,2,3,4\},E=\{(1,2),(1,3),( ...

  8. codeforces 932E Team Work(组合数学、dp)

    codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...

  9. Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...

随机推荐

  1. closest和parents方法区别

    今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...

  2. Directx11学习笔记【二十一】 封装键盘鼠标响应类

    原文:Directx11学习笔记[二十一] 封装键盘鼠标响应类 摘要: 本文由zhangbaochong原创,转载请注明出处:http://www.cnblogs.com/zhangbaochong/ ...

  3. BZOJ 1834网络扩容题解

    一道不算太难的题目 但是真的很恶心 显然,对于第一问,我们直接无脑打模板就好了 第二问也不是很难,我们将每条边再连一条容量为inf,费用为w的边 但是流量只要小于第一问的答案加k就行了 所以我们增加一 ...

  4. poj 2828【线段树 单点更新】

    POJ 2828 还是弱啊.思维是个好东西... 刚开始想来想去用线段树存人的话不仅超时,而且存不下...居然是存空位! sum[]数组存这个序列空位个数,然后逆序遍历.逆序好理解,毕竟最后一个人插进 ...

  5. Mysql 查询一天中,每个小时数据的数量

    SELECT HOUR(e.time) as Hour,count(*) as Count FROM error_log e WHERE e.date = '2017-09-02' GROUP BY ...

  6. init()方法必须使用super.init(config)的原因--Servlet

    原 因: 一个servlet在它的init()方法中传递它的ServletConfig实例,在其他的方法中却不可以.当一个servlet在 init()方法外需要调用config对象时就会产生问题.使 ...

  7. 设置select和option的文字居中

    select{ width: auto;      padding: 0 1%; //左右一定要设置      margin: 0; } option{ text-align:center; }

  8. 【Flask源码分析——请求上下文与应用上下文】

    Flask中有两种上下文,请求上下文和应用上下文.两者的作用域都处于一个请求的局部中. 查看源代码,上下文类在flask.ctx模块中定义 AppContext类定义应用上下文,app是当前应用Web ...

  9. 洛谷P2455 [SDOI2006]线性方程组

    高斯消元模板 要求输出解的情况(无穷解/无解) 1. 之前写的丑陋代码 #include <iostream> #include <cstdio> #include <c ...

  10. 2018-8-10-添加右键使用-SublimeText-打开

    title author date CreateTime categories 添加右键使用 SublimeText 打开 lindexi 2018-08-10 19:16:52 +0800 2018 ...