DP练习B-number(HDU3652)
Posted wuwendongxi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DP练习B-number(HDU3652)相关的知识,希望对你有一定的参考价值。
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
",start(n));
return 0;
}
以上是关于DP练习B-number(HDU3652)的主要内容,如果未能解决你的问题,请参考以下文章