hdu3652——B-number(数位dp变形)

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

加了一个还要被13整除的条件,那就在状态数组多加一项表示被13整除的情况,当这项等于0说明能被13整除

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 10000000005
#define Mod 10001
using namespace std;
int dight[30],dp[30][3][13];
int dfs(int pos,int s,bool limit,int mod)
{
    if(pos==0)
        return s==2&&mod==0;
    if(!limit&&dp[pos][s][mod]!=-1)
        return dp[pos][s][mod];
    int end,ret=0;
    if(limit)
        end=dight[pos];
    else
        end=9;
    for(int d=0;d<=end;++d)
    {
        int have=s,nmod=(mod*10+d)%13;
        if(s==1&&d==3)
            have=2;
        if(s==0&&d==1)
            have=1;
        if(s==1&&d!=1&&d!=3)
            have=0;
        ret+=dfs(pos-1,have,limit&&d==end,nmod);
    }
    if(!limit)
        dp[pos][s][mod]=ret;
    return ret;

}
int solve(int a)
{
    memset(dight,0,sizeof(dight));
    int cnt=1;
    while(a!=0)
    {
        dight[cnt++]=a%10;
        a/=10;
    }
    return dfs(cnt-1,0,1,0);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int n;
    while(~scanf("%d",&n))
    {
        printf("%d\n",solve(n));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值