Submission #493705

#TimeUsernameProblemLanguageResultExecution timeMemory
493705_Monkey_Brunhilda’s Birthday (BOI13_brunhilda)C++17
28.10 / 100
1098 ms1100 KiB
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define el '\n'
#define ld long double
const int maxn=1e5+1;

ll a[maxn],n,m,q,p,oo,ans,k;
ll t;
bool ok;
void check(){
    t=1;
    ok=0;
    for(int i=0;i<m;++i){
        k=t*a[i];
        if(t*a[i]<t || t*a[i]>oo || k/a[i]!=t){
            ok=1;
            return;
        }
        t=t*a[i];
    }
}
void sol(){
    ans=0;
    while(n%a[m-1]!=0){
        ans++;
        int ma=0;
        for(int i=0;i<m;++i) if(n%a[i]>ma) ma=n%a[i];
        n-=ma;
    }
    ans+=(n/a[m-1])*2;
    cout << ans;
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin >> m >> q;
    for(int i=0;i<m;++i) cin >> a[i];
    sort(a+0,a+m);
    oo=1e8;
    check();
    while(q--){
        cin >> n;
        if(ok){
            sol();
        } else {
            if(n>=t){
                cout << "oo";
            } else {
                sol();
            }
        }
        cout << el;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...