总算自己套模版套出来一道 虽然是思维 不过代码比三维的精简
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <iterator>
#include <cmath>
#include <deque>
#include <stack>
#include <cctype>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 10;
const int INF = 0xfffffff;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const ld PI = acos (-1.0);
#define INFL 0x7fffffffffffffffLL
#define met(a, b) memset(a, b, sizeof(a))
#define put(a) cout << setiosflags(ios::fixed) << setprecision(a)
ll n;
int dp[15][20][10][2], bit[15];
int calc (ll n);
int dfs (int len, int mod, int pre, bool is13, bool ismax);
int main ()
{
met (dp, -1);
for (; cin >> n; cout << calc(n) << endl);
return 0;
}
int calc (ll x)
{
int len = 0;
for (;x; bit[len++] = x % 10, x /= 10);
return dfs (len-1, 0, false, false, true);
}
int dfs (int len, int mod, int pre, bool is13, bool ismax)
{
if (len == -1) return is13 && ((mod + 13)% 13 == 0);
if (!ismax && dp[len][mod][pre][is13] != -1) return dp[len][mod][pre][is13];
ll cnt = 0;
int maxnum = ismax ? bit[len] : 9;
for (int i=0; i<=maxnum; i++)
cnt += dfs(len - 1, (mod * 10 + i) % 13, i, is13 || (pre == 1 && i == 3), ismax && i == maxnum);
return ismax ? cnt : dp[len][mod][pre][is13] = cnt;
}