제출 #1352439

#제출 시각아이디문제언어결과실행 시간메모리
1352439Trisanu_DasHack (APIO25_hack)C++20
컴파일 에러
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;

long long collisions(vector<long long> x);

// Precomputed product of the first 15 primes (≤ 10^18)
const vector<int> primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
const long long PRIMORIAL_15 = 614889782588491410LL; // 2*3*...*47

int hack() {
    const int B = 100000;
    vector<long long> S(B);
    for (int i = 0; i < B; ++i) S[i] = i + 1;
    long long c = collisions(S);

    if (c > 0) {
        // n ≤ B-1. Binary search on n using the already queried set.
        int lo = 2, hi = B - 1;
        while (lo < hi) {
            int mid = (lo + hi) / 2;
            // Simulate collisions for n = mid with the same set S
            long long sim = 0;
            vector<int> bucket(mid, 0);
            for (long long x : S) {
                int b = x % mid;
                sim += bucket[b];
                bucket[b]++;
            }
            if (sim > 0) hi = mid;
            else         lo = mid + 1;
        }
        // Verify with the 3‑element divisibility test (all numbers ≥ 1)
        if (collisions({(long long)lo, 2LL*lo, 3LL*lo}) == 3) return lo;
        // (fallback – should never be needed)
        for (int d = max(2, lo-5); d <= min(B-1, lo+5); ++d)
            if (collisions({(long long)d, 2LL*d, 3LL*d}) == 3) return d;
    }

    if (collisions({PRIMORIAL_15, 2*PRIMORIAL_15, 3*PRIMORIAL_15}) == 3) {
        vector<long long> divisors;
        function<void(int, long long)> gen = [&](int idx, long long cur) {
            if (idx == (int)primes.size()) {
                if (cur >= 2 && cur <= 1000000000) divisors.push_back(cur);
                return;
            }
            gen(idx+1, cur);
            gen(idx+1, cur * primes[idx]);
        };
        gen(0, 1);
        sort(divisors.begin(), divisors.end());
        for (long long d : divisors)
            if (collisions({d, 2*d, 3*d}) == 3) return d;
    }

    vector<long long> big_primes;
    for (int p = 53; p <= 1000000000; ) {
        // build a product of consecutive large primes that stays ≤ 1e18
        long long prod = 1;
        int start = p;
        while (p <= 1000000000 && prod <= 1000000000000000000LL / p) {
            prod *= p;
            p = next_prime(p);
        }
        big_primes.push_back(prod);
        if (p <= 1000000000 && prod == 1) p = next_prime(p);
    }

    // Binary search for the block whose product is a multiple of n
    int L = 0, R = (int)big_primes.size() - 1;
    while (L < R) {
        int mid = (L + R) / 2;
        long long prod = 1;
        for (int i = L; i <= mid; ++i) {
            if (prod > 1000000000000000000LL / big_primes[i]) break;
            prod *= big_primes[i];
        }
        if (collisions({prod, 2*prod, 3*prod}) == 3)
            R = mid;
        else
            L = mid + 1;
    }
    for (long long d = 100001; ; ++d)
        if (collisions({d, 2*d, 3*d}) == 3) return d;

    return -1;
}

컴파일 시 표준 에러 (stderr) 메시지

hack.cpp: In function 'int hack()':
hack.cpp:62:17: error: 'next_prime' was not declared in this scope
   62 |             p = next_prime(p);
      |                 ^~~~~~~~~~
hack.cpp:65:47: error: 'next_prime' was not declared in this scope
   65 |         if (p <= 1000000000 && prod == 1) p = next_prime(p);
      |                                               ^~~~~~~~~~