Cho số N. Nhiệm vụ: tìm 5 chữ số cuối của N^5.
Ví dụ:
Đầu vào: N = 25211
Đầu ra: (ví dụ output thiếu trong nguồn)
Phương pháp giải
Chỉ cần 5 chữ số cuối, nên sau mỗi lần nhân lấy modulo 100000. Lặp 5 lần.
Chương trình minh họa
Ví dụ
#include <iostream>
using namespace std;
int lastFiveDigits(int n) {
int result = 1;
for (int i = 0; i < 5; i++) {
result *= n;
result %= 100000;
}
cout << "The last five digits of " << n << " raised to the power 5 are " << result;
}
int main() {
int n = 12345;
lastFiveDigits(n);
return 0;
}
Kết quả
The last five digits of 12345 raised to the power 5 are 65625