这是一题AC自动机 + 矩阵快速幂的题目,
首先知道总答案应该是26^1 + 26^2 + 26^3 .... + 26^L,用等比数列的前n项和是无法做的,因为出现小数。
这个可以直接看到F[n] = 26 * F[n - 1] + 26,然后矩阵快速幂即可。
然后需要减去那些一个词根都不包含的单词的总数,这个可以用AC自动机算出来。就是至少包含一个词根的答案。
现在关键就是求,长度小于等于L的通路总数。
我们知道通路等于L的通路总数,正是一个可达矩阵e[i][j]的L次幂。
那么小于等于L的,也就是e + e^2 + e^3 + e^4 + ... + e^L
这是无法求的。
做法是新增一个节点,然后每一个节点都和这个新的节点连上一条边。
假设本来的可达矩阵是
e[1][1] = 1, e[1][2] = 1, e[1][3] = 1
e[2][1] = 0, e[2][2] = 0, e[2][3] = 1;
e[3][1] = 0, e[3][2] = 0, e[3][3] = 0;
那么长度是2的通路数就是,e^2,然后得到了这个图。
所以长度是2的通路数,是4。e[1][1] ---> e[1][1],e[1][1]--->e[1][2]。e[1][1] ---> e[1][3],e[1][2]--->e[2][3]
那么长度是1的通路数就不能统计了,就是e[1][1]、e[1][2]、e[1][3]丢失了。
那么我们新增一个节点,使得其他本来的节点都和它连一条边。
然后算e^2的时候,
就会把e[1][1]记录到e[1][1]-->e[1][4]中,所以记录成功。、
然后e[1][4]本来是不存在的,所以这条路径是多余的,
#include #include #include #include #include #include #define IOS ios::sync_with_stdio(false)using namespace std;#define inf (0x3f3f3f3f)typedef long long int LL;#include #include #include #include #include
View Code