CF621C.Wet Shark and Flowers

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 s__i. For i-th shark value s__i is random integer equiprobably chosen in range from l__i to r__i. 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 s__i·s__j 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.

有 nn 条鲨鱼为 Wet Shark 种植花朵。它们围坐在一张圆桌旁,因此对所有 i=1i = 1 到 n−1n-1,第 ii 条与第 i+1i+1 条鲨鱼是邻居;此外,第 nn 条与第 11 条鲨鱼也是邻居。

每条鲨鱼将种植一定数量的花朵 sis_i。对第 ii 条鲨鱼而言,sis_i 是在区间 [li,ri][l_i, r_i] 内等概率随机选取的一个整数。Wet Shark 有一个他最钟爱的质数 pp,并且他非常喜欢它!如果对任意一对相邻的鲨鱼 ii 和 jj,其花朵数量之积 si⋅sjs_i \cdot s_j 能被 pp 整除,则 Wet Shark 会感到开心,并向这两条鲨鱼各自发放 1000 美元。

一天结束时,所有鲨鱼将其获得的 Wet Shark 所发的钱款加总。求该总金额的期望值。

输入格式

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 l__i and r__i (1 ≤ l__i ≤ r__i ≤ 109), the range of flowers shark i can produce. Remember that s__i is chosen equiprobably among all integers from l__i to r__i, inclusive.

输入的第一行包含两个以空格分隔的整数 nn 和 pp(3≤n≤100 0003 \leq n \leq 100\,000,2≤p≤1092 \leq p \leq 10^9)——分别表示鲨鱼的数量以及 Wet Shark 最喜欢的质数。保证 pp 是质数。

接下来的 nn 行中,第 ii 行包含关于第 ii 条鲨鱼的信息——两个以空格分隔的整数 lil_i 和 rir_i(1≤li≤ri≤1091 \leq l_i \leq r_i \leq 10^9),表示第 ii 条鲨鱼能产生的花朵数量的取值范围。注意:sis_i 在区间 [li,ri][l_i, r_i] 内的所有整数中等概率随机选取。

输出格式

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 .

输出一个实数——鲨鱼们总共获得的美元的期望值。若你的答案的绝对或相对误差不超过 10−610^{-6},则视为正确。

即:假设你的答案为 aa,评测组的答案为 bb。当且仅当 时,评测程序将判定你的答案正确。

输入输出样例

  • 输入#1

    3 2
    1 2
    420 421
    420420 420421

    输出#1

    4500.0
  • 输入#2

    3 5
    1 4
    2 3
    11 14

    输出#2

    0.0

说明/提示

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 (_s_0, _s_1, _s_2) each shark grows:

  1. (1, 420, 420420): note that _s_0·_s_1 = 420, _s_1·_s_2 = 176576400, and _s_2·_s_0 = 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 _s_2·_s_0 is not divisible by 2. Therefore, sharks _s_0 and _s_2 will receive 1000 dollars, while shark _s_1 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.

质数是指仅能被 1 和它自身整除的正整数。1 不被视为质数。

考虑第一个样例:第一只鲨鱼种植的花朵数量在 11 到 22 之间(含端点),第二只鲨鱼在 420420 到 421421 之间,第三只鲨鱼在 420420420420 到 420421420421 之间。三只鲨鱼各自种植的花朵数量 (s0, s1, s2)(s_0,\,s_1,\,s_2) 共有八种可能情况:

  1. (1, 420, 420420)(1,\,420,\,420420):注意到 s0⋅s1=420s_0\cdot s_1 = 420,s1⋅s2=176576400s_1\cdot s_2 = 176576400,以及 s2⋅s0=420420s_2\cdot s_0 = 420420。对每一对乘积,若其为偶数,则对应两只鲨鱼各获得 1000 美元。因此,每只鲨鱼均获得 2000 美元,总计 6000 美元。
  2. (1, 420, 420421)(1,\,420,\,420421):此时乘积 s2⋅s0s_2\cdot s_0 不是偶数,故鲨鱼 s0s_0 和 s2s_2 各得 1000 美元,而鲨鱼 s1s_1 得到 2000 美元。总计为 4000 美元。
  3. (1, 421, 420420)(1,\,421,\,420420):总计为 4000 美元。
  4. (1, 421, 420421)(1,\,421,\,420421):总计为 0 美元。
  5. (2, 420, 420420)(2,\,420,\,420420):总计为 6000 美元。
  6. (2, 420, 420421)(2,\,420,\,420421):总计为 6000 美元。
  7. (2, 421, 420420)(2,\,421,\,420420):总计为 6000 美元。
  8. (2, 421, 420421)(2,\,421,\,420421):总计为 4000 美元。

期望值为 。

在第二个样例中,任意数量组合均无法使鲨鱼获得奖金。

输入解题思路,AI测评打分。不知道怎么写?

首页