Indicates that mathematics is a scum...
In fact, only the probability that the color of each box after K times is I can be calculated ..
The boxes of the range [L, R] are random colors and random values, so the probability is 1/C and 1/2 respectively, so the overall probability is the product of the two. Based on the full probability formula, we can accumulate and complete the following states ..
After finding the probability, we expect it to be the color number * probability .......
Brute force: 40 .. O (K * n * C ^ 2 )...
#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <vector>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const double eps=1e-10;double e[55][105][55];int c, K, n, L[55], R[55];int main() {int cs=getint();while(cs--) {read(n); read(c); read(K);for1(i, 1, K) read(L[i]), read(R[i]);for1(k, 1, K+1) rep(i, c) for1(j, 1, n) e[k][i][j]=0;for1(i, 1, n) e[1][1][i]=1.0;for1(k, 1, K) {rep(i, c) for1(j, 1, L[k]-1) e[k+1][i][j]=e[k][i][j];rep(i, c) for1(j, R[k]+1, n) e[k+1][i][j]=e[k][i][j];rep(b, c) rep(i, c) for1(j, L[k], R[k]) e[k+1][(i*b)%c][j]+=e[k][i][j]/2/c;rep(i, c) for1(j, L[k], R[k]) e[k+1][i][j]+=e[k][i][j]/2;}double ans=0;rep(i, c) for1(j, 1, n) ans+=i*e[K+1][i][j];printf("%.9f\n", ans);}return 0;}
Then consider optimization:
We found that the boxes are the same and the probability is determined based on the number of times covered by the range (that is, the first two of the four loops in the above Code. If these vertices are not covered, so the values in the future will be the same ...)
Therefore, we only need to calculate the probability by the number of overwrites.
If the number of times a box is overwritten is X, the probability of X is used.
In this way, the time complexity is optimized to O (K * C ^ 2)
#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <vector>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const double eps=1e-10;double e[55][105][55];int c, K, n, L[55], R[55];int main() {int cs=getint();while(cs--) {read(n); read(c); read(K);for1(i, 1, K) read(L[i]), read(R[i]);for1(k, 1, K+1) rep(i, c) for1(j, 1, n) e[k][i][j]=0;for1(i, 1, n) e[1][1][i]=1.0;for1(k, 1, K) {rep(i, c) for1(j, 1, L[k]-1) e[k+1][i][j]=e[k][i][j];rep(i, c) for1(j, R[k]+1, n) e[k+1][i][j]=e[k][i][j];rep(b, c) rep(i, c) for1(j, L[k], R[k]) e[k+1][(i*b)%c][j]+=e[k][i][j]/2/c;rep(i, c) for1(j, L[k], R[k]) e[k+1][i][j]+=e[k][i][j]/2;}double ans=0;rep(i, c) for1(j, 1, n) ans+=i*e[K+1][i][j];printf("%.9f\n", ans);}return 0;}
Description:
Elephants like to color boxes. The elephant now has c colors, numbered 0 ~ C-1 are also n boxes numbered 1 ~ N. The color of each box is 1 at the beginning. The elephant prefers to follow the inspiration when coloring: It sorts the box into a row by number, and it randomly chooses [L, r] some boxes in this range (do not select as 0), and apply a random color to it. If a box of color a is painted with B, the color of the box will change to (a * B) mod C. What is the color number and expectation of all boxes after K coloring?
Input description:
The first behavior t indicates that there are T groups of test data.
For each group of data, the first behavior is three integers: N, C, and K.
In the next K rows, each row has two integers, Li and RI, representing the L and R of the I-th operation.
Output description:
For each group of test data, the color numbers and expected values of all boxes are output, and the results are kept at 9 decimal places.
Sample input:
3
3 2 2
2 2
1 3
1 3 1
1 1
5 2 2
3 4
2 4
Sample output:
2.062500000
1.000000000
[Noip simulation questions] image coloring (probability + expectation + recurrence)