Codeforces Round #341 (Div. 2) C. Wet Shark and Flowers (期望)

博客探讨了Codeforces Round #341中的一道题目,涉及到n个鲨鱼随机选择生长花朵的数量,若相邻鲨鱼所选花朵乘积能被Wet Shark的幸运素数p整除,它们将获得奖金。文章解析了问题,给出了计算期望奖金的思路和AC代码。
摘要由CSDN通过智能技术生成
C. Wet Shark and Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharksi andi + 1 are neighbours for alli from1 to n - 1. Sharksn and 1 are neighbours too.

Each shark will grow some number of flowers si. Fori-th shark valuesi is random integer equiprobably chosen in range fromli tori. Wet Shark has it's favourite prime numberp, and he really likes it! If for any pair ofneighbouring sharksi and j the productsi·sj is divisible byp, then Wet Shark becomes happy and gives1000 dollars to each of these sharks.

At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.

Input

The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark's favourite prime number. It is guaranteed thatp is prime.

The i-th of the following n lines contains information about i-th shark — two space-separated integersli andri (1 ≤ li ≤ ri ≤ 109), the range of flowers sharki can produce. Remember thatsi is chosen equiprobably among all integers fromli tori, inclusive.

Output

Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury isb. The checker program will consider your answer correct, if.

Sample test(s)
Input
3 2
1 2
420 421
420420 420421
Output
4500.0
Input
3 5
1 4
2 3
11 14
Output
0.0
Note

A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.

Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers(s0, s1, s2) each shark grows:

  1. (1, 420, 420420): note that s0·s1 = 420,s1·s2 = 176576400, ands2·s0 = 420420. For each pair,1000 dollars will be awarded to each shark. Therefore, each shark will be awarded2000 dollars, for a total of6000 dollars.
  2. (1, 420, 420421): now, the product s2·s0 is not divisible by2. Therefore, sharkss0 ands2 will receive1000 dollars, while sharks1 will receive2000. The total is4000.
  3. (1, 421, 420420): total is 4000
  4. (1, 421, 420421): total is 0.
  5. (2, 420, 420420): total is 6000.
  6. (2, 420, 420421): total is 6000.
  7. (2, 421, 420420): total is 6000.
  8. (2, 421, 420421): total is 4000.

The expected value is .

In the second sample, no combination of quantities will garner the sharks any money.


题意:英语渣,不停的看题,才看明白,给你n个区间和一个数k,求当前区间取一个数*下一个区间取一个数能够整除k的期望。。


思路:经典概率问题,一个区间内能整除k的概率为p[i]=(r/k-(l-1)/k)/(r-l+1),所以说满足条件的概率为

P=p[i]*p[i+1]+p[i]*(1-p[i+1])+(1-p[i])*p[i+1],化简后就是P=p[i]+p[i+1]-p[i]*p[i+1]然后累加P*2000即可,注意可成环,即第n个区间的下一个为第一个。。

ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 100010
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
double p[MAXN];
int main()
{
	int l,r,n,k,i;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&l,&r);
			int pp=r/k-(l-1)/k;
			p[i]=(double)pp/(r-l+1);
			//printf("p=%lf\n",p[i]);
		}
		p[n]=p[0];
		double ans=0;
		for(i=0;i<n;i++)
		{
			ans+=(p[i]+p[i+1]-p[i]*p[i+1])*2000.0;
		}
		printf("%lf\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值