诡异数字

题目地址:

https://ac.nowcoder.com/acm/problem/20669

基本思路:

考虑维护数位,每次我们维护
表示填到位,前一个数位的数为,它连续出现了次的答案,
对于每次我们只有两个转移方向,要么这一位和上一位相同,,要么不同变为
其余部分都套上数位的板子就可以了。

参考代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define int long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f

inline int read() {
  int x = 0, neg = 1; char op = getchar();
  while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); }
  while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) { putchar('-'); x = -x; }
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}

const int mod = 20020219;
int a[20],limit[20];
int memo[20][10][20];
int l,r,n;
int dp(int pos,int pre,int num,bool flag) {
  if (num > limit[pre]) return 0;
  if (pos == 0) return 1;
  if (flag && memo[pos][pre][num] != -1) return memo[pos][pre][num];
  int cnt = flag ? 9 : a[pos];
  int ans = 0;
  for (int i = 0; i <= cnt; i++) {
    ans = (ans + dp(pos - 1, i, i == pre ? (num + 1) : 1, flag || i < cnt)) % mod;
  }
  if (flag) memo[pos][pre][num] = ans % mod;
  return ans % mod;
}
int calc(int x) {
  int pos = 0;
  while (x) {
    a[++pos] = x % 10;
    x /= 10;
  }
  return dp(pos, 0, 0, false);
}
signed main() {
  IO;
  int t;
  cin >> t;
  while (t--) {
    cin >> l >> r >> n;
    mset(limit, 0x3f);
    rep(i, 1, n) {
      int x, y;
      cin >> x >> y;
      limit[x] = min(limit[x], y);
    }
    mset(memo, -1);
    int ans = (calc(r) - calc(l - 1) + mod) % mod;
    cout << ans << '\n';
  }
  return 0;
}