# 111qqz的小窝

## hdu 4336 Card Collector (2012多校 #4) （容斥原理模板题）

http://acm.hdu.edu.cn/showproblem.php?pid=4336

20160305update:前几天有道题没写递归形式tle掉了。。。所以来学习一下容斥原理的递归形式。

## codeforces 107 B. Basketball Team

http://codeforces.com/problemset/problem/107/B

## codeforces 518 D. Ilya and Escalator

http://codeforces.com/problemset/problem/518/D

## codeforces 453 A. Little Pony and Expected Maximum

http://codeforces.com/problemset/problem/453/A

## codeforces 476 B. Dreamoon and WiFi

http://codeforces.com/problemset/problem/476/B

## codeforces #341 div2 C. Wet Shark and Flowers

http://codeforces.com/contest/621/problem/C

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 lito 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 fromli 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.

Sample test(s)
input

output

input

output

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 421flowers 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, and s2·s0 = 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 s2·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, 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.

## codeforces 30 C. Shooting Gallery

http://codeforces.com/contest/30/problem/C

# Desiderium

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 427    Accepted Submission(s): 167

Problem Description

There is a set of intervals, the size of this set is

If we select a subset of this set with equal probability, how many the expected length of intervals’ union of this subset is?

We assume that the length of empty set’s union is 0, and we want the answer multiply

Input

The first line of the input is a integer

Every test cases begin with a integer

Then

Output
For every test case output the answer multiply

Sample Input
2
1
0 1
2
0 2
1 3

Sample Output

1 7

Hint

For the second sample, the excepted length is $frac{0+2+2+3}{4}=frac{7}{4}$.

1、空集，集合中区间的并的长度为0

2、{区间1}，集合中区间的并的长度为2

3、{区间2}，集合中区间的并的长度为2

4、{区间1、区间2}，集合中区间的并为[0,3]，长度为3

…………

## cf 442B Andrey and Problem

B. Andrey and Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.

Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of Andrey’s friends. The second line contains n real numbers pi(0.0 ≤ pi ≤ 1.0) — the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.

Output

Print a single real number — the probability that Andrey won’t get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10 - 9.

Sample test(s)
input

output

input

output

Note

In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.

In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8 + 0.9·0.2 = 0.26.