• 蓝桥杯 快速幂


    问题描述
      给定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 }

    找不同系列

  • 相关阅读:
    flask的简单使用
    Android Studio 使用Lambda表达式
    安卓SDK更新 国内镜像站点记录
    REACT-NATIVE入门(环境搭建及HELLOWORLD)
    深入理解Android 读感(一)
    JNI初入手
    (转)c++ 中的using namespace std是什么意思,什么时候用
    (转)const char to LPCTSTR不能转化问题
    Git常用操作之add操作
    Git日志操作
  • 原文地址:https://www.cnblogs.com/fx1998/p/12722077.html
Copyright © 2020-2023  润新知