【题解】B-number

B-number

Description

一个wqb号或简称B号是一个非负整数,其十进制形式包含子串“13”,可以除以13.例如,130和2613是wqb数,而143和 2639不是。 您的任务是为给定的整数n计算从1到n的wqb数。

(原文: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

输入直到EOF。 在每行中,有一个正整数n(1 <= n <= 1000000000)。



Output

在每行中输出每个答案。


Sample Input

13

100

200

1000




Sample Output

1

1

2

2


纯数位dp题

记忆化搜索:对于dfs(len,mod,status,full)中:

len代表还剩的数字位数,

mod代表现在的数除以13的余数,

status:

==0 代表上一位不是1

==1 代表上一位是1

==2 代表已经含有串“13”。


代码:

#include <cstdio>
#include <algorithm>
using namespace std;

#define LAST_NOT_1 0
#define LAST_IS_1 1
#define HAS_13 2

int f[11][13][3],num[11];

int dfs(int len,int mod,int status,bool full)
{
	if(!len) return !mod && status==HAS_13;
	if(!full && f[len][mod][status])return f[len][mod][status];
	int sum=0;
	for(int i=0;i<=(full?num[len]:9);++i)
	{
		if(status==HAS_13 || (status==LAST_IS_1 && i==3))
			sum+=dfs(len-1,(mod*10+i)%13,HAS_13,full && i==num[len]);
		else
			sum+=dfs(len-1,(mod*10+i)%13,i==1?LAST_IS_1:LAST_NOT_1,full && i==num[len]);
	}
	if(!full)f[len][mod][status]=sum;
	return sum;
}

int work(int x)
{
	num[0]=0;
	while(x)
	{
		num[++num[0]]=x%10;
		x/=10;
	}
	return dfs(num[0],0,LAST_NOT_1,true);
}

int main()
{
	int n;
	while(~scanf("%d",&n))printf("%d\n",work(n));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值