/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 5ms 580.0 KiB
#3 Time Exceeded ≥1581ms ≥1.105 MiB
#4 Time Exceeded ≥1581ms ≥1.117 MiB

Code

#include <bits/stdc++.h>
using namespace std;

// Function to perform prime factorization of a number
unordered_map<int, int> prime_factorization(int x) {
    unordered_map<int, int> factors;
    for (int i = 2; i * i <= x; i++) {
        while (x % i == 0) {
            factors[i]++;
            x /= i;
        }
    }
    if (x > 1) {
        factors[x]++;
    }
    return factors;
}

// Function to build cumulative factor counts for the array
vector<unordered_map<int, int>> build_factor_counts(const vector<int>& arr) {
    int n = arr.size();
    vector<unordered_map<int, int>> factor_counts(n);
    
    for (int i = 0; i < n; i++) {
        unordered_map<int, int> factors = prime_factorization(arr[i]);
        // Add current element's factors to cumulative counts
        for (const auto& [p, count] : factors) {
            factor_counts[i][p] += count;
            if (i > 0) {
                // Carry forward previous counts
                factor_counts[i][p] += factor_counts[i - 1][p];
            }
        }
        // Carry forward counts of primes not present in the current element
        if (i > 0) {
            for (const auto& [prime, prev_count] : factor_counts[i - 1]) {
                if (factors.find(prime) == factors.end()) {
                    factor_counts[i][prime] = prev_count;
                }
            }
        }
    }
    return factor_counts;
}

// Function to check if a subarray product is divisible by X
vector<bool> can_subarray_product_be_divisible(const vector<int>& arr, int x, const vector<pair<int, int>>& queries) {
    auto factor_needed = prime_factorization(x);
    auto factor_counts = build_factor_counts(arr);
    vector<bool> results;

    for (const auto& [L, R] : queries) {
        int left = L - 1; // Convert to 0-based index
        int right = R - 1;

        unordered_map<int, int> current_count;

        // Calculate factor counts for the subarray arr[left...right]
        if (left > 0) {
            for (const auto& [p, count] : factor_counts[right]) {
                current_count[p] = count - factor_counts[left - 1][p];
            }
        } else {
            current_count = factor_counts[right];
        }

        // Check if the current_count meets the needed factor counts
        bool divisible = true;
        for (const auto& [p, needed] : factor_needed) {
            if (current_count[p] < needed) {
                divisible = false;
                break;
            }
        }

        results.push_back(divisible);
    }
    return results;
}

int main() {
    int T;
    cin >> T; // Read the number of test cases
    while (T--) {
        int N, X;
        cin >> N >> X; // Read the size of array and the number X
        vector<int> arr(N);
        
        // Read the array elements
        for (int i = 0; i < N; i++) {
            cin >> arr[i];
        }
        
        int Q;
        cin >> Q; // Read the number of queries
        vector<pair<int, int>> queries(Q);
        
        // Read the queries
        for (int i = 0; i < Q; i++) {
            cin >> queries[i].first >> queries[i].second;
        }
        
        // Check for divisibility of subarray products and print results
        auto results = can_subarray_product_be_divisible(arr, X, queries);
        for (bool result : results) {
            cout << (result ? "Yes" : "No") << endl;
        }
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1128 Roy and Product
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-29 20:05:16
Judged At
2024-12-17 11:26:12
Judged By
Score
3
Total Time
≥1581ms
Peak Memory
≥1.117 MiB