蓝桥杯 快速幂

问题描述
  给定A, B, P,求(A^B) mod P。
输入格式
  输入共一行。
  第一行有三个数,N, M, P。
输出格式
  输出共一行,表示所求。
样例输入
2 5 3
样例输出
2
数据规模和约定
  共10组数据
  对100%的数据,A, B为long long范围内的非负整数,P为int内的非负整数。
悲伤的故事增加了。
赤裸裸的模板题,复制粘贴提交,40分。
然后又是取模的细节错了。
40分代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll quick_pow(ll x, ll y, ll c) {
 5     ll ans = 1;
 6     while (y) {
 7         if (y & 1) {
 8             ans = (ans % c * x % c) % c;
 9         }
10         x = (x % c * x % c) % c;
11         y >>= 1;
12     }
13     return ans;
14 }
15 int main() {
16     ll x, y, p;
17     while (cin >> x >> y >> p) {
18         ll ans = quick_pow(x, y, p);
19         cout << ans << endl;    
20     }
21     return 0;
22 }

100分代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll quick_pow(ll x, ll y, ll c) {
 5     ll ans = 1;
 6     while (y) {
 7         if (y & 1) {
 8             ans = (ans % c * x % c) % c;
 9         }
10         x = (x % c * x % c) % c;
11         y >>= 1;
12     }
13     return ans;
14 }
15 int main() {
16     ll x, y, p;
17     while (cin >> x >> y >> p) {
18         ll ans = quick_pow(x % p, y, p);
19         cout << ans << endl;    
20     }
21     return 0;
22 }

找不同系列

猜你喜欢

转载自www.cnblogs.com/fx1998/p/12722077.html