B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8375 Accepted Submission(s): 4977
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
#include<cstdio>
#include<cstring>
int s[10];int dp[10][13][3];
inline int dfs(int pos,int mod,int have,bool lim)
{
if(pos<=0)return mod==0&&have==2;
if(!lim&&dp[pos][mod][have]!=-1)return dp[pos][mod][have];
int num=lim?s[pos]:9;int ans=0;
for(int i=0;i<=num;i++)
{
int mod_x=(mod*10+i)%13;
int 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,lim&&i==num);
}
if(!lim)dp[pos][mod][have]=ans;
return ans;
}
int main()
{
int n;int t;
while(scanf("%d",&n)!=EOF)
{
memset(s,0,sizeof(s));memset(dp,-1,sizeof(dp));t=0;
while(n)
{
s[++t]=n%10;
n/=10;
}
printf("%d\n",dfs(t,0,0,1));
}
}