正解
我们可以首先考虑在(k)进制下末尾有奇数个(0)的数(m)满足什么性质。
当然,这部分可以通过打表找规律来实现。
我们可以很显然的发现(m=a imes k^p(pequiv1pmod{m},a otequiv0pmod{k}))。
这样一来,我们可以很方便的统计出([1,n])范围内有多少个满足条件的数。
对于需要查找的第(k)大,可以二分上边界得到。
代码
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
LL n,k,c[105];
LL sum(LL x) {
LL tot=0;
for(LL i=x; i>0; i-=2) {
tot+=(k-1)*pow(k,i-2);
}
if(x&1) {
tot++;
}
return tot;
}
bool check(LL x) {
LL cnt=0,tot=0;
while(x) {
c[++cnt]=x%k,x/=k;
}
for(LL i=1; i<=cnt; i++) {
tot+=c[i]*sum(i-1);
}
return tot>=n;
}
int main() {
scanf("%lld %lld",&n,&k);
LL l=1,r=1e18;
while(l<=r) {
LL mid=(l+r)>>1;
if(check(mid)) {
r=mid-1;
} else {
l=mid+1;
}
}
printf("%lld",l);
return 0;
}