链接:
https://vjudge.net/problem/HDU-3652
题意:
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
思路:
数位DP,记录前面的余数,和前一位的数,和是否已经存在13.四维DP。
如果不记录前一位的值,可能出现无法判断13是否存在。
代码:
// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;
LL F[20][20][2][10];
LL dig[20];
LL a, b, n;
LL Dfs(int pos, int pre, int num, int ok, bool lim)
{
if (pos == -1)
{
if (pre == 0 && ok)
return 1;
return 0;
}
if (!lim && F[pos][pre][ok][num] != -1)
return F[pos][pre][ok][num];
int up = lim ? dig[pos] : 9;
LL ans = 0;
for (int i = 0;i <= up;i++)
ans += Dfs(pos-1, ((pre*10)%13+i)%13, i, ok ? ok : (num == 1 && i == 3), lim && i == up);
if (!lim)
F[pos][pre][ok][num] = ans;
return ans;
}
LL Solve(LL x)
{
int p = 0;
while(x)
{
dig[p++] = x%10;
x /= 10;
}
return Dfs(p-1, 0, -1, 0, 1);
}
int main()
{
// freopen("test.in", "r", stdin);
memset(F, -1, sizeof(F));
while(~scanf("%lld", &n))
{
printf("%lld
", Solve(n));
}
return 0;
}