【hdu2089不要62】(数位dp)

http://acm.hdu.edu.cn/showproblem.php?pid=2089

题意:求区间[n,m]中不是不含4或者62的数的个数

代码实现(记忆化+dfs):

#pragma warning(disable:4996)
#include<algorithm>
#include<cstdio>
#include<string>
#include<queue>
#include<stdlib.h>
#include<math.h>
#include<map>
#include<stdlib.h>
#include<stack>
#include<bitset>
#include<string.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;

typedef pair<int, int> pii;
typedef pair<double, int>pdi;
#define ll long long
#define CLR(a,b) memset(a,b,sizeof(a))
const int  mod = 1e9 + 7;
const int  maxn = 2e5 + 5;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

int a[maxn];
int dp[maxn][2];

int dfs(int pos, int is6, int islimit) {
	if (pos == 0)return 1;
	if (!islimit&&dp[pos][is6])return dp[pos][is6];
	int up = islimit ? a[pos] : 9;
	int cnt = 0;
	for (int i = 0; i <= up; i++) {
		if (i == 4 || i == 2 && is6) continue;
		cnt += dfs(pos - 1, i == 6, islimit&&i == up);
	}
	return islimit ? cnt : dp[pos][is6] = cnt;
}

ll solve(int n) {
	int tot = 0;
	while (n) {
		a[++tot] = n % 10;
		n /= 10;
	}
	return dfs(tot, 0, 1);
}

signed main() {
	int n, m;
	//CLR(dp, -1);
	while (~scanf("%d%d", &n, &m), (n + m)) {
		printf("%lld\n", solve(m) - solve(n - 1));
	}
}

猜你喜欢

转载自blog.csdn.net/running_acmer/article/details/81502185