/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 3ms 324.0 KiB
#3 Accepted 323ms 836.0 KiB
#4 Accepted 309ms 876.0 KiB
#5 Accepted 320ms 892.0 KiB
#6 Accepted 354ms 860.0 KiB
#7 Accepted 370ms 1.141 MiB
#8 Accepted 384ms 1.012 MiB
#9 Accepted 386ms 996.0 KiB
#10 Accepted 394ms 2.289 MiB
#11 Accepted 390ms 1.684 MiB
#12 Accepted 400ms 1.961 MiB
#13 Accepted 403ms 1.086 MiB
#14 Accepted 331ms 952.0 KiB
#15 Accepted 413ms 824.0 KiB
#16 Accepted 406ms 828.0 KiB
#17 Accepted 374ms 804.0 KiB
#18 Time Exceeded ≥1582ms ≥720.0 KiB
#19 Time Exceeded ≥1553ms ≥744.0 KiB

Code

#include <iostream>
#include <vector>
#include <unordered_map>
#include <cmath>
using namespace std;

// Function to factorize a number and return a map of its prime factors with their powers
unordered_map<int, int> getPrimeFactors(int num) {
    unordered_map<int, int> prime_factors;
    for (int i = 2; i <= sqrt(num); i++) {
        while (num % i == 0) {
            prime_factors[i]++;
            num /= i;
        }
    }
    if (num > 1) {
        prime_factors[num]++;
    }
    return prime_factors;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int N, X;
        cin >> N >> X;
        
        vector<int> A(N);
        for (int i = 0; i < N; i++) {
            cin >> A[i];
        }
        
        unordered_map<int, int> xFactors = getPrimeFactors(X);
        
        // Prefix factor count map for each factor in X
        unordered_map<int, vector<int>> prefixFactorCount;
        for (auto &[prime, _] : xFactors) {
            prefixFactorCount[prime] = vector<int>(N + 1, 0);
        }
        
        // Fill prefix factor counts
        for (int i = 1; i <= N; i++) {
            unordered_map<int, int> currentFactors = getPrimeFactors(A[i - 1]);
            for (auto &[prime, _] : xFactors) {
                prefixFactorCount[prime][i] = prefixFactorCount[prime][i - 1] + currentFactors[prime];
            }
        }
        
        int Q;
        cin >> Q;
        while (Q--) {
            int L, R;
            cin >> L >> R;
            L--; R--; // Adjust for 0-based indexing
            
            bool divisible = true;
            for (auto &[prime, requiredCount] : xFactors) {
                int factorCountInRange = prefixFactorCount[prime][R + 1] - prefixFactorCount[prime][L];
                if (factorCountInRange < requiredCount) {
                    divisible = false;
                    break;
                }
            }
            cout << (divisible ? "Yes" : "No") << endl;
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1128 Roy and Product
Contest
Brain Booster #7
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 15:59:53
Judged At
2024-11-11 02:28:57
Judged By
Score
70
Total Time
≥1582ms
Peak Memory
≥2.289 MiB