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.
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Output
Print each answer in a single line.
Sample Input
13
100
200
1000
Sample Output
1
1
2
2
题意: 求范围内所有含有“13”且能被13整除数的数目
思路:
多加一维mod即可,返回的时候判断mod是否等于0且是否含有13.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[20][20][20];
int digit[20];
int dfs(int len,int if13,int mod,int limit)
{
if(len == 0)
{
if(if13 == 2 && mod == 0)return 1;
return 0;
}
if(!limit && dp[len][if13][mod])return dp[len][if13][mod];
int up = limit ? digit[len] : 9;
int ans = 0;
for(int i = 0;i <= up;i++)
{
int sta = 0;
if(if13 == 2)sta = 2;
else if(if13 == 1 && i == 3)sta = 2;
else if(i == 1)sta = 1;
ans += dfs(len - 1,sta,(mod * 10 + i) % 13,i == up && limit);
}
return limit ? ans : dp[len][if13][mod] = ans;
}
int solve(int x)
{
int len = 0;
while(x)
{
digit[++len] = x % 10;
x /= 10;
}
return dfs(len,0,0,1);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
printf("%d\n",solve(n));
}
return 0;
}