HDU - 3652 B-number 【数位DP】

B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9321    Accepted Submission(s): 5555


 

Problem 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.

 

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

 

Author

wqb0039

 

Source

2010 Asia Regional Chengdu Site —— Online Contest

 

Recommend

lcy

因为要求既有“13”的子串,又是13的倍数,记录两个状态,一是是否存在13,二是mod值。

#include "cstdio"
#include "cstring"
#include "queue"
#include "iostream"
#include "vector"
#include "algorithm"
using namespace std;
const int inf = 0x3f3f3f3f;
int dig[10];
int dp[10][2][15][2][2][2];
int dfs(int pos,bool have,int mod,bool sum,bool las,bool lim)//pos表示位置,have表示存在13,mod模数,sum表示是否为0,las上一位是否为1
{
    if(pos==-1)return (have&&mod==0&&sum);
    if(dp[pos][have][mod][sum][las][lim]!=-1)return dp[pos][have][mod][sum][las][lim];
    int maxi=lim?dig[pos]:9;
    int ans=0;
    for (int i = 0; i <= maxi; ++i) {
        ans+=dfs(pos-1,have||(las&&i==3),(mod*10+i)%13,i>0||sum,i==1,lim&&i==maxi);
    }
    dp[pos][have][mod][sum][las][lim]=ans;
    return ans;
}
int get(int n)
{
    memset(dp,-1, sizeof(dp));
    int cnt=0;
    while(n)
    {
        dig[cnt++]=n%10;
        n/=10;
    }
    return dfs(cnt-1,0,0,0,0,1);
}
int main()
{
    int n;
    while(cin>>n)
    {
        printf("%d\n",get(n));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/86062676