제출 #493718

#제출 시각아이디문제언어결과실행 시간메모리
493718_Monkey_Brunhilda’s Birthday (BOI13_brunhilda)C++17
77.46 / 100
237 ms262148 KiB
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long
#define el '\n'
#define ld long double
const int maxn=1e5+1,nn=1e7+1;
void amax(int &x,int y){if(y>x)x=y;}
int a[maxn],n,m,q,p,oo,ans,k,f[nn],ut[nn];
int 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];
    }
}
int sol(int z){
    if(f[z]>=0) return f[z];
    f[z]=sol(z-ut[z])+1;
    return f[z];
}
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);
  	for(int i=0;i<m;++i)
        for(int j=a[i]-1;j<nn;j+=a[i]) ut[j]=a[i]-1;
    for(int i=nn-2;i>0;--i) amax(ut[i],ut[i+1]-1);
    memset(f,-1,sizeof f);
    f[0]=0;
    oo=1e7;
    check();
    while(q--){
        cin >> n;
        if(ok){
            cout << sol(n);
        } else {
            if(n>=t){
                cout << "oo";
            } else {
                cout << sol(n);
            }
        }
        cout << el;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...