分析
跟上面一题的思路差不多,加一个容斥就好了
我们需要求有目标串的数量,可以计算出不含目标串的数量,然后用总数量去减即可
因为需要对2^64取模,可以直接用ull
然后再用矩阵快速幂求出等比矩阵的和即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 100;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
char c=getchar();T x=0,f=1;while(!isdigit(c)){
if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
return (b>0)?gcd(b,a%b):a;}
int tr[N][26];
int ne[N],idx;
int q[N];
char str[N];
int cnt[N];
int n,m;
void init(){
memset(tr,0,sizeof tr);
memset(ne,0,sizeof ne);
memset(cnt,0,sizeof cnt);
idx = 0;
}
struct Node{
ull a[N][N];
Node(){
memset(a,0,sizeof a);
}
};
Node operator*(const Node &A,const Node &B){
Node res;
for(int i = 0;i <= idx;i++)
for(int j = 0;j <= idx;j++)
for(int k = 0;k <= idx;k++)
res.a[i][j] = (res.a[i][j] + A.a[i][k] * B.a[k][j]);
return res;
}
Node fastm(Node A,int x){
Node res;
for(int i = 0;i <= idx;i++) res.a[i][i] = 1;
while(x){
if(x & 1) res = res * A;
A = A * A;
x >>= 1;
}
return res;
}
void insert(){
int p = 0;
for(int i = 0;str[i];i++){
int t = str[i] - 'a';
if(!tr[p][t]) tr[p][t] = ++idx;
p = tr[p][t];
}
cnt[p] = 1;
}
void build(){
int hh = 0,tt = -1;
for(int i = 0;i < 26;i++)
if(tr[0][i])
q[++tt] = tr[0][i];
while(hh <= tt){
int t = q[hh++];
for(int i = 0;i < 26;i++){
int c = tr[t][i];
if(!c) tr[t][i] = tr[ne[t]][i];
else{
q[++tt] = c;
ne[c] = tr[ne[t]][i];
}
cnt[tr[t][i]] |= cnt[tr[ne[t]][i]];
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i = 1;i <= n;i++){
scanf("%s",str);
insert();
}
build();
Node res;
for(int i = 0;i <= idx;i++)
if(!cnt[i]){
for(int j = 0;j < 26;j++)
if(!cnt[tr[i][j]])
res.a[i][tr[i][j]]++;
}
idx++;
for(int i = 0;i <= idx;i++)
res.a[i][idx] = 1;
res = fastm(res,m);
ull pp = 0;
for(int i = 0;i <= idx;i++) pp += res.a[0][i];
pp--;
Node p;
p.a[0][0] = 26;
p.a[1][1] = p.a[0][1] = 1;
p = fastm(p,m);
ull ans = p.a[0][0] + p.a[0][1] - 1;
printf("%llu\n",ans - pp);
}
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/