#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 10000005
#else
#define M 20000
#define N 20000
#endif
int m, q, dp[N], yes[N], freq[N];
priority_queue<int, vector<int>, greater<int>> pq;
int main()
{
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;
}
auto factorize = [&](int n) {
vector<int> f, nf;
for (int d : {2, 3, 5}) {
if (n % d == 0) {
f.push_back(d);
n /= d;
while (n % d == 0) n /= d;
}
}
static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6};
int i = 0;
for (long long d = 7; d * d <= n; d += increments[i++]) {
if (n % d == 0) {
f.push_back(d);
n /= d;
while (n % d == 0) n /= d;
}
if (i == 8) i = 0;
}
if (n > 1) f.push_back(n);
return f;
};
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);
};
for (int i = 0; i < m; ++i) push(0);
for (int i = 1; i < N; ++i)
{
auto F = factorize(i);
for (auto x : F) if (yes[x] and dp[i-x] < N)
del(dp[i-x]);
dp[i] = min(dp[i], top() + 1);
if (dp[i] < 1e9)
for (auto x : F) if (yes[x])
push(dp[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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1037 ms |
41808 KB |
Time limit exceeded |
2 |
Execution timed out |
1066 ms |
42212 KB |
Time limit exceeded |
3 |
Execution timed out |
1044 ms |
41812 KB |
Time limit exceeded |
4 |
Execution timed out |
1071 ms |
41768 KB |
Time limit exceeded |
5 |
Execution timed out |
1008 ms |
42320 KB |
Time limit exceeded |
6 |
Execution timed out |
1055 ms |
42016 KB |
Time limit exceeded |
7 |
Execution timed out |
1047 ms |
41812 KB |
Time limit exceeded |
8 |
Execution timed out |
1002 ms |
41780 KB |
Time limit exceeded |
9 |
Execution timed out |
1060 ms |
43972 KB |
Time limit exceeded |
10 |
Execution timed out |
1072 ms |
42056 KB |
Time limit exceeded |
11 |
Execution timed out |
1048 ms |
41812 KB |
Time limit exceeded |
12 |
Execution timed out |
1075 ms |
41816 KB |
Time limit exceeded |
13 |
Execution timed out |
1067 ms |
44028 KB |
Time limit exceeded |
14 |
Execution timed out |
1050 ms |
43868 KB |
Time limit exceeded |
15 |
Execution timed out |
1063 ms |
42028 KB |
Time limit exceeded |
16 |
Execution timed out |
1038 ms |
41812 KB |
Time limit exceeded |
17 |
Execution timed out |
1064 ms |
41768 KB |
Time limit exceeded |
18 |
Execution timed out |
1024 ms |
41764 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1043 ms |
46800 KB |
Time limit exceeded |
2 |
Execution timed out |
1031 ms |
80912 KB |
Time limit exceeded |
3 |
Execution timed out |
1097 ms |
81608 KB |
Time limit exceeded |
4 |
Execution timed out |
1069 ms |
44404 KB |
Time limit exceeded |
5 |
Execution timed out |
1053 ms |
56220 KB |
Time limit exceeded |
6 |
Execution timed out |
1068 ms |
44180 KB |
Time limit exceeded |
7 |
Execution timed out |
1072 ms |
46924 KB |
Time limit exceeded |
8 |
Execution timed out |
1042 ms |
44188 KB |
Time limit exceeded |
9 |
Execution timed out |
1053 ms |
56252 KB |
Time limit exceeded |
10 |
Execution timed out |
1044 ms |
81072 KB |
Time limit exceeded |
11 |
Execution timed out |
1038 ms |
64176 KB |
Time limit exceeded |
12 |
Execution timed out |
1060 ms |
44448 KB |
Time limit exceeded |
13 |
Execution timed out |
1046 ms |
44756 KB |
Time limit exceeded |
14 |
Execution timed out |
1024 ms |
44304 KB |
Time limit exceeded |
15 |
Execution timed out |
1061 ms |
54624 KB |
Time limit exceeded |
16 |
Execution timed out |
1069 ms |
85028 KB |
Time limit exceeded |
17 |
Execution timed out |
1059 ms |
44764 KB |
Time limit exceeded |
18 |
Execution timed out |
1058 ms |
69048 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1048 ms |
63712 KB |
Time limit exceeded |
2 |
Execution timed out |
1026 ms |
54464 KB |
Time limit exceeded |
3 |
Execution timed out |
1057 ms |
63428 KB |
Time limit exceeded |
4 |
Execution timed out |
1069 ms |
44764 KB |
Time limit exceeded |
5 |
Execution timed out |
1077 ms |
62416 KB |
Time limit exceeded |
6 |
Execution timed out |
1027 ms |
44752 KB |
Time limit exceeded |
7 |
Execution timed out |
1062 ms |
63300 KB |
Time limit exceeded |
8 |
Execution timed out |
1067 ms |
63928 KB |
Time limit exceeded |
9 |
Execution timed out |
1072 ms |
64440 KB |
Time limit exceeded |
10 |
Execution timed out |
1035 ms |
44404 KB |
Time limit exceeded |
11 |
Execution timed out |
1038 ms |
44500 KB |
Time limit exceeded |
12 |
Execution timed out |
1058 ms |
44968 KB |
Time limit exceeded |
13 |
Execution timed out |
1016 ms |
54968 KB |
Time limit exceeded |
14 |
Execution timed out |
1046 ms |
41816 KB |
Time limit exceeded |
15 |
Execution timed out |
1020 ms |
44760 KB |
Time limit exceeded |
16 |
Execution timed out |
1072 ms |
44764 KB |
Time limit exceeded |
17 |
Execution timed out |
1076 ms |
54524 KB |
Time limit exceeded |
18 |
Execution timed out |
1066 ms |
54144 KB |
Time limit exceeded |
19 |
Execution timed out |
1038 ms |
47836 KB |
Time limit exceeded |
20 |
Execution timed out |
1063 ms |
64068 KB |
Time limit exceeded |
21 |
Execution timed out |
1068 ms |
41564 KB |
Time limit exceeded |
22 |
Execution timed out |
1073 ms |
66120 KB |
Time limit exceeded |
23 |
Execution timed out |
1032 ms |
47816 KB |
Time limit exceeded |
24 |
Execution timed out |
1077 ms |
44016 KB |
Time limit exceeded |
25 |
Execution timed out |
1038 ms |
44256 KB |
Time limit exceeded |
26 |
Execution timed out |
1027 ms |
44748 KB |
Time limit exceeded |
27 |
Execution timed out |
1058 ms |
83488 KB |
Time limit exceeded |
28 |
Execution timed out |
1028 ms |
44492 KB |
Time limit exceeded |
29 |
Execution timed out |
1035 ms |
64612 KB |
Time limit exceeded |
30 |
Execution timed out |
1064 ms |
55984 KB |
Time limit exceeded |
31 |
Execution timed out |
1059 ms |
45040 KB |
Time limit exceeded |
32 |
Execution timed out |
1067 ms |
44172 KB |
Time limit exceeded |
33 |
Execution timed out |
1027 ms |
43864 KB |
Time limit exceeded |
34 |
Execution timed out |
1070 ms |
62984 KB |
Time limit exceeded |
35 |
Execution timed out |
1043 ms |
44304 KB |
Time limit exceeded |
36 |
Execution timed out |
1065 ms |
64460 KB |
Time limit exceeded |
37 |
Execution timed out |
1024 ms |
62448 KB |
Time limit exceeded |
38 |
Execution timed out |
1010 ms |
44972 KB |
Time limit exceeded |
39 |
Execution timed out |
1067 ms |
44288 KB |
Time limit exceeded |
40 |
Execution timed out |
1055 ms |
45796 KB |
Time limit exceeded |
41 |
Execution timed out |
1073 ms |
76552 KB |
Time limit exceeded |
42 |
Execution timed out |
1077 ms |
44752 KB |
Time limit exceeded |