B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6020 Accepted Submission(s): 3473
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> #include<iostream> using namespace std; typedef long long ll; const int N=20; ll n,f[N][13][3];int T,bits[N]; ll dfs(int pos,int mod,int st,bool lim){ if(!pos) return st==2&&!mod; ll &res=f[pos][mod][st],ans=0; if(!lim&&(~res)) return res; int up=!lim?9:bits[pos]; for(int i=up;~i;i--){ int p=(mod*10+i)%13; if(st==2||(st==1&&i==3)) ans+=dfs(pos-1,p,2,lim&&i==bits[pos]); else if(i==1) ans+=dfs(pos-1,p,1,lim&&i==bits[pos]); else ans+=dfs(pos-1,p,0,lim&&i==bits[pos]); } if(!lim) res=ans; return ans; } ll solve(ll x){ int len=0; for(;x;x/=10) bits[++len]=x%10; return dfs(len,0,0,1); } int main(){ memset(f,-1,sizeof f); while(~scanf("%I64d",&n)) printf("%I64d ",solve(n)); return 0; }