二维记录前五个数的转态和先后顺序 数组53MB水过。。。。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
LL dp[18][100001][4];
LL l, x[20];
bool judge(LL n, LL k, LL sym)
{
k--;
while(n && k)
{
if(n % 10ll == sym)return true;
n /= 10ll;
k--;
}
return false;
}
LL dfs(LL i, bool e, LL pre, bool sym, LL k)
{
if(i == -1) return 1;
if(e && dp[i][pre][k - 2] != -1) return dp[i][pre][k - 2];
LL Max = e ? 9 : x[i], ans = 0;
for(int j = 0; j <= Max; j++)
{
if(j == 0 && sym == 0) ans += dfs(i - 1, !(!e && j == x[i]), pre, sym || j, k);
else
{
if(judge(pre, k, j))continue;
LL pre2 = pre;
pre2 *= 10ll;
pre2 %= 100000ll;
pre2 += j;
ans += dfs(i - 1, !(!e && j == x[i]), pre2, sym || j, k);
}
}
if(e) dp[i][pre][k - 2] = ans;
return ans;
}
LL cal(LL n, LL k)
{
l = 0;
while(n)
{
x[l++] = n % 10ll;
n /= 10ll;
}
return dfs(l - 1, 0, 0, 0, k);
}
int main()
{
LL a, b, k;
memset(dp, -1, sizeof(dp));
while(scanf("%I64d %I64d %I64d", &a, &b, &k)!=EOF) printf("%I64d\n", cal(b, k) - cal(a - 1,k));
return 0;
}