数位DP模板
2014-07-26 21:24
417 查看
解释一下:dp数组只保存!limit和!first的状态
dp数组保存的是,当前位确定后,之后的数字没有限制的结果,显然当limit或者first时候是不适合的。
first的时候是没必要记录的,因为到当前状态只有一条路径(当前位前边全零)
limit的时候也是没必要记录的,因为到当前状态只有一条路径(当前位前边和待求得串相同)
const int MAX_DIGITS, MAX_STATUS;
LL f[MAX_DIGITS][MAX_STATUS], bits[MAX_DIGITS];
LL dfs(int position, int status, bool limit, bool first)
{
if (position == -1)
return s == target_status;
if (!limit && !first && ~f[position][status])
return f[position][status];
int u = limit ? bits[position] : MAX_BITS;
LL ret = 0;
for (int i = 0; i <= u; i++)
{
ret += dfs(position - 1, next_status(status, i), limit && i == u, first && !i);
}
return limit || first ? ret : f[pos][status] = ret;
}
LL calc(LL n)
{
CLR(f, -1);
int len = 0;
while (n)
{
bits[len++] = n % 10;
n /= 10;
}
return dfs(len - 1, 0, true, true);
}
int main()
{
//freopen("0.txt", "r", stdin);
LL a, b;
while (cin >> a >> b)
cout << calc(b) - calc(a - 1) << endl;
return 0;
}
dp数组保存的是,当前位确定后,之后的数字没有限制的结果,显然当limit或者first时候是不适合的。
first的时候是没必要记录的,因为到当前状态只有一条路径(当前位前边全零)
limit的时候也是没必要记录的,因为到当前状态只有一条路径(当前位前边和待求得串相同)
const int MAX_DIGITS, MAX_STATUS;
LL f[MAX_DIGITS][MAX_STATUS], bits[MAX_DIGITS];
LL dfs(int position, int status, bool limit, bool first)
{
if (position == -1)
return s == target_status;
if (!limit && !first && ~f[position][status])
return f[position][status];
int u = limit ? bits[position] : MAX_BITS;
LL ret = 0;
for (int i = 0; i <= u; i++)
{
ret += dfs(position - 1, next_status(status, i), limit && i == u, first && !i);
}
return limit || first ? ret : f[pos][status] = ret;
}
LL calc(LL n)
{
CLR(f, -1);
int len = 0;
while (n)
{
bits[len++] = n % 10;
n /= 10;
}
return dfs(len - 1, 0, true, true);
}
int main()
{
//freopen("0.txt", "r", stdin);
LL a, b;
while (cin >> a >> b)
cout << calc(b) - calc(a - 1) << endl;
return 0;
}
相关文章推荐
- 数位dp 模板 poj 2089
- 数位DP导学模板
- 数位dp基础(类似模板) HDU 2089+3555
- 数位dp模板
- HDU 2089 不要62(数位DP入门+模板)
- 数位DP模板详解
- hdu2089 不要62(数位dp模板)
- HDU 2089 不要62(数位DP入门+模板)
- HDU-2089 不要62 数位DP 模板题
- SPOJ Balanced Numbers (数位DP+3进制状压)【模板】
- 【HDU 3555】Bomb 数位dp模板
- HDU 3555 Bomb(数位DP模板啊两种形式)
- 数位dp模板
- 数位DP入门 HDOJ2089学模板
- POJ-3286 + 数位DP模板
- 数位dp模板
- HDU 2089 不要62(数位DP入门+模板)
- 数位DP模板
- C++——数位DP模板——Windy数【四川省选2009】
- 数位dp模板