hdu 3652 B-number(数位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.

InputProcess till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).OutputPrint each answer in a single line.Sample Input

13

100

200

1000

Sample Output

1

1

2

2

题意:求1~n的包含13的数字 且 整除13的数的个数

思路:数位dp

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int bits[20];
ll dp[20][20][3]; //i为数位 j为模数 k(0表示非13 1表示有1 2表示有13) 
ll dfs(int len,int isone,bool ismax,int modd){
    if(!len){
        return isone==2&&modd==0; 
    }
    if(!ismax&&dp[len][modd][isone]>=0) return dp[len][modd][isone];
    int up=ismax?bits[len]:9;
    ll ans=0;
    for(int i=0;i<=up;i++){
        int temp=(modd*10+i)%13;
        if(isone==0||(isone==1&&i!=3)) //如果没出现13
        ans+=dfs(len-1,i==1,ismax&&i==up,temp);
        else
        ans+=dfs(len-1,2,ismax&&i==up,temp);
    }
    if(!ismax) dp[len][modd][isone]=ans;
    return ans;
}
ll solve(int n){
    int len=0;
    while(n){
        bits[++len]=n%10;
        n/=10;
    }
    return dfs(len,0,true,0);
}
int main(){
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n){
        memset(dp,-1,sizeof(dp));
        cout<<solve(n)<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wmj6/p/10646013.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值