CF1151F - Sonya and Informatics

1151F - Sonya and Informatics

Meaning of the questions: There is a sequence of length n, 01, seeking through K times after the two exchanged random number sequence of this non-drop probability. n <= 100, k <= 1e9.

Solution: see the data matrix fast power range thought ...

First think a violent maintain each position for the probability of 1 DP, we found no independent ...

Then think of PKUSC T2, to determine each and every individual, but this thing is not aligned with the same elements not engage in ...

Finally, think of thuPC put furniture, it can be found with that routine.

Found that the final probability of each state from the initial state only with distance. Here the distance is the number 1 or exclusive state after the two.

Thus for DP, denotes the front i fij set operations, there is a probability of a j-th state of the initial state obtained after the exclusive OR.

There are three kinds of transfer, namely useless transfer, move a 1 to 0 in the initial state, a 1 to 1 Norway initial state.

Then look at the power of the matrix quickly.

Finally, when the operator answers, we need to calculate how many states to meet the conditions, which is a combination of numbers. This is then divided by the number of combinations on the line.

  1 #include <bits/stdc++.h>
  2 
  3 const int N = 210, MO = 1e9 + 7;
  4 
  5 int n, K, A[N], fac[N], inv[N], invn[N], a[N][N], ans[N][N], c[N][N], lm;
  6 
  7 inline void Add(int &a, const int &b) {
  8     a += b;
  9     if(a >= MO) a -= MO;
 10     else if(a < 0) a += MO;
 11     return;
 12 }
 13 
 14 inline int qpow(int a, int b) {
 15     int ans = 1;
 16     while(b) {
 17         if(b & 1) {
 18             ans = 1ll * ans * a % MO;
 19         }
 20         a = 1ll * a * a % MO;
 21         b = b >> 1;
 22     }
 23     return ans;
 24 }
 25 
 26 inline int Inv(int x) {
 27     return qpow(x, MO - 2);
 28 }
 29 
 30 inline int iC(int n, int m) {
 31     if(n < 0 || m < 0 || n < m) return 0;
 32     return 1ll * invn[n] * fac[m] % MO * fac[n - m] % MO;
 33 }
 34 
 35 inline void mulself() {
 36     memset(c, 0, sizeof(c));
 37     for(int k = 0; k <= lm; k++) {
 38         for(int i = 0; i <= lm; i++) {
 39             if(!a[i][k]) continue;
 40             for(int j = 0; j <= lm; j++) {
 41                 Add(c[i][j], 1ll * a[i][k] * a[k][j] % MO);
 42             }
 43         }
 44     }
 45     memcpy(a, c, sizeof(a));
 46     return;
 47 }
 48 
 49 inline void mul() {
 50     memset(c, 0, sizeof(c));
 51     for(int k = 0; k <= lm; k++) {
 52         for(int i = 0; i <= lm; i++) {
 53             if(!a[i][k]) continue;
 54             for(int j = 0; j <= lm; j++) {
 55                 Add(c[i][j], 1ll * a[i][k] * ans[k][j] % MO);
 56             }
 57         }
 58     }
 59     memcpy(ans, c, sizeof(c));
 60     return;
 61 }
 62 
 63 int main() {
 64     scanf("%d%d", &n, &K);
 65     int cnt = 0, aim = 0;
 66     for(int i = 1; i <= n; i++) {
 67         scanf("%d", &A[i]);
 68         cnt += A[i];
 69     }
 70     for(int i = 1; i + cnt <= n; i++) {
 71         aim += A[i];
 72     }
 73     //int P = 1ll * Inv(n * (n - 1) / 2) * cnt % MO * (n - cnt) % MO;
 74     int P = Inv(n * (n - 1) / 2);
 75     fac[0] = inv[0] = invn[0] = 1;
 76     fac[1] = inv[1] = invn[1] = 1;
 77     for(int i = 2; i <= n; i++) {
 78         fac[i] = 1ll * i * fac[i - 1] % MO;
 79         inv[i] = 1ll * inv[MO % i] * (MO - MO / i) % MO;
 80         invn[i] = 1ll * invn[i - 1] * inv[i] % MO;
 81     }
 82     
 83     lm = n - cnt;
 84     for(int j = 0; j <= n - cnt; j++) {
 85         int t1 = 1ll * (cnt - j) * (n - cnt - j) % MO * P % MO;
 86         int t2 = 1ll * j * j * P % MO;
 87         Add(a[j][j], (1 - t1 + MO - t2 + MO) % MO);
 88         Add(a[j][j + 1], t1);
 89         Add(a[j][j - 1], t2);
 90     }
 91     for(int i = 0; i <= lm; i++) {
 92         ans[i][i] = 1;
 93     }
 94     
 95     while(K) {
 96         if(K & 1) {
 97             mul();
 98         }
 99         mulself();
100         K >>= 1;
101     }
102     
103     
104     /*f[0][0] = 1;
105     for(int i = 0; i < K; i++) {
106         for(int j = 0; j <= n - cnt; j++) {
107             //Add(f[i + 1][j], (1ll - P + MO) * f[i][j] % MO);
108             int t1 = 1ll * (cnt - j) * (n - cnt - j) % MO * P % MO;
109             int t2 = 1ll * j * j * P % MO;
110             Add(f[i + 1][j + 1], 1ll * f[i][j] * t1 % MO);
111             Add(f[i + 1][j - 1], 1ll * f[i][j] * t2 % MO);
112             Add(f[i + 1][j], 1ll * f[i][j] * ((1 - t1 + MO - t2 + MO) % MO) % MO);
113         }
114     }*/
115     
116     int Ans = 1ll * ans[0][aim] * iC(n - cnt, aim) % MO * iC(cnt, aim) % MO;
117     printf("%d\n", (Ans + MO) % MO);
118     return 0;
119 }
AC Code

I understand the way that there are two sets S1, S2. S1 is the initial state in which the position 1, S2 of the position in the initial state is zero. We will go into S2 which put a number 1. Each time may not change the number of S2 is 1, or a 1 to take over from the S1, S2 or from a 1 to take out.

Write the code did not pay attention when the boundary j = 0, might array bounds, but the A ...

Guess you like

Origin www.cnblogs.com/huyufeifei/p/10962506.html