码迷,mamicode.com
首页 > 其他好文 > 详细

【DP练习】B-number(HDU3652)

时间:2020-07-15 23:20:22      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:tar   lse   and   output   mem   ++i   ide   hdu   inpu   

Description

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.
求从1到N,含有13且能被13整除的数的个数

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


  • 简单数位DP题

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int bit[15],dp[14][14][2][2];
int dfs(int x,int mod,int last,int have,int flag)//
{
	if(x==1) return (!mod&&have);
	if(!flag&&dp[x][mod][last][have]!=-1) return dp[x][mod][last][have];
	int count=flag?bit[x-1]:9,ans=0;
	for(int i=0;i<=count;++i)
	{
		if(last&&i==3) ans+=dfs(x-1,(mod*10+i)%13,0,1,flag&&i==count);
		else ans+=dfs(x-1,(mod*10+i)%13,i==1,have,flag&&i==count);
	}
	return flag?ans:dp[x][mod][last][have]=ans;
}
int start(int x)
{
	int len=0;
	for(;x;x/=10) bit[++len]=x%10;
	memset(dp,-1,sizeof(dp));
	return dfs(len+1,0,0,0,1);
}
int main()
{
	int n;
	while(~scanf("%d",&n)) printf("%d\n",start(n));
	return 0;
}

【DP练习】B-number(HDU3652)

标签:tar   lse   and   output   mem   ++i   ide   hdu   inpu   

原文地址:https://www.cnblogs.com/wuwendongxi/p/13307151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!