# 【做题】SDOI2017硬币游戏——方程＆概念处理

$n,m \leq 300$

-　恰好走到串$i$的终止状态。并且，由于$S$是任意走不到终止状态的串，因此这能包含所有恰好在串$i$终止的状态。
-　在后面接上$j$个01后，就在途中停止了。假设我们在串$k$处停止。那么，串$k$长度为$j$的前缀就与串$i$长度为$j$的后缀相同。然后，因为能保证串$k$的串长不小于$j$，且在$k$串处终止就意味着在之前一定没有终止，所以这包含了所有在$k$处终止的状态。但还要注意后面又强行加上了$m-j$位。

$\frac {x_0} {2^m} = \sum_{k=1}^n \sum_{j=1}^m \frac {1} {2^{m-j}} \left[s_k[m-j+1:m+1] = s_i[1,j+1]\right] x_k$

#include <bits/stdc++.h>
using namespace std;
const int N = 310, MOD[2] = {(int)(1e9 + 7), (int)(1e9 + 9)}, BAS = 3;
typedef double db;
db mat[N][N],pwi2[N];
int n,m,has[2][N][N],pw[2][N];
char s[N][N];
int gethas(int k,int *has,int l,int r) {
return (has[r] - 1ll * has[l-1] * pw[k][r-l+1] % MOD[k] + MOD[k]) % MOD[k];
}
void guass(int rn) {
for (int i = 1 ; i <= rn ; ++ i) {
int r = i;
for (int j = i + 1 ; j <= rn ; ++ j)
if (fabs(mat[j][i]) > fabs(mat[r][i]))
r = j;
if (r != i)
for (int j = i ; j <= rn + 1 ; ++ j)
swap(mat[i][j],mat[r][j]);
for (int j = i + 1 ; j <= rn ; ++ j) {
for (int k = i + 1 ; k <= rn + 1 ; ++ k)
mat[j][k] -= (mat[j][i] / mat[i][i]) * mat[i][k];
mat[j][i] = 0;
}
}
for (int i = rn ; i >= 1 ; -- i) {
mat[i][rn+1] /= mat[i][i];
for (int j = i - 1 ; j >= 1 ; -- j)
mat[j][rn+1] -= mat[i][rn+1] * mat[j][i];
}
}
int main() {
scanf("%d%d",&n,&m);
for (int i = 1 ; i <= n ; ++ i)
scanf("%s",s[i] + 1);
for (int k = 0 ; k < 2 ; ++ k) {
pw[k][0] = 1;
for (int i = 1 ; i <= m ; ++ i)
pw[k][i] = 1ll * pw[k][i-1] * BAS % MOD[k];
for (int i = 1 ; i <= n ; ++ i) {
has[k][i][0] = 0;
for (int j = 1 ; j <= m ; ++ j)
has[k][i][j] = (1ll * has[k][i][j-1] * BAS + (s[i][j] == 'T')) % MOD[k];
}
}
pwi2[0] = 1.0;
for (int i = 1 ; i <= m ; ++ i)
pwi2[i] = pwi2[i-1] / 2.0;
for (int i = 1 ; i <= n ; ++ i) {
mat[i][n+1] = - pwi2[m];
for (int j = 1 ; j <= m ; ++ j) {
for (int k = 1 ; k <= n ; ++ k) {
if (gethas(0,has[0][k],m-j+1,m) == gethas(0,has[0][i],1,j))
if (gethas(1,has[1][k],m-j+1,m) == gethas(1,has[1][i],1,j)) {
mat[i][k] += pwi2[m - j];
}
}
}
}
for (int i = 1 ; i <= n ; ++ i)
mat[n+1][i] = 1;
mat[n+1][n+2] = 1;
guass(n+1);
for (int i = 1 ; i <= n ; ++ i)
printf("%.8lf\n",mat[i][n+2]);
return 0;
}

posted @ 2018-10-21 15:56 莫名其妙的aaa 阅读(...) 评论(...) 编辑 收藏