THPTTD_108 - Đếm các cặp số

View as PDF

Submit solution

Points: 10.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: countpairs.inp
Output: countpairs.out

Author:
Problem type
Allowed languages
C, C#, C++, Go, Java, JavaScript, Kotlin, Pascal, Perl, PHP, Python, Ruby, Rust, Scratch, Swift

In case the statement didn't load correctly, you can download the statement here: Statement


Comments

Please read the guidelines before commenting.



  • 3
    lephuochauhungvuong  commented on March 11, 2025, 7:52 a.m.
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    long long countPairs(long long K) {
        long long count = 0;
        long long a = 1, b = K - 1;
        while (a < b) {
            count += (b - a); // Mỗi lần ta tăng a lên, số lượng cặp (a, b) thỏa mãn điều kiện sẽ tăng thêm (b - a)
            a++;
            b = min(b, K - a);
        }
        return count;
    }
    
    int main() {
        ifstream cin("countpairs.inp");
        ofstream cout("countpairs.out");
        int T;
        cin >> T;
        while (T--) {
            long long K;
            cin >> K;
            cout << countPairs(K) << endl;
        }
        return 0;
    }
    

  • 0
    longglolcom  commented on Feb. 10, 2025, 2:32 a.m.

    bài củ lồn này bj nứng à