Description
给定 m m ,,求 m m 到的所有整数中及不含’4’也不含’62’的数的个数。
Solution
直接数位DP即可。。。
Source
/****************************
* Au: Hany01
* Prob: [HDU2089] 不要62
* Date: Feb 19th, 2018
* Email: hany01@foxmail.com
****************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define rep(i , j) for (int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif
inline void File() {
#ifdef hany01
freopen("hdu2089.in" , "r" , stdin);
freopen("hdu2089.out" , "w" , stdout);
#endif
}
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _ , __;
for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxl = 9;
int dp[maxl][2], cnt, a[maxl];
int dfs(int pos, int pre, int sta, int lmt) //'pre' stands for the last number, 'sta' stands for whether the last number is '6'.
{
if (!pos) return 1;
if (!lmt && dp[pos][sta] >= 0) return dp[pos][sta];
register int tmp = 0;
For(i, 0, lmt ? a[pos] : 9) if ((pre != 6 || i != 2) && i != 4) tmp += dfs(pos - 1, i, i == 6, lmt && i == a[pos]);
if (!lmt) dp[pos][sta] = tmp;
return tmp;
}
inline int Solve(int t)
{
cnt = 0;
while (t) a[++ cnt] = t % 10, t /= 10;
return dfs(cnt, -1, 0, 1);
}
int main()
{
File();
register int n, m; Set(dp, -1);
while (n = read(), m = read(), n || m) printf("%d\n", Solve(m) - Solve(n - 1));
return 0;
}