【HDU2089】不要62【数位DP】【记忆化搜索】
2016-05-01 10:42
281 查看
【题目链接】
忘记写了个判断条件
如果上一位为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;
}
忘记写了个判断条件
如果上一位为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;
}
相关文章推荐
- 74、JDK安装与环境变量配置
- 设计模式——工厂方法模式
- 剑指offer(61):对称的二叉树
- VS2015 未能正确加载 JavascriptWebExtensionsPackage
- 4.图像标记<img>
- Leetcode:62. Unique Paths
- 使用单片机的定时器0和中断实现9到0的倒计时功能,并用数码管显示
- LA_2659_POJ_3076_ZOJ_3122_Sudoku(DancingLinksX精确覆盖,数独题模板)
- hdu ztr loves lucky numbers (最小的但不小于n的幸运数字是多少,幸运数字只含4和7,且4的个数等于7的个数)
- 闲聊欧拉函数入门
- C程序的存储空间布局
- WinPython-64-2.7安装MySQLdb包
- css3 实现3D导航栏
- Hdu 1372 Knight Moves
- 搜索数据库某列名
- Hackerrank :Gena Playing Hanoi
- centOS中修改语言环境
- 核心编程答案(第二章)
- 通过自定义TextView使得其获得焦点
- 【HUSTOJ】1000: 一个伟大的题