作者:
炽热的
,
2023-03-16 16:15:46
,
所有人可见
,
阅读 44
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int len;
int nums[15], f[15][15][15][2];
int dfs(int pos, int pre, int mod, int has, int limit) {
if (!pos) return !mod && has;
if (!limit && ~f[pos][pre][mod][has]) return f[pos][pre][mod][has];
int up = limit ? nums[pos] : 9, res = 0;
for (int i = 0; i <= up; i ++ )
res += dfs(pos - 1, i, (mod * 10 + i) % 13, has || (pre == 1 && i == 3), limit && i == up);
return limit ? res : f[pos][pre][mod][has] = res;
}
int calc(int x) {
len = 0;
while (x) nums[ ++ len] = x % 10, x /= 10;
return dfs(len, 0, 0, 0, 1);
}
int main() {
int r;
memset(f, -1, sizeof f);
while (cin >> r)
cout << calc(r) << endl;
return 0;
}