Codeforces 621C Wet Shark and Flowers

题意:

一群鲨鱼围成一圈,Wet Shark说个质数p,每个鲨鱼在一定范围内选个数,如果两个相邻的鲨鱼选的数的乘积能被p整除,则每个鲨鱼都将得到1000元,求鲨鱼们最终得到钱数的期望。

分析:

比赛时乱七八糟的写,一直错,重新读题才注意到题目中说的:

If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.

只看相邻的两条鲨鱼,所以对于每条鲨鱼,只需看他和他前一条鲨鱼的数值范围大小以及范围内p的倍数的个数,转化为概率,分两种情况考虑:

  • 两条鱼选的数都是p的倍数
  • 一条鱼选的是p的倍数,另一条不是

代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
const int maxn = 100005, INF=0x3fffffff;
typedef long long ll;
double f[maxn];
int main (void)
{
    int n;ll p;
    ll l,r;
    ll total = 1, cnt;
    scanf("%d%I64d",&n,&p);
    for(int i = 1; i <= n; i++){
        scanf("%I64d%I64d",&l,&r);
        cnt = r/p - l/p;
        if(l%p == 0) cnt++;
        f[i]=(double)cnt/(r - l+1);
    }
    f[0] = f[n];
    double ans = 0.0;
    for(int i = 1; i <= n; i++)
       ans += 1000*(f[i]*f[i-1] + (1-f[i])*f[i-1]+(1-f[i-1])*f[i]);

    printf("%.10lf\n",(double)ans*2);
    return 0;
}

仔细读题!!别再马虎了!!心疼时间和分数。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值