15 solutions

  • 1
    @ 2021-11-30 12:48:17

    代码自认为十分好懂

    #include<cstdio>
    using namespace std;
    #define ll long long
    ll cal(ll a, ll b, ll p)
    {
        ll ans = 1 % p;
        for (; b; b >>= 1)//个人码风原因,也可使用while循环额外加b >>= 1
        {
            if (b & 1)
                ans = ans * a % p;//及时取模防止爆掉
            a = a * a % p;//及时取模防止爆掉
        }
        return ans;
    }
    int main()
    {
    	ll a, b, p;
    	scanf("%lld %lld %lld", &a, &b, &p);
    	printf("%lld", cal(a, b, p));
        return 0;
    }
    

    Information

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