제출 #386034

#제출 시각아이디문제언어결과실행 시간메모리
386034ronnithBrunhilda’s Birthday (BOI13_brunhilda)C++14
100 / 100
293 ms79980 KiB
#include <bits/stdc++.h>

using namespace std;

const int MX = (int)1e7;
int n, q, sieve[MX + 1], dp[MX + 1];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> q;
	for(int i = 0;i < n;i ++) {
		int x; cin >> x;
		for(int j = x;j <= MX;j += x) {
			sieve[j] = x;
		}
		sieve[0] = x;
	}
	int ptr = 0;
	for(int i = 1;i <= MX;i ++) {
		while(ptr < i && (ptr + sieve[ptr] <= i || dp[ptr] == INT_MAX)) ptr ++;
		if(ptr == i) dp[i] = INT_MAX;
		else dp[i] = dp[ptr] + 1;
	}
	while(q --) {
		int x;
		cin >> x;
		if(dp[x] == INT_MAX) {
			cout << "oo\n";
		} else {
			cout << dp[x] << '\n';
		}
	}
	return 0;
}
// OUTPUT in new line.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...