solution
d p [ p o s ] [ s t a t e ] [ i s 4 ] [ i s 8 ] dp[pos][state][is4][is8] dp[pos][state][is4][is8] s t a t e = 0 , 表 示 当 前 数 字 没 有 连 续 state=0,表示当前数字没有连续 state=0,表示当前数字没有连续 s t a t e = 1 , 表 示 当 前 连 续 两 个 相 同 数 字 state=1,表示当前连续两个相同数字 state=1,表示当前连续两个相同数字 s t a t e = 2 , 表 示 三 个 连 续 数 字 state=2,表示三个连续数字 state=2,表示三个连续数字
code
/*SiberianSquirrel*//*CuteKiloFish*/
#include <bits/stdc++.h>
//#include<bits/extc++.h>
#include<ext/rope>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define Inv(x) quick_pow(x, mod - 2)
#define Polynomial vector<int>
#define DEBUG(x, y) cout << x << ": " << y << '\n';
#define mem(a, x) memset(a, x, sizeof a)
#define right_1_pos(x) __builtin_ffs(x)
#define left_0_num(x) __builtin_clz(x)
#define right_0_num(x) __builtin_ctz(x)
#define num_of_1(x) __builtin_popcount(x)
#define Pii pair<int, int>
#define mp_(x, y) make_pair(x, y)
#define all(v) (v).begin(), (v).end()
using ld = long double;
using ll = long long;
//using ill = __int128;
using ull = unsigned long long;
using i16 = short;
const ld pi = acos(-1.0);
int inv2, inv3;
tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> tr;
/*
rb_tree_tag 和 splay_tree_tag 选择树的类型(红黑树和伸展树)
null_type//无映射(g++为null_mapped_type)
less<pii>//从小到大排序
tree_order_statistics_node_update//更新方式
tr.insert(mp_(x, y));//插入
tr.erase(mp_(x, y));//删除
tr.order_of_key(pii(x, y));//求排名
tr.find_by_order(x);//找k小值,返回迭代器
tr.join(b);//将b并入tr,前提是两棵树类型一致并且二没有重复元素
tr.split(v, b);//分裂,key小于等于v的元素属于tr,其余属于b
tr.lower_bound(x);//返回第一个大于等于x的元素的迭代器
tr.upper_bound(x);//返回第一个大于x的元素的迭代器
以上的所有操作的时间复杂度均为O(logn)
注意,插入的元素会去重,如set
*/
//-----------------------------------------------------------------IO Template
namespace StandardIO {
template<typename T> inline void read(T &x) {
x = 0; T f = 1;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template<typename T> inline void write(T x) {
if (x < 0) putchar('-'), x *= -1;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
}
using namespace StandardIO;
// -------------------------------------------------------------------------------BASIC_MATH
namespace BASIC_MATH {
// const ll mod = 998244353, mod_g = 3, img = 86583718;
const ll mod = 10000007;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
ll mul(ll a, ll b) { ll z = (long double) a / mod * b; ll res = (unsigned long long) a * b - (unsigned long long) z * mod; return (res + mod) % mod; }
// O(1) quick_mul, use long double
inline ll quick_pow(ll ans, ll p, ll res = 1) {
for(; p; p >>= 1, ans = mul(ans, ans) % mod)
if(p & 1) res = mul(res, ans) % mod;
return res % mod;
}
double gcd(double a,double b) {
if(fabs(b) < eps) return a;
if(fabs(a) < eps) return b;
return gcd(b, fmod(a,b));
}
int gcd(int a, int b) { return __gcd(a, b); }
ll gcd(ll a, ll b) { return __gcd(a, b); }
int sgn(double x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}
}
using namespace BASIC_MATH;
int a[13];
ll dp[13][3][2][2];
ll dfs(int pos, bool limit, bool lead, int state, int pre, bool is4, bool is8) {
if(is4 && is8) return 0;
if(pos == 0) return state == 2;
if(!lead && !limit && dp[pos][state][is4][is8] != -1) return dp[pos][state][is4][is8];
int up = limit? a[pos]: 9;
ll ans = 0;
for(int i = 0; i <= up; ++ i) {
ans += dfs(pos - 1, limit && i == up, lead && i == 0, state == 2? 2: (i == pre? state + 1: 0), i, is4 || i == 4, is8 || i == 8);
}
return dp[pos][state][is4][is8] = ans;
}
ll cal(ll n, int pos = 0, ll ans = 0) {
if(n < 1e10) return 0;
mem(dp, -1);
while(n) {
a[++ pos] = n % 10;
n /= 10;
}
for(int i = 1; i <= a[pos]; ++ i)
ans += dfs(pos - 1, i == a[pos], false, 0, i, i == 4, i == 8);
return ans;
}
void solve() {
ll l, r; cin >> l >> r;
cout << cal(r) - cal(l - 1) << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful!" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}