B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8160 Accepted Submission(s): 4822
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<bits/stdc++.h> using namespace std; int dp[12][15][3],a[12],k[11]; int dfs(int pos,int mod,int f,int p)//位数,对13取模的值,是否有13,是否有上限 { if(pos==-1) return mod==0&&f==2;//能被13整除且含有13 if(!p&&dp[pos][mod][f]!=-1) return dp[pos][mod][f]; int num; if(p)num=a[pos]; else num=9; int ans=0; for(int i=0;i<=num;i++) { int F; if(f==2)F=2;//已经有13 else if(f==1&&i==3)F=2;//上一位是1且这一位是3 else if(i==1)F=1;//这一位是1 else F=0;// ans+=dfs(pos-1,(mod+i*k[pos])%13,F,p&&i==num); } if(!p)dp[pos][mod][f]=ans; return ans; } int main() { int n; memset(dp,-1,sizeof(dp)); k[0]=1; for(int i=1;i<=10;i++) k[i]=k[i-1]*10; while(~scanf("%d",&n)) { memset(a,0,sizeof(a)); int pos=0; while(n) { a[pos++]=n%10; n/=10; } printf("%d\n",dfs(pos-1,0,0,1)); } return 0; }