观察样例:当 为 ()时输出 ,为 ,已经十分接近上限 。
我们大胆猜测好数的个数小于一个阈值 。暴力搜出 个好数后排序找到第 小的。为了避免搜出重复的数,可以适当放大阈值。
实测 时可过。
需要注意 并不是合法的答案,所以答案是搜出来的第 小的数。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1500000 + 10;
int n;
ll p[MAXN], po[MAXN];
ll res, tot = 0;
ll ans[MAXN];
ll limit = 1500000;//k = 1.5 * 10 ^7
void dfs(){
if(tot == limit){
//排序 + 去重
sort(ans + 1, ans + 1 + tot);
unique(ans + 1, ans + 1 + tot);
cout << ans[n + 1];
exit(0);
}
ans[++tot] = res;
for(int i = 0; i <= 30; i++){
if(res * po[i] + p[i] < 1e9){
int las = res;
res = res * po[i] + p[i];
dfs();
res = las;
}
}
}
ll get(ll x){
ll cnt = 0;
while(x){
x /= 10;
cnt++;
}
return cnt;
}
int main(){
cin >> n;
p[0] = 1, po[0] = 10;
for(int i = 1; i <= 30; i++){
p[i] = (1 << i), po[i] = pow(10, get(p[i]));
}
dfs();
//为了防止搜不出来,最好在主函数也统计一遍答案。
sort(ans + 1, ans + 1 + tot);
unique(ans + 1, ans + 1 + tot);
cout << ans[n + 1];
return 0;
}