洛谷 - P3193 GT考试


传送门:洛谷 - P3193

思路分析

$dp[i][j]$表示当前是第$i$位处于ac自动机的$j$结点,转移可以用矩阵快速幂优化

AC代码

#include <bits/stdc++.h>
#define V vector
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lowbit(x) (x)&(-x)
#define sz(x) (int)(x).size()
#define each(x,a) for(auto& x:a)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

using namespace std;
#define int long long
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef unsigned long long ull;

const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
const int N = 111;

int n,m;
int mod;

struct T {
    int son[26],fail,word,last;
    void clear() {
        mem(son,0);
        word = fail = last = 0;
    }
} tree[N];

int cnt = 1;
int ed[N];

int newnode() {
    ++cnt;
    tree[cnt].clear();
    return cnt;
}

void init() {
    cnt = 1;
    mem(ed,0);
    tree[1].clear();
    tree[1].word = 1;
}

void ins(string s) {
    int p = 1;
    for(auto i:s) {
        int c = i-'0';
        if(!tree[p].son[c]) tree[p].son[c] = newnode();
        p = tree[p].son[c];
    }
    tree[p].word = 1;
    ed[p] = 1;
}

void getfail() {
    for(int i=0; i<10; i++) tree[0].son[i] = 1;
    queue<int>q;
    q.push(1);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        int ufail = tree[u].fail;
        ed[u]|=ed[ufail];
        tree[u].last = tree[ufail].word>0?ufail:tree[ufail].last;
        for(int i=0; i<10; i++) {
            int v = tree[u].son[i];
            if(v) {
                tree[v].fail = tree[ufail].son[i];
                q.push(v);
            } else tree[u].son[i] = tree[ufail].son[i];
        }
    }

}

struct mat{
    int g[N][N];
    void init(){
        mem(g,0);
        for(int i=0;i<N;i++) g[i][i] = 1;
    }
};

void mul(mat &a,mat b){
    mat c;
    mem(c.g,0);
    for(int i=1;i<=cnt;i++){
        for(int j=1;j<=cnt;j++){
            for(int k=1;k<=cnt;k++){
                c.g[i][j] = (c.g[i][j]+a.g[i][k]*b.g[k][j])%mod;
            }
        }
    }
    a = c;
}

mat qpow(mat a,int b){
    mat c;
    c.init();
    while(b){
        if(b&1) mul(c,a);
        b>>=1;
        mul(a,a);
    }
    return c;
}


signed main() {

#ifdef xiaofan
    clock_t stTime = clock();
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
/*==========================begin============================*/
    IOS;
    cin>>n>>m>>mod;
    string s;
    cin>>s;
    init();
    ins(s);
    getfail();
    mat ans;
    mem(ans.g,0);
    for(int i=1;i<=cnt;i++){
        for(int j=0;j<10;j++){
            if(!ed[i] && !ed[tree[i].son[j]]) ans.g[i][tree[i].son[j]]++;
        }
    }
    int res = 0;
    ans = qpow(ans,n);
    for(int i=1;i<=cnt;i++) res = (res+ans.g[1][i])%mod;
    cout<<res<<endl;





/*===========================end=============================*/
#ifdef xiaofan
    cerr << "Time Used:" << clock() - stTime << "ms" <<endl;
#endif
    return 0;
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}