Codeforces #341 C. Wet Shark and Flowers 概率论
来源:互联网 发布:网络数字共青团 编辑:程序博客网 时间:2024/06/03 13:40
题目
题目链接:http://codeforces.com/problemset/problem/621/C
题目来源:cf#341
简要题意:
n 个鲨鱼环形排列,每只鲨鱼的数等概率从[li,ri] 中选取。
相邻鲨鱼的数乘积为素数 p 的倍数,出题人就给两个鲨鱼每条1000 刀,求付出的钱的期望、
题解
由素数的性质可知,
p∣x⋅y 则x,y 至少一个数是p 的倍数。单个为
p 的倍数的概率posi=ri/p−(li−1)/pri−li+1 假设当前鲨鱼编号为
x ,左右的鲨鱼编号l,r 则有单点贡献期望:
前两个为expx=(posx⋅(1−posl)+posx⋅(1−posr)+posx2⋅(posl+posr))⋅2000=2000⋅posx⋅(2−posl+posr2) x 是p 倍数而相邻不是,最后一个是x 相邻都是的,由于每个位置都要算,所以除以2 即可。最终结果就是
∑i=1nexpi 。赛场上由于手误在本题上浪费了大量的时间。
代码
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#define pb push_back#define mp make_pair#define all(x) (x).begin(),(x).end()#define sz(x) ((int)(x).size())#define fi first#define se secondusing namespace std;typedef long long LL;typedef vector<int> VI;typedef pair<int,int> PII;LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}// headconst int N = 1e5+5;double pos[N];double getPos(int l, int r, int p) { int ans = ((r/p) - ((l-1)/p)); return double(ans) / (r-l+1);}double getExp(int x, int n, int p) { int l = (x==1 ? n : x-1);// 赛场上此处写成了(x==1 ? n : l-1),严重手误 int r = (x==n ? 1 : x+1);// 赛场上此处写成了(x==n ? 1 : r+1),严重手误 return pos[x] * (2.0-pos[l]/2-pos[r]/2);}int main(){ int l, r, n, p; double ans = 0.0; scanf("%d%d", &n, &p); for (int i = 1; i <= n; i++) { scanf("%d%d", &l, &r); pos[i] = getPos(l, r, p); } for (int i = 1; i <= n; i++) { ans += getExp(i, n, p); } printf("%.9f\n", ans * 1000.0 * 2); return 0;}
0 0
- Codeforces #341 C. Wet Shark and Flowers 概率论
- Codeforces 621C Wet Shark and Flowers
- CodeForces 621C Wet Shark and Flowers
- Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers
- Codeforces Round #341 (Div. 2)--C. Wet Shark and Flowers
- Codeforces Round #341 (Div. 2)C. Wet Shark and Flowers
- Codeforces #341 div2 C.Wet Shark and Flowers(概率)
- Codeforces Round #341 (Div. 2)-C. Wet Shark and Flowers
- Codeforces 621C Wet Shark and Flowers 【期望】
- 【概率与期望】[CodeForces - 621C]Wet Shark and Flowers
- CodeForces 621 C. Wet Shark and Flowers(期望)
- Codeforces 621B Wet Shark and Flowers
- Codeforces Round #341 (Div. 2)-C. Wet Shark and Flowers(区间概率)
- Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers(简单容斥)
- Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers (期望)
- Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers(期望)
- Codeforces Round #341 (Div. 2) C Wet Shark and Flowers - 期望与概率
- Codeforces Round #341 (Div. 2)C. Wet Shark and Flowers(数学期望)
- 在AngularJS应用中实现认证授权
- 阿里云服务器搭建
- WebGL 入门-WebGL简介与3D图形学
- Nodejs开发框架Express3.0开发手记–从零开始
- 六款值得推荐的android(安卓)开源框架简介
- Codeforces #341 C. Wet Shark and Flowers 概率论
- 10款实用Android UI 开发框架
- 设计模式-观察者模式
- 最火的Android开源项目整理
- Android利用V4包中的SwipeRefreshLayout实现上拉加载
- Protocol Buffer技术详解(数据编码)
- -webkit-line-clamp
- 编程王道,唯“慢”不破
- Windows Azure免费空间如何搭建PHP网站/数据库、域名绑定