이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
	There is a simple solution with upper bound on the time equal to O(MAX * log(log(MAX)) * log(MAX)) with DP and maintaining a heap. Probably the actual complexity is smaller.
*/
#include <bits/stdc++.h>
#define endl '\n'
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back
using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
const int B = (int)1e7 + 42;
const int inf = B + 42;
int n, q;
int dp[B + 42], cnt[inf + 42];
int a[MAXN];
void read()
{
	cin >> n >> q;
	for(int i = 0; i < n; i++)
		cin >> a[i];
}
vector<int> li[B + 42];
priority_queue<int, vector<int>, greater<int> > Q;
int last[MAXN];
void fix()
{
	while(!Q.empty() && cnt[Q.top()])
	{
		cnt[Q.top()]--;
		Q.pop();
	}
}
void solve()
{
	for(int i = 0; i < a[n - 1]; i++) dp[i] = 1;
	for(int i = a[n - 1]; i <= B; i++) dp[i] = inf;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j <= B; j += a[i])
			if(dp[j] != 1)
				li[j].pb(i);
	}
	for(int i = 0; i < n - 1; i++)
		Q.push(1), last[i] = 1;
	for(int d = a[n - 1]; d <= B; d++)
	{
		for(int i: li[d]) cnt[last[i]]++;
		fix();	
		if(!Q.empty()) 
			chkmin(dp[d], Q.top() + 1);
	
		for(int i: li[d])
		{
			Q.push(dp[d]);
			last[i] = dp[d];
		}
	}
	while(q--)
	{
		int x;
		cin >> x;
		if(dp[x] == inf) cout << "oo" << endl;
		else cout << dp[x] << endl;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	read();
	solve();
	return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |