Topic Link: Click to open the link
Test instructions: There are n people sitting around in a circle, everyone can choose a number from the A[I].L to A[I].R, if the two adjacent number of products can be divided into p, then reward them a person 1000, the sum of the money to seek the expectation.
Idea: Since the expectation, the probability is obtained first. It is clear that each group of two adjacent to the value of the product can be divided by P, it is easy to know how many numbers in the interval is not divisible by P, is difficult to reverse, you can calculate the number of adjacent two how many combinations can not be divided by P, then it is easy to calculate each group can be divided by P probability, multiply 2000 is the Expectation Plus is the total expectation.
See the code for details:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long Long ll;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int mod = 10000 00000 + 7;const int INF = 1000000000;const int MAXN = 100000 + 10;int t,n,m,p;struct node {int L, r, Num; Double P;} A[maxn];int Main () {scanf ("%d%d", &n,&p); for (int i=0;i<n;i++) {scanf ("%d%d", &A[I].L,&A[I].R); A[i].num = a[i].r/p-(A[I].L-1)/p; } double ans = 0; for (int i=0;i<n;i++) {int J = (i + 1)% n; Double la = a[i].l, RA = a[i].r, lb = a[j].l, RB = A[J].R; Double C1 = a[i].num, c2 = A[j].num; A[I].P = 1.0-(double) (Ra-la + 1-c1)/(double) (Ra-la + 1) * (double) (rb-lb + 1-c2)/(double) (rb-lb + 1) ; Ans + = A[I].P; } printf ("%.8f\n", ans*2000); return 0;}
Codeforces Round #341 (Div. 2) c. Wet Shark and Flowers (simple repulsion)