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
题意:求<=n,满足包含13且能被13整除的数的个数
题解:简单数位dp
//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF = 0x3f3f3f3f;
const int O = 1e6;
const int mod = 10007;
const int maxn = 1<<5;
const double PI = acos(-1.0);
const double E = 2.718281828459;
const int _ = 1;
const int __ = 2;
const int ___ = 3;
int dp[20][2][2][13]; //dp[i][true][true][j] 后i位,上一位为1,已经满足包含13,余数为j,满足条件的个数
int a[20];
// pos位,sta表示上一位是否为1,ok表示前面的位数是否包含13,rad表示余数,flag表示是否在上限
int dfs(int pos, int sta, int ok, int rad, int flag ){
if(pos == -1) return ok && rad == 0;
if(!flag && dp[pos][sta][ok][rad] != -1) return dp[pos][sta][ok][rad];
int up = flag ? a[pos] : 9;
int sum = 0;
for(int i=0; i<=up; i++) {
int next_rad = (rad * 10 + i) % 13;
sum += dfs(pos-1, i == 1, ok || (sta && i == 3), next_rad, flag && i == a[pos]);
}
if(!flag) dp[pos][sta][ok][rad] = sum;
return sum;
}
int main(){
int n;
MT(dp, -1);
while(~scanf("%d", &n)){
int cnt = 0;
while(n) {
a[cnt ++] = n % 10;
n /= 10;
}
printf("%d\n", dfs(cnt -1, 0, 0, 0, 1));
}
}