您的位置:首页 > 其它

HDU - 3652 HDU - 3652 (数位DP&记忆化dfs)

2016-05-17 22:39 288 查看
HDU - 3652
B-number

Time Limit:                                                        1000MS                        Memory Limit: 32768KB 64bit IO Format:                            %I64d & %I64u                       
SubmitStatus

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

               

Hint

Source
2010 Asia Regional Chengdu Site —— Online Contest
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[20][20][3];
int bit[20];
int dfs(int pos,int pre,int h,bool f)
{
if(pos==-1) return h==2&&pre==0;
if(!f&&dp[pos][pre][h]!=-1) return dp[pos][pre][h];
int mm=f?bit[pos]:9;
int ans=0;
for(int i=0;i<=mm;i++)
{
int npre=(pre*10+i)%13;
int nh=h;
if(h==0&&i==1)
nh=1;
else if(h==1&&i!=1)
nh=0;
if(h==1&&i==3)
nh=2;
ans+=dfs(pos-1,npre,nh,f&&i==mm);
}
if(!f) dp[pos][pre][h]=ans;
return ans;
}
int solve(int n)
{
int len=0;
while(n)
{
bit[len++]=n%10;
n/=10;
}
return dfs(len-1,0,0,1);
}
int main()
{
int n;
memset(dp,-1,sizeof(dp));
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",solve(n));
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: