您的位置:首页 > 其它

数位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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm dp