HDU - 3652 数位dp水题

题意:

找到[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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值