HDU 3652 B-number

来源:互联网 发布:淘宝全屏店招怎么做 编辑:程序博客网 时间:2024/05/16 17:32
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
131002001000
 

Sample Output
1122
 

Author
wqb0039
 

Source
2010 Asia Regional Chengdu Site —— Online Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3651 3654 3653 3659 3658 

 


数位dp +记忆化搜索!

dp[i][j][k]表示长度为i,余数为j,状态为k,个数。

#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <iostream>#include <queue>#include <stack>using namespace std;typedef long long ll;int dp[20][15][3];int bit[20];int dfs(int pos,int mod,int have,int flag){    int mod_x,have_x;    int num;    if(pos<=0)        return mod==0 && have==2;    if(!flag &&dp[pos][mod][have]!=-1)        return dp[pos][mod][have];    num=flag?bit[pos]:9;    int i;    int ans=0;    for(i=0; i<=num; i++)    {        mod_x=(mod*10+i)%13;        have_x=have;        if(have==0 &&i==1)            have_x=1;        if(have==1 &&i!=1)            have_x=0;        if(have==1 &&i==3)            have_x=2;        ans+=dfs(pos-1,mod_x,have_x,flag&&i==num);    }    if(!flag)        dp[pos][mod][have]=ans;    return ans;}int main(){    int n;    while(cin>>n)    {        int len=0;        memset(dp,-1,sizeof(dp));        memset(bit,0,sizeof(0));        while(n)        {            bit[++len]=n%10;            n/=10;        }        cout<<dfs(len,0,0,1)<<endl;    }    return 0;}

0 0
原创粉丝点击