您的位置:首页 > 其它

[HDU]3652 B-number 数位dp

2017-07-05 14:05 501 查看
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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: