题解 P7960 【[NOIP2021] 报数】
StudyingFather · · 题解
这次 T1 算是比较常规。
禁止报的数的生成规则与 埃氏筛法 类似,考虑用筛法预处理可以报出的数字列表和不可报出的数字,从而
具体来说,我们从
小细节:虽然
问题来了,这个做法的时间复杂度是多少呢?是
// Problem: P7960 [NOIP2021] 报数
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7960
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
using namespace std;
const int maxn = 10000000 + 1000;
int vis[maxn + 5], lis[maxn + 5], cnt;
bool check(int x) {
while (x) {
if (x % 10 == 7) return false;
x /= 10;
}
return true;
}
void init() {
for (int i = 1; i <= maxn; i++)
if (vis[i] == 0) {
if (check(i)) {
vis[i] = ++cnt;
lis[cnt] = i;
} else
for (int j = 1; i * j <= maxn; j++) vis[i * j] = -1;
}
}
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
init();
while (T--) {
int x;
cin >> x;
if (vis[x] == -1)
cout << -1 << endl;
else {
cout << lis[vis[x] + 1] << endl;
}
}
return 0;
}