Codeforces #341 C. Wet Shark and Flowers 概率论

题目

题目链接:http://codeforces.com/problemset/problem/621/C

题目来源:cf#341

简要题意: n 个鲨鱼环形排列,每只鲨鱼的数等概率从[li,ri]中选取。
     相邻鲨鱼的数乘积为素数 p 的倍数,出题人就给两个鲨鱼每条1000刀,求付出的钱的期望、

题解

由素数的性质可知, pxy x,y 至少一个数是 p 的倍数。

单个为p的倍数的概率 posi=ri/p(li1)/prili+1

假设当前鲨鱼编号为 x ,左右的鲨鱼编号l,r则有单点贡献期望:

expx=(posx(1posl)+posx(1posr)+posx2(posl+posr))2000=2000posx(2posl+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 second
using 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;}
// head
const 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值