B-number HDU - 3652 (数位dp)

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

与其叫数位dp,我觉得这种题其实就是搜索

此题我开了4维数组

分别记录当前位数,是否被限制,前面是否为1,前面是否出现了13

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[20][20][2][2],a[20];
int dfs(int pos,int mod,int lim,int sta,int bef)
{

    int s,ans=0;
    if(pos==0) return mod%13==0&&sta==1;
    if(!lim&&dp[pos][mod][bef][sta]!=-1) return dp[pos][mod][bef][sta];
    s=lim==1?a[pos]:9;
    int temp;
    for(int i=0; i<=s; i++)
    {
        temp=(mod*10+i)%13;
        //cout<<pos<<" "<<lim<<" "<<bef<<" "<<"temp="<<temp<<" "<<s<<" "<<i<<endl;
        if(i==1)
        {
            ans+=dfs(pos-1,temp,lim&&i==s,sta,1);
        }
        else if(bef&&i==3)
        {
            ans+=dfs(pos-1,temp,lim&&i==s,1,0);
        }
        else
        {
            ans+=dfs(pos-1,temp,lim&&i==s,sta,0);
        }
    }
    if(!lim) dp[pos][mod][bef][sta]=ans;
    //cout<<ans<<" "<<pos<<" "<<bef<<endl;
    return ans;
}
int solve(int x)
{
    int cnt=0;
    while(x!=0)
    {
        cnt++;
        a[cnt]=x%10;
        x/=10;
    }
    return dfs(cnt,0,1,0,0);
}
int main()
{
    int n;
    memset(dp,-1,sizeof(dp));
    while(~scanf("%d",&n))
    {

        cout<<solve(n)<<endl;
       //printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值