#include <iostream>
#include <queue>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <complex>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,tune=native")
#define ALL(x) begin(x), end(x)
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#if 1
#define M 100005
#define N 5000005
#else
#define M 20000
#define N 20000
#endif
int m, q, dp[N], yes[N], freq[N], i;
priority_queue<int, vector<int>, greater<int>> pq;
int si[4000];
vector<int> prime;
struct Precalc {
unsigned char divisor[1<<16];
constexpr Precalc() : divisor{} {
for (int i = 0; i < (1<<16); i++)
divisor[i] = 1;
for (int i = 2; i * i < (1<<16); i++)
if (divisor[i] == 1)
for (int k = i * i; k < (1<<16); k += i)
divisor[k] = i;
}
};
auto push = [](int x){
pq.push(x);
};
auto del = [](int x){
++freq[x];
};
auto top = [](){
while (pq.size())
{
int top = pq.top();
if (freq[top]) --freq[top], pq.pop();
else return top;
}
return int(1e9);
};
int main()
{
for (int i = 2; i < 4000; ++i)
{
if (si[i]) continue;
for (int j = 2 * i; j < 4000; j += i) si[j] = 1;
}
for (int i = 2; i < 4000; ++i) if (!si[i]) prime.push_back(i);
memset(dp, 63, sizeof dp);
ShinLena;
dp[0] = 0;
cin >> m >> q;
for (int p, i = 1; i <= m; ++i)
{
cin >> p;
yes[p] = 1;
}
constexpr Precalc P{};
auto f = +[](int d){};
decltype(f) ff = nullptr;
auto factorize2 = [&](int n) {
using u64 = unsigned long long;
auto find_factor = [&](u64 n){
if (n < (1 << 16)) return u64(P.divisor[n]);
for (u64 d : prime)
if (d * n < d)
return u64(-1) / d + 1;
return 1ull;
};
u64 d;
do {
if (yes[d]) ff(int(d = find_factor(n)));
n /= d;
} while (d != 1);
};
ff = +[](volatile int x){};
for (int i = 1; i < N; ++i) factorize2(i);
return 0;
for (int i = 0; i < m; ++i) push(0);
for (i = 1; i < N; ++i)
{
ff = +[](int x) { if(dp[i-x] < N) del(dp[i-x]); };
factorize2(i);
dp[i] = min(dp[i], top() + 1);
if (dp[i] < 1e9)
{
ff = +push;
factorize2(i);
}
}
for (int x, i = 1; i <= q; ++i)
{
cin >> x;
if (dp[x] >= 1e9) cout << "oo\n";
else cout << dp[x] << '\n';
}
return 0;
}
Compilation message
brunhilda.cpp: In function 'int main()':
brunhilda.cpp:110:22: warning: 'd' is used uninitialized in this function [-Wuninitialized]
110 | if (yes[d]) ff(int(d = find_factor(n)));
| ~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1055 ms |
22108 KB |
Time limit exceeded |
2 |
Execution timed out |
1080 ms |
22108 KB |
Time limit exceeded |
3 |
Execution timed out |
1087 ms |
22108 KB |
Time limit exceeded |
4 |
Execution timed out |
1042 ms |
22104 KB |
Time limit exceeded |
5 |
Execution timed out |
1012 ms |
22360 KB |
Time limit exceeded |
6 |
Execution timed out |
1004 ms |
22104 KB |
Time limit exceeded |
7 |
Execution timed out |
1084 ms |
22108 KB |
Time limit exceeded |
8 |
Execution timed out |
1090 ms |
22108 KB |
Time limit exceeded |
9 |
Execution timed out |
1028 ms |
22104 KB |
Time limit exceeded |
10 |
Execution timed out |
1029 ms |
22108 KB |
Time limit exceeded |
11 |
Execution timed out |
1029 ms |
22104 KB |
Time limit exceeded |
12 |
Execution timed out |
1040 ms |
22360 KB |
Time limit exceeded |
13 |
Execution timed out |
1066 ms |
22108 KB |
Time limit exceeded |
14 |
Execution timed out |
1063 ms |
22108 KB |
Time limit exceeded |
15 |
Execution timed out |
1025 ms |
22104 KB |
Time limit exceeded |
16 |
Execution timed out |
1045 ms |
22108 KB |
Time limit exceeded |
17 |
Execution timed out |
1041 ms |
22104 KB |
Time limit exceeded |
18 |
Execution timed out |
1027 ms |
22108 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1044 ms |
26204 KB |
Time limit exceeded |
2 |
Execution timed out |
1025 ms |
41304 KB |
Time limit exceeded |
3 |
Execution timed out |
1030 ms |
28724 KB |
Time limit exceeded |
4 |
Execution timed out |
1074 ms |
22108 KB |
Time limit exceeded |
5 |
Execution timed out |
1054 ms |
26460 KB |
Time limit exceeded |
6 |
Execution timed out |
1056 ms |
22108 KB |
Time limit exceeded |
7 |
Execution timed out |
1008 ms |
26200 KB |
Time limit exceeded |
8 |
Execution timed out |
1044 ms |
22108 KB |
Time limit exceeded |
9 |
Execution timed out |
1036 ms |
26716 KB |
Time limit exceeded |
10 |
Execution timed out |
1042 ms |
28764 KB |
Time limit exceeded |
11 |
Execution timed out |
1037 ms |
26456 KB |
Time limit exceeded |
12 |
Execution timed out |
1016 ms |
22108 KB |
Time limit exceeded |
13 |
Execution timed out |
1052 ms |
24156 KB |
Time limit exceeded |
14 |
Execution timed out |
1044 ms |
22108 KB |
Time limit exceeded |
15 |
Execution timed out |
1058 ms |
26460 KB |
Time limit exceeded |
16 |
Execution timed out |
1049 ms |
41284 KB |
Time limit exceeded |
17 |
Execution timed out |
1094 ms |
22104 KB |
Time limit exceeded |
18 |
Execution timed out |
1016 ms |
33112 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1031 ms |
26804 KB |
Time limit exceeded |
2 |
Execution timed out |
1020 ms |
24664 KB |
Time limit exceeded |
3 |
Execution timed out |
1038 ms |
26716 KB |
Time limit exceeded |
4 |
Execution timed out |
1054 ms |
22108 KB |
Time limit exceeded |
5 |
Execution timed out |
1039 ms |
39260 KB |
Time limit exceeded |
6 |
Execution timed out |
1041 ms |
22360 KB |
Time limit exceeded |
7 |
Execution timed out |
1051 ms |
33116 KB |
Time limit exceeded |
8 |
Execution timed out |
1055 ms |
26716 KB |
Time limit exceeded |
9 |
Execution timed out |
1084 ms |
26636 KB |
Time limit exceeded |
10 |
Execution timed out |
1054 ms |
22120 KB |
Time limit exceeded |
11 |
Execution timed out |
1041 ms |
22104 KB |
Time limit exceeded |
12 |
Execution timed out |
1028 ms |
22104 KB |
Time limit exceeded |
13 |
Execution timed out |
1041 ms |
24412 KB |
Time limit exceeded |
14 |
Execution timed out |
1036 ms |
22108 KB |
Time limit exceeded |
15 |
Execution timed out |
1012 ms |
22104 KB |
Time limit exceeded |
16 |
Execution timed out |
1059 ms |
22108 KB |
Time limit exceeded |
17 |
Execution timed out |
1080 ms |
26204 KB |
Time limit exceeded |
18 |
Execution timed out |
1087 ms |
24152 KB |
Time limit exceeded |
19 |
Execution timed out |
1069 ms |
24156 KB |
Time limit exceeded |
20 |
Execution timed out |
1097 ms |
26456 KB |
Time limit exceeded |
21 |
Execution timed out |
1044 ms |
22104 KB |
Time limit exceeded |
22 |
Execution timed out |
1101 ms |
28248 KB |
Time limit exceeded |
23 |
Execution timed out |
1071 ms |
26204 KB |
Time limit exceeded |
24 |
Execution timed out |
1078 ms |
22108 KB |
Time limit exceeded |
25 |
Execution timed out |
1082 ms |
22104 KB |
Time limit exceeded |
26 |
Execution timed out |
1022 ms |
22104 KB |
Time limit exceeded |
27 |
Execution timed out |
1022 ms |
28364 KB |
Time limit exceeded |
28 |
Execution timed out |
1057 ms |
22108 KB |
Time limit exceeded |
29 |
Execution timed out |
1056 ms |
28252 KB |
Time limit exceeded |
30 |
Execution timed out |
1016 ms |
26316 KB |
Time limit exceeded |
31 |
Execution timed out |
1036 ms |
24152 KB |
Time limit exceeded |
32 |
Execution timed out |
1042 ms |
22104 KB |
Time limit exceeded |
33 |
Execution timed out |
1044 ms |
22104 KB |
Time limit exceeded |
34 |
Execution timed out |
1026 ms |
32344 KB |
Time limit exceeded |
35 |
Execution timed out |
1034 ms |
22360 KB |
Time limit exceeded |
36 |
Execution timed out |
1018 ms |
28248 KB |
Time limit exceeded |
37 |
Execution timed out |
1037 ms |
38624 KB |
Time limit exceeded |
38 |
Execution timed out |
1018 ms |
22104 KB |
Time limit exceeded |
39 |
Execution timed out |
1075 ms |
22108 KB |
Time limit exceeded |
40 |
Execution timed out |
1102 ms |
22104 KB |
Time limit exceeded |
41 |
Execution timed out |
1046 ms |
34776 KB |
Time limit exceeded |
42 |
Execution timed out |
1049 ms |
22108 KB |
Time limit exceeded |