B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5639 Accepted Submission(s): 3248
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
Author
wqb0039
Source
题意i:求1~n中包含13且能被13整除的数的个数。
思路:见代码。
# include <stdio.h> # include <string.h> int a[11], dp[11][14][2][2];//dp[数位][上一位取模结果][是否已包含13][上一位是否为1]。 int dfs(int pos, int num, int pre, int sta, bool limit) { if(pos==-1) return sta && num==0; if(!limit && (dp[pos][num][sta][pre==1] != -1)) return dp[pos][num][sta][pre==1]; int up = limit?a[pos]:9; int tmp = 0; for(int i=0; i<=up; ++i) tmp += dfs(pos-1, (num*10+i)%13, i, (pre==1&&i==3)||sta, limit&&(i==a[pos])); if(!limit) dp[pos][num][sta][pre==1] = tmp; return tmp; } int solve(int num) { int cnt = 0; while(num) { a[cnt++] = num%10; num /= 10; } return dfs(cnt-1, 0, 0, 0, true); } int main() { int n; memset(dp, -1, sizeof(dp)); while(~scanf("%d",&n)) printf("%d\n",solve(n)); return 0; }