# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
483299 | 2021-10-28T14:46:28 Z | ntabc05101 | Brunhilda’s Birthday (BOI13_brunhilda) | C++14 | 0 ms | 0 KB |
#include<bits/stdc++.h> using namespace std; const int mxN = 10000000; int dp[mxN + 1], b[mxN + 1]; int main() { cin.tie(0)->sync_with_stdio(0); int m, Q; cin >> m >> Q; int prs[m]; for (int i = 0; i < m; i++) { cin >> prs[i]; } iota(b + 1, b + mxN + 1, 1); sort(prs, prs + m); int mn = mxN + 1; for (int i = 0; i < m; i++) { for (j = prs[i]; j <= mxN; j += prs[i]) { b[j - 1] = j - prs[i]; } mn = min(mn, j - prs[i]); } for (int i = mxN; i; i--) { dp[i] = mxN + 1; mn = min(mn, b[i]); b[i] = mn; } for (int i = 1; i <= mxN; i++) { if (i == b[i]) { break; } dp[i] = dp[b[i]] + 1; } while (Q--) { int n; cin >> n; if (dp[n] == mxN + 1) { cout << "oo\n"; } else { cout << dp[n] << "\n"; } } return 0; }