loj#6074. 「2017 山東一輪集訓 Day6」子序列(矩陣乘法 dp)

自為風月馬前卒發表於2019-03-29

題意

題目連結

Sol

\(f[i][j]\)表示前\(i\)個位置中,以\(j\)為結尾的方案數。

轉移的時候判斷一下\(j\)是否和當前位置相同

然後發現可以用矩陣優化,可以分別求出字首積和逆矩陣的字首積(這題的逆矩陣炒雞好求)

這樣就可以\(n*10^3\)

發現相鄰兩個矩陣只有一行不同,那麼其他的可以直接copy。

就可以做到\(n*10^2\)了。

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 1e5 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline int add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline int mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

char s[MAXN];
int N, Q, a[MAXN];
struct Ma {
    int m[11][11];
    Ma() {
        memset(m, 0, sizeof(m));
    }
    void init() {
        for(int i = 0; i < 10; i++) m[i][i] = 1;
    }
    Ma operator * (const Ma &rhs) const {
        Ma ans; 
        for(int i = 0; i < 10; i++)
            for(int j = 0; j < 10; j++) {
                __int128 tp = 0;
                for(int k = 0; k < 10; k++)
                    tp += 1ll * m[i][k] * rhs.m[k][j];
                ans.m[i][j] = tp % mod;
            }
        return ans;         
    }
}suf[MAXN], inv[MAXN];
int solve(int l, int r) {
    Ma tmp = suf[r] * inv[l - 1];
    int ans = 0;
    for(int i = 0; i < 10; i++) add2(ans, tmp.m[i][9]);
    return (ans - 1 + mod) % mod;
}
signed main() {
//  freopen("a.in", "r", stdin);
    scanf("%s", s + 1);
    N = strlen(s + 1);
    for(int i = 1; i <= N; i++) {
        a[i] = s[i] - 'a';
        suf[i].init(); inv[i].init();
        for(int j = 0; j < 10; j++) suf[i].m[a[i]][j] = 1;
        for(int j = 0; j < 10; j++) if(j != a[i]) inv[i].m[a[i]][j] = mod - 1;
    }
    suf[0].init(); inv[0].init();
    for(int i = 2; i <= N; i++) suf[i] = suf[i] * suf[i - 1];
    for(int i = 2; i <= N; i++) inv[i] = inv[i - 1] * inv[i];
    int Q = read();
    while(Q--) {
        int l = read(), r = read();
        cout << solve(l, r) << '\n';
    }
    return 0;
}

相關文章