题意:
一群鲨鱼围成一圈,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;
}
仔细读题!!别再马虎了!!心疼时间和分数。。。