/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 788.0 KiB
#2 Accepted 3ms 836.0 KiB
#3 Accepted 94ms 1.176 MiB
#4 Accepted 91ms 1.242 MiB
#5 Accepted 113ms 1.441 MiB
#6 Accepted 120ms 1.375 MiB
#7 Accepted 197ms 3.605 MiB
#8 Accepted 274ms 4.172 MiB
#9 Accepted 202ms 3.582 MiB
#10 Accepted 389ms 19.809 MiB
#11 Accepted 143ms 7.629 MiB
#12 Accepted 257ms 13.711 MiB
#13 Accepted 198ms 4.625 MiB
#14 Accepted 253ms 4.199 MiB
#15 Accepted 112ms 1.203 MiB
#16 Accepted 113ms 1.156 MiB
#17 Accepted 54ms 1.234 MiB
#18 Accepted 114ms 1.191 MiB
#19 Accepted 137ms 1.238 MiB
#20 Accepted 256ms 4.211 MiB
#21 Accepted 480ms 32.152 MiB
#22 Accepted 449ms 32.016 MiB
#23 Accepted 192ms 7.5 MiB
#24 Accepted 1196ms 56.547 MiB
#25 Accepted 167ms 7.574 MiB
#26 Accepted 58ms 1.047 MiB
#27 Accepted 301ms 7.91 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define endl '\n'
#define Endl '\n'

using namespace std;

const int N = 2e5 + 5;
int tc, n, x, a[N];
bool isPrime[N];
vector<int> primes;
map<int, int> xFact;
map<pair<int, int>, int> adj;

void sieve() {
    memset(isPrime, 1, sizeof isPrime);
    isPrime[0] = isPrime[1] = 0;
    for (int i = 2; i * i < N; i++) {
        if (isPrime[i]) {
            for (int j = i * i; j < N; j += i) {
                isPrime[j] = 0;
            }
        }
    }
    for (int i = 2; i < N; i++) {
        if (isPrime[i]) {
            primes.push_back(i);
        }
    }
}

void factorizeOriginalX(int x) {
    for (int i = 0; i < primes.size() && primes[i] * primes[i] <= x; i++) {
        while (x % primes[i] == 0) {
            xFact[primes[i]]++;
            x /= primes[i];
        }
    }
    if (x > 1) {
        xFact[x]++;
    }
}

map<int, int> factorize(int x) {
    map<int, int> fact;
    for (auto i : xFact) {
        if (x < i.first) {
            break;
        }
        while (x > 1 && x % i.first == 0) {
            fact[i.first]++;
            x /= i.first;
        }
    }
    return fact;
}

void buildAdjascentList() {
    map<int, int> fact;
    set<int> st, seen;
    for (int i = 0; i < n; i++) {
        fact.clear();
        seen.clear();
        fact = factorize(a[i]);
        for (auto p : fact) {
            int previousVal = 0;
            if (i) {
                previousVal = adj[{i - 1, p.first}];
            }
            adj[{i, p.first}] = previousVal + p.second;
            seen.insert(p.first);
        }
        // fill the gaps
        for (auto p : xFact) {
            if (!seen.count(p.first)) {
                adj[{i, p.first}] = (i ? adj[{i - 1, p.first}] : 0);
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); // cout.tie(0);
    sieve();
    cin >> tc;
    while (tc--) {
        cin >> n >> x;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        xFact.clear();
        adj.clear();
        factorizeOriginalX(x);

        buildAdjascentList();

        int q;
        cin >> q;
        while (q--) {
            int l, r;
            cin >> l >> r;
            l--, r--;
            bool ok = 1;
            for (auto p : xFact) {
                int cnt = adj[{r, p.first}];
                if (l) {
                    cnt -= adj[{l - 1, p.first}];
                }
                if (cnt < p.second) {
                    ok = 0;
                    break;
                }
            }
            cout << (ok ? "Yes" : "No") << endl;
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1128 Roy and Product
Contest
Lockout contest round-1 ( Mazharul Islam vs Thakur Emon)
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-04 17:44:04
Judged At
2024-11-11 02:33:33
Judged By
Score
100
Total Time
1196ms
Peak Memory
56.547 MiB