24 solutions

  • 2
    @ 2023-8-12 15:12:01

    核心代码一行的快速幂:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll a, b, p;
    ll power (ll a, ll b, ll p) {
    	return b == 0? 1: (b & 1? a: 1) * power (a * a % p, b >> 1, p) % p;
    }
    int main () {
        scanf ("%lld%lld%lld", &a, &b, &p);
        printf ("%lld", power (a, b, p));
        return 0;
    }
    

    压行(164B)!

    #import<iostream>
    typedef long long L;L a,b,p;L P(L a,L b,L p){return b?(b&1?a:1)*P(a*a%p,b/2,p)%p:1;}int main(){std::cin>>a>>b>>p;std::cout<<P(a,b,p);return 0;}
    

    顺便找了一个不递归的压了压(182B):

    #import<iostream>
    typedef long long L;L a,b,p;L P(L a,L b,L p){L S=1;while(b){if(b&1)S=S*a%p;a=a*a%p;b/=2;}return ans;}int main(){std::cin>>a>>b>>p;std::cout<<P(a,b,p);return 0;}
    

    可以发现,前者代码短一点。

    (喂喂我这个大码量人为什么要玩 Code Golf 啊

    Information

    ID
    171
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    2
    Tags
    # Submissions
    871
    Accepted
    292
    Uploaded By