/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 532.0 KiB
#2 Accepted 7ms 532.0 KiB
#3 Accepted 591ms 1.102 MiB
#4 Accepted 566ms 984.0 KiB
#5 Time Exceeded ≥1601ms ≥14.434 MiB
#6 Time Exceeded ≥1600ms ≥18.754 MiB

Code

#include <iostream>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

// Function to perform prime factorization
map<int, int> primeFactorization(int num) {
    map<int, int> factors;
    while (num % 2 == 0) {
        factors[2]++;
        num /= 2;
    }
    for (int i = 3; i <= sqrt(num); i += 2) {
        while (num % i == 0) {
            factors[i]++;
            num /= i;
        }
    }
    if (num > 2) {
        factors[num]++;
    }
    return factors;
}

// Main function to solve the problem
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];
        }

        // Prime factorization of X
        map<int, int> xFactors = primeFactorization(X);

        // Create prefix factorization maps for array A
        vector<map<int, int>> prefixFactors(N + 1);

        for (int i = 0; i < N; ++i) {
            map<int, int> currentFactors = primeFactorization(A[i]);
            prefixFactors[i + 1] = prefixFactors[i]; // Start with the previous prefix
            for (const auto& factor : currentFactors) {
                prefixFactors[i + 1][factor.first] += factor.second;
            }
        }

        int Q;
        cin >> Q;
        while (Q--) {
            int L, R;
            cin >> L >> R;
            L--; R--; // Convert to 0-based index

            bool divisible = true;
            for (const auto& factor : xFactors) {
                int prime = factor.first;
                int requiredPower = factor.second;

                // Calculate the count of this prime in the subarray product
                int powerInSubarray = prefixFactors[R + 1][prime] - prefixFactors[L][prime];
                if (powerInSubarray < requiredPower) {
                    divisible = false;
                    break;
                }
            }

            if (divisible) {
                cout << "Yes" << endl;
            } else {
                cout << "No" << endl;
            }
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1128 Roy and Product
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 17:05:27
Judged At
2024-11-05 17:05:27
Judged By
Score
7
Total Time
≥1601ms
Peak Memory
≥18.754 MiB