HDU 3652 B-number

来源:互联网 发布:seo方案ppt 编辑:程序博客网 时间:2024/05/16 18:19



B-number


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)




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

总算用记忆化搜索搞定了一个难一点的数位dp了!!


AC代码如下:

///记忆化搜素  500MS 272K#include<iostream>#include<cstdio>#include<cstring>#define mod 13using namespace std;int dp[20][15][4];int num[20];int dfs(int pos,int mo,int status,bool limit){    int i;    //cout<<pos<<"~~~~~~~"<<mo<<"~~~~~"<<status<<"~~~~~~~~"<<limit<<"~~~~~~~"<<dp[pos][status]<<endl;    if(!pos)        return status==2&&mo==0;    if(!limit&&dp[pos][mo][status]!=0) return dp[pos][mo][status];    int end = limit ? num[pos] : 9;    int sum=0;    for(i=0;i<=end;i++)    {        int a=mo;        int flag = status ;        if(flag==0&&i==1) flag=1;        if(flag==1&&i==3) flag=2;        if(flag==1&&i!=1&&i!=3) flag=0;        sum+=dfs(pos-1,(a*10+i)%mod,flag,limit&&i==end);    }    //cout<<"!!!!!!!!!!!"<<sum<<"!!!!!!!!"<<endl;    return limit ? sum : dp[pos][mo][status] = sum;}int _13(int n){    int pos=1;    memset(dp,0,sizeof dp);    while (n>0)    {        num[pos++]=n%10;        n/=10;    }    //cout<<"~~~~~~~~~"<<pos-1<<"~~~~~~~~"<<endl;    return dfs(pos-1,0,0,true);}int main(){    int i,j;    int n;    while(~scanf("%d",&n))    {        printf("%d\n",_13(n));    }    return 0;}





0 0
原创粉丝点击