输入分三行输入三个整数a,b,m 输出a的b次幂,ab对m取模
时间: 2024-02-01 09:13:03 浏览: 22
以下是 Python 代码实现:
```python
a = int(input())
b = int(input())
m = int(input())
# 求 a 的 b 次幂,可以使用快速幂算法
def quick_pow(a, b):
res = 1
while b > 0:
if b % 2 == 1:
res *= a
a *= a
b //= 2
return res
# 求 a 的 b 次幂对 m 取模,同样可以使用快速幂算法
def quick_pow_mod(a, b, m):
res = 1
a %= m
while b > 0:
if b % 2 == 1:
res = (res * a) % m
a = (a * a) % m
b //= 2
return res
print(quick_pow(a, b))
print(quick_pow_mod(a, b, m))
```
输入示例:
```
2
10
1000000007
```
输出示例:
```
1024
976371285
```
相关问题
分三行输入三个整数a,b,m 输出a的b次幂,ab对m取模
以下是 Python 代码实现:
```python
a, b, m = map(int, input().split())
# 求 a 的 b 次幂
res = pow(a, b)
# 对 m 取模
res %= m
print(res)
```
其中,`pow(a, b)` 表示求 `a` 的 `b` 次幂。取模运算可以使用 `%` 符号或者 `divmod` 函数实现,例如 `res = res % m` 或者 `(res, _) = divmod(res, m)`。
分三行输入三个整数a,b,m,在两行中分别输出a的b次幂和ab对m取模的结果
代码实现如下:
```
a = int(input())
b = int(input())
m = int(input())
# 计算a的b次幂
res1 = pow(a, b)
print(res1)
# 计算ab对m取模的结果
res2 = pow(a, b, m)
print(res2)
```
注意,需要使用 `pow` 函数来进行幂运算和取模运算。其中,`pow(x, y)` 表示计算 x 的 y 次幂,`pow(x, y, z)` 表示计算 x 的 y 次幂对 z 取模的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)