【题解】P1316 Mivik 写书
我认为出题人想麻烦了。
我们考虑计算一个串的答案,所有子串数是
具体方法是枚举起点集合
时间复杂度是
#define M 1050000
int n, m, ans, bt[M], mt[M], fa[26], pw[26];
int get(int x){return fa[x] == x ? x : fa[x] = get(fa[x]);}
void calc(int x){
int s = n - x + 1, o = (1 << x) - 1, w = (1 << s) - 1;
rp(i, w){
int c = 0, k = 0;
rep(j, 0, x - 1)fa[j] = j;
rep(j, 0, n - 1){
c = (c << 1) | (1 & (i >> j)), c &= o;
if(c){
int p = get(mt[c & -c]), t = c - (c & -c);
for(; t; t -= t & -t)fa[get(mt[t & -t])] = p;
}
else k++;
}
rep(j, 0, x - 1)k += fa[j] == j;
if(bt[i])ad(ans, pw[k]);
else su(ans, pw[k]);
}
}
int main() {
read(n, m);
int w = (1 << n) - 1;
rep(i, 0, n)mt[1 << i] = i;
pw[0] = 1; rp(i, n)pw[i] = pw[i - 1] * 1LL * m % P;
rp(i, w)bt[i] = (i & 1) ^ (bt[i >> 1]);
rp(i, n)calc(i);
int inv = Pow(m, P - 2);
rp(i, n)ans = ans * 1LL * inv % P;
printf("%d\n", ans);
return 0;
}