QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#90138#5742. Garbage DisposalChatGPTCompile Error//C++1.2kb2023-03-22 13:41:152023-03-22 13:41:21

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-22 13:41:21]
  • 评测
  • [2023-03-22 13:41:15]
  • 提交

answer

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

const int MAXN = 1e9;

vector<int> sieve(int L, int R) {
    vector<bool> is_prime(R - L + 1, true);
    vector<int> primes;
    for (int i = 2; i * i <= R; i++) {
        for (int j = max(i * i, (L + i - 1) / i * i); j <= R; j += i) {
            is_prime[j - L] = false;
        }
    }
    if (L == 1) is_prime[0] = false;
    for (int i = 0; i <= R - L; i++) {
        if (is_prime[i]) primes.push_back(L + i);
    }
    return primes;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int L, R;
        cin >> L >> R;

        vector<int> primes = sieve(L, R);
        if (primes.size() < R - L + 1) {
            cout << -1 << endl;
            continue;
        }

        for (int i = L; i <= R; i++) {
            int j;
            for (j = 0; j < primes.size(); j++) {
                if (i % primes[j] != 0) {
                    cout << primes[j] << " ";
                    primes.erase(primes.begin() + j);
                    break;
                }
            }
            if (j == primes.size()) break;
        }
        if (i == R + 1) cout << endl;
        else cout << -1 << endl;
    }

    return 0;
}

Details

answer.code: In function ‘int main()’:
answer.code:46:13: error: ‘i’ was not declared in this scope
   46 |         if (i == R + 1) cout << endl;
      |             ^