There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i andi + 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 neighbouringsharks 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 to421 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, 1000dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000dollars.
  2. (1, 420, 420421): now, the product ss0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000dollars, while shark s1 will 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.

题意

n个人围成环,如果两个相邻人的数字相乘后可以整除p,那么这两个人都得到1000元,现在给我们每个人的数字的范围,让我们求所有人得到的钱的期望。

分析

只要两个相邻的人数字中有一个可以整除那就两个人都能得到1000,我们求出每个人不能整除p的概率pp[i]=1-(r/p-(l-1)/p)/(r-l+1),因为这里是整除,所以r/p就是1到r有几个p的倍数,总个数是r-l+1。

然后两个邻居可以得到的概率就是只有两个人都不能整除时,就不能得到钱,即1-pp[i]*pp[i%n+1],概率累加起来再乘上2000就好了。

代码

#include <stdio.h>
long long n,p,l,r;
double pp[],ans;
int main()
{
scanf("%lld%lld",&n,&p);
for(int i=;i<=n;i++)
{
scanf("%lld%lld",&l,&r);
pp[i]=-(double)(r/p-(l-)/p)/(r-l+);
}
for(int i=;i<=n;i++)
ans+=-pp[i]*pp[i%n+];
printf("%f",ans*);
return ;
}

【CodeForces 621C】Wet Shark and Flowers的更多相关文章

  1. 【38.24%】【codeforces 621E】 Wet Shark and Blocks

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【CodeForces 621A】Wet Shark and Odd and Even

    题 Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wan ...

  3. 【CodeForces 621B】Wet Shark and Bishops

    题 题意 1000*1000的格子里,给你n≤200 000个点的坐标,求有多少对在一个对角线上. 分析 如果求每个点有几个共对角线的点,会超时. 考虑到对角线总共就主对角线1999条+副对角线199 ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. Wet Shark and Flowers(思维)

    C. Wet Shark and Flowers time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. 【81.82%】【codeforces 740B】Alyona and flowers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【Codeforces 258E】 Devu and Flowers

    [题目链接] http://codeforces.com/contest/451/problem/E [算法] 容斥原理 [代码] #include<bits/stdc++.h> usin ...

  8. 【17.00%】【codeforces 621D】Rat Kwesh and Cheese

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

随机推荐

  1. Django项目中model增加了新字段怎样更新?

    Django是不直接支持syncdb更新数据库的字段的,必须重新建立. 或者改一个表名新建一个表... 刚刚想出来一招: 自己在表上面先加一个字段,然后再在model上面改,貌似是可以的.

  2. nginx 一二事(3) - 反向代理以及负载均衡

    先来说说正向代理 就是普通的代理,记得高中年代偷跑去网吧是要办卡的 题外话: 就是这货...相信很多80同龄人都有吧...    回到正题,那正向代理就不让你直接访问网络,而需要登录一下网吧的某个系统 ...

  3. ehcache 一二事 - ssm 中ehcashe的简单配置应用

    Ehcache是一个开源Java分布式缓存.可以配合mybatis来使用   首先,在资源文件夹中新建ehcache.xml   内容如下: <?xml version="1.0&qu ...

  4. Unity2D Sprite Packer用法介绍

    想充分利用图片空间? 我们用来做sprite的图片通常会留有很多空白的地方,我们在画完了sprite之后,这些地方很可能就没有什么作用了. 如果想避免这些资源上的浪费,我们可以把各个sprite做成图 ...

  5. zepto.js 源码解析

    http://www.runoob.com/w3cnote/zepto-js-source-analysis.html Zepto是一个轻量级的针对现代高级浏览器的JavaScript库, 它与jqu ...

  6. 语义化的html结构的好处

    HTML是提供网页文档内容的上下文结构和含义:html本身是没有表现的,我们看到例如<h1>是粗体,字体大小2em,加粗:<strong>是加粗的,不要认为这是html的表现, ...

  7. .NET性能调优之一:ANTS Performance Profiler的使用

    .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使用 .NET性能调优之二:使用Visual Studio进行代码度量 .NET性能调 ...

  8. 在 WinForm 中使用 Direct2D

    在 C# 的 WinForm 应用中,界面的绘制使用的是 GDI+.不过在一些特别的应用中,可能需要用硬件加速来提高绘制的效率.下面就来介绍两种在 WinForm 应用中嵌入 Direct2D 的方法 ...

  9. 尝试EJB整合Mybatis部署时报错:获得带有类加载器MybatisUtil的ModuleClassLoader的反射信息出错,请问大神如何解决

    mybatis的配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configurati ...

  10. 如何自学 Android 编程?

    最近知乎上有网友问我怎么自学Android,其实说实在的,我学的也一塌糊涂,当然在学习过程也积累了一些知识,对于以前没接触过Android的朋友,或者刚入门Android 的朋友,这篇文章作为入门,那 ...