题意
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;
}