Soul artist
Title Description
I do not know why, but you always want to finish a painting in a magical way.
You put a row of n sheets of drawing paper paved, and from them to the n 1 number. You a total of c colors are available, these colors can be 0 to c-1 are numbered. Initially, all colors are drawing paper 1 . A total of k times you want to paint, paint when the i-th, the probability that you would like randomly selected from a closed interval [L i , R & lt i ] in a subset of the drawing paper (which may be empty), and then randomly pick one color B I , and to pick out the color of the drawing paper is coated. Original color after a drawing paper painted color b, the color will become (a * b) mod c, this is the law of this world.
Because you will use digital color named, so you can find at the end of the k-th painting, drawing paper for each color corresponding to the sum of digital and expectations. Now you ask about programming this value.
Just in case you do not know what is expected: If a quantity X, which has the p- 1 probability values v 1 , there is the p- 2 probability value v 2 ...... the p- the n- probability value of v the n- , then X is expected value equal to P . 1 V . 1 + P 2 V 2 + ...... + P n- V n-
Input Format
The first line contains three positive integer n, c, k, as meaning the title.
Next k rows, each row comprising two numbers L I , R & lt I , that you will be randomly selected from each operation of drawing paper from which section.
Output Format
Line, a decimal represent three answers, rounded to the nearest decimal place.
Sample input 1
2 3 1 1 2
Sample output 1
2.000
Sample interpretation
There are four possibilities to select a subset of, the probability of each is 1/4.
Empty set is selected from: Paper does not change, and the color is 1 + 1 = 2;
Selected from {1}: Paper 2 does not change. There are three possible to choose the color, so that the drawing paper into a final color 0,1,2 respectively, the probability is 1/3, and a desired color is 1/3 * (1 + 0) + 1/3 * (1 +1) + 1/3 * (2 + 1) = 2;
Is selected from: {2} with symmetrical selected from 1, and a desired color is 2;
Is selected from {1,2}: color selected from the three possible, so that two colors eventually become 0,1,2 Paper, the probability is 1/3, and a desired color is 1/3 * (0 + 0 ) + 1/3 * (1 + 1) + 1/3 * (2 + 2) = 2;
In summary, the four kinds of color scheme and subset selection is desirably 2.
Sample input 2
3 3 3 1 2 2 3 1 3
Sample output 2
2.639
data range:
Ideas:
And expectations and desired =. Since the initial state of each sheet are 1, so consider N . 3 desired DP. After we dp [i] [j] represents a piece of paper it has been operated once i become a probability of color j. Calculated separately for each sheet, several statistical operation. At each enumeration subset of probability has one-half of each element in the interval selected color, while the probability of each color being selected is 1 / c, then we have a transition equation, if It was chosen: dp [t] [i * j% c] + = dp [t-1] [i] / (c * 2); if not to be chosen: dp [t] [i] + = dp [ t-1] [i] / 2;
Code:
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int n,c,k,a,b,num[108],maxn; double dp[108][108],ans; long long read() { long long x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int main() { freopen("paint.in","r",stdin); freopen("paint.out","w",stdout); n=read();c=read();k=read(); for(int i=1;i<=k;++i) { a=read();b=read(); for(int i=a;i<=b;++i) { num[i]++; maxn=max(maxn,num[i]); } } dp[0][1]=1; for(int t=1;t<=maxn;++t) { for(int i=0;i<c;++i) { for(int j=0;j<c;++j) { dp[t][i*j%c]+=dp[t-1][i]/(c*2); } dp[t][i]+=dp[t-1][i]/2; } } for(int i=1;i<=n;++i) { for(int j=0;j<c;++j) { ans+=dp[num[i]][j]*j; } } printf("%.3lf",ans); return 0; }