This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define N 100050
using namespace std;
int n, q, p[N], dp[10000005], prox[10000005];
int solve(int x)
{
if(!x) return 0;
if(dp[x] != -1) return dp[x];
int ans = -2000000000, opt = 0;
for(int i = n; i >= (q == 1 ? 1 : max(1, n - 500)); i--)
{
if(x % p[i] == 0) continue;
if(x % p[i] > ans)
{
ans = x % p[i];
opt = i;
}
}
return dp[x] = opt ? solve(x - ans) + 1: 2000000000;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>q;
for(int i = 1; i <= n; i++) cin>>p[i];
sort(p + 1, p + n + 1);
memset(dp, -1, sizeof dp);
for(int i = 1, x; i <= q; i++)
{
cin>>x;
int s = solve(x);
if(s >= 2000000000) cout<<"oo\n";
else cout<<s<<"\n";
//print(x);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |