THPTQH_134 - Bình Phước 3

Xem dạng PDF

Gửi bài giải


Điểm: 5,00 (OI)
Giới hạn thời gian: 5.0s
Giới hạn bộ nhớ: 256M
Input: stdin
Output: stdout

Nguồn bài:
huyenquangha
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Go, Java, JavaScript, Kotlin, Pascal, Perl, PHP, PyPy, Python, Ruby, Rust, Scratch, Swift


Bình luận

Please read the guidelines before commenting.



  • 0
    ToiNhoDuongThanhThao  đã bình luận lúc 9, Tháng 6, 2026, 3:25

    include <iostream>

    include <vector>

    using namespace std;

    const int MAXVAL = 10000000; bool isprime[MAX_VAL + 1];

    bool is_palindrome(int n) { int reversed = 0, original = n; while (n > 0) { reversed = reversed * 10 + n % 10; n /= 10; } return original == reversed; }

    int main() { iosbase::syncwith_stdio(false); cin.tie(NULL);

    for (int i = 2; i <= MAX_VAL; i++) {
        is_prime[i] = true;
    }
    for (int i = 2; i * i <= MAX_VAL; i++) {
        if (is_prime[i]) {
            for (int j = i * i; j <= MAX_VAL; j += i) {
                is_prime[j] = false;
            }
        }
    }
    
    vector&lt;long long> beautiful_squares;
    for (int i = 2; i <= MAX_VAL; i++) {
        if (is_prime[i] && is_palindrome(i)) {
            beautiful_squares.push_back((long long)i * i);
        }
    }
    
    long long a, b;
    if (cin >> a >> b) {
        int count = 0;
        for (long long sq : beautiful_squares) {
            if (sq >= a && sq <= b) {
                count++;
            }
        }
        cout << count << "\n";
    }
    
    return 0;
    

    }