HDU - 2089

题意:[L,R]中,不含4或62的数字个数。

总结:dp[l][if6] 表示当前是第几位,上一位是否为6

dfs(int l, bool if6, bool sig) sig表示是否越界

#include <bits/stdc++.h>

using namespace std;
const int maxn = 20;
typedef long long ll;

ll dight[maxn], dp[20][2], n, m, tot;

ll dfs(int l, bool if6, bool sig) {
	if(l == 0) return 1;
	if(!sig && dp[l][if6] != -1) return dp[l][if6];
	int nex = sig ?dight[l] :9; ll res = 0;
	for (int i = 0; i <= nex; ++i) {
		if(i == 4 || (if6 && i == 2)) continue;
		res += dfs(l - 1, i == 6, sig && (i == nex));
	}
	if(!sig) dp[l][if6] = res;
	return res;
}
ll calc(ll a) {
	tot = 0;
	while(a) {
		dight[++tot] = a % 10; a /= 10;
	}
	return dfs(tot, 0, 1);
}
int main() {
	while(~scanf("%lld%lld", &n, &m) &&n &&m) {
		memset(dp, -1, sizeof dp);
		printf("%lld\n", calc(m) - calc(n - 1));
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/oi-forever/p/9125897.html
今日推荐