Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Chương trình Python lũy thừa modulo (Modular Exponentiation)


Cho ba số x, y, z. Nhiệm vụ tính (x^y) % z.

Ví dụ

Input: x = 2, y = 3, p = 3
Output: 2

Giải thích: 2^3 % 3 = 8 % 3 = 2.

Thuật toán

Bước 1: Nhập ba số.
Bước 2: Dùng pow() tính lũy thừa và % cho modulo.
Bước 3: Hiển thị kết quả.

Ví dụ mã

x = int(input("Enter First Value ::>"))
y = int(input("Enter Second Value ::>"))
z = int(1e9+7)  # pow function use
d = pow(x, y) % z
print("Value Is=", d)

Kết quả

Enter First Value ::> 2
Enter Second Value ::> 3
Value Is= 8