(x)代表数字范围在([1,n]), (tar)是要统计的数字, (k)是进制.
#include <bits/stdc++.h>
using namespace std;
long long p[100];
long long dig[100];
long long tmp[100];
long long dfs(long long pos, bool limit, bool lead0, long long k, long long tar, long long x)
{
if (pos == -1) return 0;
if (!limit && !lead0 && tmp[pos] != -1) return tmp[pos];
long long ret = 0;
int up = limit ? dig[pos] : k-1;
for (int i = 0; i <= up; ++i)
{
if (i == tar)
{
if (lead0 && i == 0)
{
ret += dfs(pos-1, limit && i == up, lead0 && i == 0, k, tar, x);
}else if(limit && i == up)
{
ret += x % p[pos] + 1 + dfs(pos-1, limit && i == up, lead0 && i == 0, k, tar, x);
}else{
ret += p[pos] + dfs(pos-1, limit && i == up, lead0 && i == 0, k, tar, x);
}
}else
ret += dfs(pos-1, limit && i == up, lead0 && i == 0, k, tar, x);
}
if (!limit && !lead0) tmp[pos] = ret;
return ret;
}
long long swdp(long long x, long long k, long long tar)
{
memset(tmp, -1, sizeof(tmp));
int len = 0;
long long _x = x;
while (_x)
{
dig[len++] = _x%k;
_x /= k;
}
p[0] = 1; for (int i = 1; i < len; ++i) p[i] = p[i-1] * k;
return dfs(len-1, 1, 1, k, tar, x);
}
int main()
{
long long x, k, tar;
scanf("%lld%lld%lld
", &x, &k, &tar);
printf("%lld
", swdp(999999999, 10 ,1));
}