[HDU]3652 B-number 数位dp

来源:互联网 发布:电脑文件数据恢复 编辑:程序博客网 时间:2024/06/06 23:52

B-number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6344 Accepted Submission(s): 3672

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的倍数就在每一位模拟除法取余即可,用一维来维护,有没有十三字符再用一维来维护.

#include<stdio.h>#include<cstring>using namespace std;int dp[15][15][3],bit[15],n,len;int dfs(int pos,int mod,int st,int lim){    if(pos<=0) return mod==0&&st==2;    if(!lim&&dp[pos][mod][st]!=-1) return dp[pos][mod][st];    int ban=lim?bit[pos]:9;    int ans=0,newhave,mod_x=0;    for(int i=0;i<=ban;i++){        mod_x=(mod*10+i)%13;        newhave=st;        if(st==0&&i==1) newhave=1;        if(st==1&&i!=1) newhave=0;        if(st==1&&i==3) newhave=2;        ans+=dfs(pos-1,mod_x,newhave,lim&&i==ban);    }    if(!lim) dp[pos][mod][st]=ans;    return ans;}int main(){    while(scanf("%d",&n)!=EOF){      memset(dp,-1,sizeof(dp));      len=0;      while(n){bit[++len]=n%10;n/=10;}      printf("%d\n",dfs(len,0,0,1));    }}
原创粉丝点击