Submission #1358785

#TimeUsernameProblemLanguageResultExecution timeMemory
1358785charlestiBrunhilda’s Birthday (BOI13_brunhilda)C++20
100 / 100
124 ms79688 KiB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
using namespace std;

int dp[10000069];
int P[10000069]; 
int a[100069];
int qu[100069];
int mx = 0;

signed main(){

    cin.tie(0); ios::sync_with_stdio(0);
    int m, q; cin >> m >> q;
    for(int i=1; i<=m; i++){
        cin >> a[i];
    }
    
    for(int i=1; i<=q; i++){
        cin >> qu[i];
        mx = max(mx, qu[i]);
    }
    
    for(int i=1; i<=m; i++){
        for(int j=a[i]; j<=mx; j+=a[i]){
            P[j] = a[i];
        }
    }
    
    int ptr = 0;
    dp[0] = 0;
    P[0] = a[m];
    
    for(int i=1; i<=mx; i++){
        
        while(ptr + P[ptr] <= i){
            ptr++;
            // cout << ptr << "Push\n";
        }
        
        if(ptr == i){
            dp[i] = -1;
            // if(i==4) cout << "S\n";
        } else {
            dp[i] = (dp[ptr] == -1) ? -1 : dp[ptr] + 1;
            // cout << i << ' ' << dp[i] << "PU\n";
        }
        // cout << i << ' ' << ptr << "TOP\n";
    }

    // for(int i=1; i<=10; i++) cout << i << ' ' << dp[i] << "A\n";
    
    for(int i=1; i<=q; i++){
        if(dp[qu[i]] == -1) cout << "oo\n";
        else cout << dp[qu[i]] << '\n';
    }

    return 0;
}

/*
2 2
2 3
5
6

*/
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...