考场上随手写的,洛谷 AC,最慢 242ms,但是没有任何优化,能过吗?
//18 = 9 + 9 = 18.
#include <iostream>
#include <cstdio>
#include <algorithm>
#define Debug(x) cout << #x << "=" << x << endl
using namespace std;
bool vis[10000010];
int a[770000], x[200005];
bool check(int n)
{
while (n)
{
if (n % 10 == 7)
{
return true;
}
n /= 10;
}
return false;
}
void init(int mx)
{
for (int i = 1; i <= mx + 100; i++)
{
if (check(i))
{
for (int j = 1; j * i <= mx + 100; j++)
{
vis[i * j] = true;
}
}
else if (!vis[i])
{
a[++a[0]] = i;
}
}
}
int main()
{
freopen("number.in", "r", stdin);
freopen("number.out", "w", stdout);
int t, mx = 0;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
{
scanf("%d", x + i);
mx = max(mx, x[i]);
}
init(mx);
for (int i = 1; i <= t; i++)
{
if (vis[x[i]])
{
puts("-1");
continue;
}
int id = upper_bound(a + 1, a + a[0] + 1, x[i]) - a;
printf("%d\n", a[id]);
}
return 0;
}