/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 26ms 792.0 KiB
#3 Accepted 85ms 540.0 KiB
#4 Accepted 241ms 540.0 KiB
#5 Accepted 343ms 540.0 KiB
#6 Accepted 544ms 540.0 KiB
#7 Accepted 777ms 544.0 KiB
#8 Accepted 4ms 540.0 KiB
#9 Accepted 15ms 544.0 KiB
#10 Accepted 26ms 544.0 KiB
#11 Accepted 39ms 540.0 KiB
#12 Accepted 803ms 564.0 KiB
#13 Accepted 2ms 328.0 KiB
#14 Accepted 1ms 540.0 KiB
#15 Accepted 2ms 332.0 KiB
#16 Accepted 1ms 540.0 KiB
#17 Accepted 2ms 336.0 KiB
#18 Accepted 2ms 332.0 KiB
#19 Accepted 1ms 540.0 KiB
#20 Accepted 2ms 332.0 KiB
#21 Accepted 1ms 328.0 KiB

Code

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

// Function to check if a number is prime
bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int x;
    cin >> x;

    // Precompute prime numbers up to 3x using sieve
    vector<bool> prime(3 * x + 1, true);
    prime[0] = prime[1] = false; // 0 and 1 are not prime numbers

    for (int i = 2; i * i <= 3 * x; i++) {
        if (prime[i]) {
            for (int j = i * i; j <= 3 * x; j += i) {
                prime[j] = false;
            }
        }
    }

    int count = 0;

    // Iterate through all triplets (a, b, c)
    for (int a = 1; a <= x; a++) {
        for (int b = 1; b <= x; b++) {
            for (int c = 1; c <= x; c++) {
                int sum = a + b + c;
                if (sum <= x && prime[sum]) {
                    count++;
                }
            }
        }
    }

    cout << count << endl;

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1172 Counting Triplets
Language
C++17 (G++ 13.2.0)
Submit At
2025-03-04 06:16:40
Judged At
2025-03-04 06:16:40
Judged By
Score
100
Total Time
803ms
Peak Memory
792.0 KiB