THPTTD_107 - Tìm ước

Xem dạng PDF

Gửi bài giải

Điểm: 10,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M
Input: powerofprime.inp
Output: powerofprime.out

Tác giả:
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Go, Java, JavaScript, Kotlin, Pascal, Perl, PHP, Python, Ruby, Rust, Scratch, Swift

Trong trường hợp đề bài hiển thị không chính xác, bạn có thể tải đề bài tại đây: Đề bài


Bình luận

Hãy đọc nội quy trước khi bình luận.



  • 4
    lephuochauhungvuong  đã bình luận lúc 11, Tháng 3, 2025, 7:52
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    // Hàm tính số mũ lớn nhất của p là ước của N!
    int powerOfPrimeInFactorial(int N, int p) {
        int power = 0;
        int current_power = p;
        while (current_power <= N) {
            power += N / current_power;
            current_power *= p;
        }
        return power;
    }
    
    int main() {
        // Mở file input và output
        ifstream infile("powerofprime.inp");
        ofstream outfile("powerofprime.out");
    
        if (!infile || !outfile) {
            cerr << "Không thể mở file input hoặc output!" << endl;
            return 1;
        }
    
        int T; // Số bộ dữ liệu
        infile >> T;
    
        while (T--) {
            int N, p;
            infile >> N >> p;
    
            // Tính và ghi kết quả ra file
            outfile << powerOfPrimeInFactorial(N, p) << endl;
        }
    
        // Đóng file
        infile.close();
        outfile.close();
    
        return 0;
    }
    

  • 0
    leedat313  đã bình luận lúc 6, Tháng 8, 2024, 3:58

    Test 18 là gì vậy ạ?