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
題意:與“不要62”相反,即“要13”
#include<stdio.h>
#include<string.h>
bool flag;
int now, len;
int numb[30]; //存儲數字
int note[30][20][20];
int dfs(int now, bool flag, int left, int key){
if(now == len){
if(!left && key == 2) return 1; //能被13整除,有13
return 0;
}
if(flag == false && note[now][left][key] != -1) return note[now][left][key];
int bound = 9;
if(flag) bound = numb[now + 1];//bound = flag ? num[now + 1] : 9; //如果當前位數到邊界下一位數範圍存在約束(0~numb[now + 1])否則範圍無約束(即0~9)
int ret = 0;
for(int i = 0; i <= bound; i++){
int lleft = left*10 + i;
int k = 0;
lleft %= 13;
if(i == 1) k = 1;
if(key == 1 && i == 3) k = 2; //有13
if(key == 2) k = 2;
ret += dfs(now + 1, flag == true && i == bound, lleft, k);
}
if(flag == false) note[now][left][key] = ret;
return ret;
}
int main(){
int na, n;
char a[30];
while(~scanf("%d", &na)){
sprintf(a, "%d", na);
n = 0;
len = strlen(a);
for(int i = 0; i < len; i++) numb[i + 1] = a[i] - '0';
memset(note, -1, sizeof(note));
n = dfs(0, true, 0, 0); //計算0~na之間的13的數的個數
printf("%d\n", n); //sum即1~na之間有13的數的個數
}
return 0;
}