/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 2ms 580.0 KiB
#3 Accepted 55ms 832.0 KiB
#4 Accepted 54ms 680.0 KiB
#5 Accepted 55ms 1.188 MiB
#6 Accepted 60ms 1.152 MiB
#7 Accepted 83ms 3.73 MiB
#8 Accepted 104ms 4.414 MiB
#9 Accepted 80ms 3.672 MiB
#10 Accepted 102ms 24.52 MiB
#11 Accepted 67ms 12.375 MiB
#12 Accepted 80ms 18.207 MiB
#13 Accepted 85ms 4.797 MiB
#14 Accepted 96ms 4.191 MiB
#15 Accepted 117ms 892.0 KiB
#16 Accepted 114ms 888.0 KiB
#17 Accepted 40ms 900.0 KiB
#18 Accepted 118ms 744.0 KiB
#19 Accepted 73ms 1.25 MiB
#20 Accepted 89ms 4.34 MiB
#21 Accepted 132ms 36.684 MiB
#22 Accepted 133ms 36.75 MiB
#23 Accepted 124ms 12.148 MiB
#24 Accepted 217ms 61.305 MiB
#25 Accepted 67ms 12.312 MiB
#26 Accepted 366ms 872.0 KiB
#27 Accepted 131ms 12.648 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

void solve() {
    int n, x;
    cin >> n >> x;
    
    // Get prime factorization of x
    map<int, int> x_primes;
    int temp_x = x;
    for(int i = 2; i * i <= temp_x; i++) {
        while(temp_x % i == 0) {
            x_primes[i]++;
            temp_x /= i;
        }
    }
    if(temp_x > 1) {
        x_primes[temp_x]++;
    }
    
    vector<int> arr(n);
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    // For each position, store count of prime factors
    vector<map<int, int>> prefix(n);
    for(int i = 0; i < n; i++) {
        int curr = arr[i];
        // Only factorize primes that are in x
        for(auto &[prime, count] : x_primes) {
            int cnt = 0;
            while(curr % prime == 0) {
                cnt++;
                curr /= prime;
            }
            prefix[i][prime] = cnt;
        }
        
        // Add previous counts
        if(i > 0) {
            for(auto &[prime, count] : x_primes) {
                prefix[i][prime] += prefix[i-1][prime];
            }
        }
    }
    
    int q;
    cin >> q;
    while(q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;  // Convert to 0-based indexing
        
        bool possible = true;
        // Check if range has enough of each prime factor
        for(auto &[prime, needed] : x_primes) {
            int has = prefix[r][prime];
            if(l > 0) {
                has -= prefix[l-1][prime];
            }
            if(has < needed) {
                possible = false;
                break;
            }
        }
        
        cout << (possible ? "Yes" : "No") << endl;
    }
    cout << endl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int t = 1;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1128 Roy and Product
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-07 20:44:51
Judged At
2024-11-11 02:22:54
Judged By
Score
100
Total Time
366ms
Peak Memory
61.305 MiB