【HDU2089】不要62【数位DP】【记忆化搜索】

来源:互联网 发布:软件开发是什么工作 编辑:程序博客网 时间:2024/05/16 07:53

【题目链接】

忘记写了个判断条件

如果上一位为6,当前为也为6,那么状态还是1。

/* Telekinetic Forest Guard */#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 20;int dig[maxn];LL dp[maxn][3];// 0 ÎÞ 62// 1 ÓÐ 6// 2 ÓÐ 62 template <class numtype>inline void read(numtype &x) {int f = 0; x = 0; char ch = getchar();for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? 1 : 0;for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';if(f) x = -x;}inline LL dfs(int pos, int state, bool limit) {if(pos == 0) return state != 2;if(!limit && ~dp[pos][state]) return dp[pos][state];int upb = limit ? dig[pos] : 9;LL res = 0;for(int i = 0; i <= upb; i++) if(i ^ 4) {int s = state;if(state == 1) {if(i == 2) s = 2;else if(i != 6) s = 0;}else if(state == 0 && i == 6) s = 1;res += dfs(pos - 1, s, limit && i == dig[pos]);}if(!limit) dp[pos][state] = res;return res;}inline LL calc(LL x) {int top = 0;for(; x; x /= 10) dig[++top] = x % 10;return dfs(top, 0, 1);}int main() {LL l, r;memset(dp, -1, sizeof(dp));while(1) {read(l); read(r);if(!l && !r) break;printf("%lld\n", calc(r) - calc(l - 1));}return 0;}


0 0
原创粉丝点击