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 sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.

Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it's favourite prime number p, and he really likes it! 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.

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 that p is prime.

The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, 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 exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. 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 ss1 = 420, ss2 = 176576400, and ss0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars.
  2. (1, 420, 420421): now, the product ss0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1will receive 2000. The total is 4000.
  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.

昨晚求期望ans+=1000.0*(1.0-(1.0-A[i].P)*(1.0-A[(i+1)%n].P)*(1.0-A[i-1].P))

之后看的别人的AC代码每形成一对奖励2000 dollars所以计算每相邻的一对形成的概率而我则计算了每一个形成的概率显然不对。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
struct Nodee{double P;long long l,r,num;
}A[100010];
int main()
{int i,j,k,n;long long p;while(scanf("%d%I64d",&n,&p)!=EOF){for(i=0;i<n;++i){scanf("%I64d%I64d",&A[i].l,&A[i].r);long long ll,rr;if(A[i].l%p==0)ll=A[i].l;else ll=A[i].l/p*p+p;rr=A[i].r/p*p;if(ll==rr){A[i].num=1;}else if(ll>A[i].r){A[i].num=0;}else {A[i].num=(rr-ll)/p+1;}A[i].P=(A[i].num*1.0)/(1.0*(A[i].r-A[i].l+1));}double ans=0;for(i=0;i<n;++i){ans+=2000.0*(1.0-(1.0-A[i].P)*(1.0-A[(i+1)%n].P));}printf("%.6lf\n",ans);}return 0;
}

codeforces621C. Wet Shark and Flowers【求期望】相关推荐

  1. HDU4405(概率DP求期望)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:飞行棋,从0到n,置骰子,置到几就往前走几步,前进中会有捷径,比如2和5连到一起了,那你走到2 ...

  2. java求期望_Java 11的期望

    java求期望 过去的几年对Java世界一直是动荡不安的,在相当多的发行版中添加了各种各样的功能. 在开发人员社区中,人们逐渐意识到Java的开发速度不够快. 在最初的20年中,Java平台和Java ...

  3. 【概率论与数理统计】猴博士 笔记 p26-28 F、f的性质、一、二维连续型求期望、方差

    F.f的性质 做法: 上述公式的原理: 做一些题目来练习套公式. 例1: 解: P{X≤2}=F(2)=1−e−2P\{X\le2\}=F(2)=1-e^{-2} P{X≤2}=F(2)=1−e−2 ...

  4. matlab已知随机变量分布律求期望/已知概率密度求期望与方差

    本博文源于matlab基础,主要讲述已知随机变量分布律求期望还有已知随机变量的概率密度求期望与方差. 例子:设随机变量X的分布律如下表所示: X 10 30 50 70 90 Pk 1/2 1/3 1 ...

  5. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  6. P2473 奖励关 状压概率DP求期望

    题目链接 https://www.luogu.com.cn/problem/P2473 题意 n轮,m种球,每轮随即一个球,可以接可以不接,接的话需要满足这个球的前置条件(比如3号球接球必须已经接过1 ...

  7. 矩阵期望 matlab,matlab中矩阵元素求和、求期望和均方差

    在matlab中求一个矩阵中元素的和可以自己编写for循环来完成,这样比较方便,想求那些数据的和都可以做到,然而效率比较低,如果数据量大程序会跑好长时间.所以我们可以转而用matlab提供的sum函数 ...

  8. lightoj1027_数学求期望

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意 : 在n个门前选择一扇门出去, 然后如果第i扇门的 Xi值是正的话,你会花 ...

  9. LightOJ 1030 【概率DP求期望】

    借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...

  10. hdu 3853 LOOPS (概率dp 逆推求期望)

    题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Tot ...

最新文章

  1. Nginx负载均衡的详细配置及使用案例
  2. VISP视觉库识别AprilTag详细解读
  3. Jmeter连接mysql(一)
  4. C语言main函数、return、exit函数
  5. 【DIY】DIYarduino温湿度计视频图文教程
  6. 基于stm32f405控制三相无刷电机例程_ST SSC 频道 | 基于ST STM32F302R8T6 电机空气压缩机方案...
  7. 笔记-信息系统开发基础-架构设计-软件架构主要职责
  8. springmvc工作流程_SpringMVC工作原理
  9. Mybatis系列全解(六):Mybatis最硬核的API你知道几个?
  10. @Html.ValidationSummary()作用
  11. fatal: git-write-tree: error building trees
  12. car-like robot与两轮差速驱动机器人异同分析
  13. 递推DP URAL 1260 Nudnik Photographer
  14. SubSonic的配置及运用
  15. 人工智能商用成果落地撬动AI未来
  16. 分享,iOS国家手机区号代码.plist
  17. 建立SIP软电话环境
  18. 《黑客大曝光:移动应用安全揭秘及防护措施》一1.2 移动风险模型
  19. c++游戏编程初步(超简单)教学
  20. 【火炉炼AI】机器学习008-简单线性分类器解决二分类问题

热门文章

  1. 2020年到2021年计算机技术,2020年读计算机技术专业好不好
  2. solr配置oracle数据源,Solr索引Oracle数据库的基本配置
  3. Angr安装与使用之使用篇(六)
  4. linux web部署命令简单记录
  5. 异步任务,HttpContext.Current为null解决办法
  6. [LeetCode]319. Bulb Switcher灯泡开关
  7. java基础篇(二) ----- java面向对象的三大特性之继承
  8. 《JavaScript征途》广泛征集读后感创作者,参与有奖!
  9. 求1+2+3+...+n(剑指 Offer 64)
  10. C++对BIL格式遥感影像读取