【数位DP】HDU 3652 B-number

B - B-number
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
题目大意:给定区间[L,R],求在区间内有多少个包含"13"又被13整除的数。

基础的一道数位DP,我们不妨设dp[pos][mod][sta]表示从高位填数至第pos位,除13余数为mod,含有13的状态为sta(1表示上一个数是1,2表示包含 13,0表示没有包含13)的方案数,照这个状态进行记忆化搜索即可。

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

#define MAXLEN 10
#define MAXNUM 9
#define MAXMOD 13
#define INF 0x3f3f3f3f
typedef long long int LL;

int dp[MAXLEN+10][MAXMOD+10][3];
int Num[MAXLEN+10];
int Len;

void GetNum(int x)
{
    memset(Num,0,sizeof(Num));
    Len=0;

    while(x)
    {
        Num[++Len]=x%10;
        x/=10;
    }

    return;
}

int dfs(int len,int mod,int flag,bool lim)
{
    if(len<=0)
    {
        if(mod==0&&flag==2)
            return 1;
        else return 0;
    }

    if(!lim&&dp[len][mod][flag]!=-1)
        return dp[len][mod][flag];

    int Nmax,i,rn,mod_x,flag_x;
    int ans=0;

    Nmax=lim?Num[len]:9;
    for(i=0;i<=Nmax;++i)
    {
        mod_x=(mod*10+i)%13;
        flag_x=flag;

        if(flag==0&&i==1)
            flag_x=1;
        else if(flag==1&&i==3)
            flag_x=2;
        else if(flag==1&&i!=1)
            flag_x=0;

        ans+=dfs(len-1,mod_x,flag_x,lim&&i==Nmax);
    }

    if(!lim)
        dp[len][mod][flag]=ans;

    return ans;
}

int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        memset(dp,-1,sizeof(dp));

        GetNum(N);
        printf("%d\n",dfs(Len,0,0,1));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值