"Codeforces 621C" Wet Shark and Flowers

Source: Internet
Author: User

Problem

There is n sharks who grow flowers for Wet Shark. They is all sitting around the table, such that sharks i andi + 1 is neighbours for all i from 1 to n -1. Sharks N and 1 are neighbours too.

Each shark'll grow some number of flowerssI. ForI-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 neighbouringsharks  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.

Input

The first line of the input contains the 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's p is prime.

TheI-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.

Output

Print a single real number-the expected number of dollars, the Sharks receive in total. You answer'll be considered correct if it absolute or relative error does not exceed 6.

Namely:let ' s assume that your answer are a, and the answer of the jury is b. The checker program would 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, which is divisible only by 1 and itself. 1 is isn't considered to being 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 is eight cases for the quantities of flowers (s0, s1, s2) each SH Ark grows:

  1. (1, 420, 420420): note That  s 0 s 1 = 420,  s s 2 = 176576400, And  s 2 s 0 = 420420. For each pair, 1000dollars'll be awarded to each shark. Therefore, each shark would be Awarded 2000 dollars, for a total Of 6000dollars.
  2. (1, 420, 420421): Now, the product s2 · S0 is not divisible by 2. Therefore, Sharks s0 and s2 would receive 1000dollars, while Shark s 1 would 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 would garner the Sharks any money.

Test instructions

n individuals surround into a ring, if the number of two neighboring people can be divided into P, then these two people all get 1000 yuan, now give us each of the number of the range, let us ask all people to get the money of the expectation.

Analysis

As long as the number of two adjacent people can be divided into two people can get 1000, we find that everyone can not divide the probability of P pp[i]=1-(r/p-l/p+ (l%p==0))/(r-l+1), because this is the division, so r/p is 1 to R has a number of multiples of p, When L is a multiple of p, then add 1, because you subtract the number of multiples of p from 1 to L, the total number is r-l+1.

Then two neighbors can get the probability that only two people cannot be divisible, you can not get money, that is, 1-pp[i]*pp[i%n+1], the probability of summing up and then multiply 2000 on the good.

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

"Codeforces 621C" Wet Shark and Flowers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.