1.请问 [l,r] 中有多少个数字 a
满足数字中存在 3 个连续的数位 ai,ai+1,ai+2 使得 ai<ai+1<ai+2,其中 ai 表示 a 从左到右数第 i 位上的值。
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
ll L, r;
ll f[22][2][2][2][22][22];
int a[30], l;
ll dfs(int len, bool ok, bool lim, bool lead, int last, int cnt) {
if(len == l) {
return ok;
}
if(f[len][ok][lim][lead][last][cnt] != -1) {
return f[len][ok][lim][lead][last][cnt];
}
ll res = 0;
for(int i = 0; i <= (lim ? a[len] : 9); i ++) {
int ncnt = 1;
if(!i && lead) {
ncnt = 0;
}
if(i > last) {
ncnt = min(cnt + 1, 3);
}
res += dfs(len+1, ok | ncnt == 3, lim & i == a[len], lead & !i, i, ncnt);
}
return f[len][ok][lim][lead][last][cnt] = res;
}
ll cal(ll x) {
memset(f, -1, sizeof f);
l = 0;
while(x) {
a[l ++] = x % 10;
x /= 10;
}
reverse(a, a + l);
return dfs(0, 0, 1, 1, 0, 0);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> L >> r;
cout << cal(r) - cal(L-1);
}