您的位置:首页 > 其它

HDU - 3652 数位dp水题

2017-02-14 22:16 302 查看

题意:

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3652
找到[1,n]中的满足数位上有子串13,且mod13 == 0的数字有多少个。

思路:

数位dp水题

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;

int a[20];
ll dp[20][15][15][2];

ll dfs(int pos, int mod, int pre, bool flag, bool limit) {
if (pos == -1) {
if (mod == 0 && flag) return 1;
return 0;
}
if (!limit && dp[pos][mod][pre][flag] != -1) return dp[pos][mod][pre][flag];
int up = limit ? a[pos] : 9;
ll res = 0;
for (int i = 0; i <= up; i++) {
bool tag = flag | (pre == 1 && i == 3);
res += dfs(pos - 1, (mod * 10 + i) % 13, i, tag, limit && a[pos] == i);
}
if (!limit) dp[pos][mod][pre][flag] = res;
return res;
}

ll solve(ll x) {
int pos = 0;
while (x) {
a[pos++] = x % 10;
x /= 10;
}
return dfs(pos - 1, 0, 0, false, true);
}

int main() {
int n;
memset(dp, -1, sizeof(dp));
while (scanf("%d", &n) != EOF) {
printf("%lld\n", solve(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu acm 数位dp