[AcWing 90] 64位整数乘法

image

位运算

大数乘法


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 5e5 + 10;

LL a, b, p;

void solve()
{
    cin >> a >> b >> p;
    LL res = 0;
    while (b) {
        if (b & 1)
            res = (res + a) % p;
        b >>= 1;
        a = (a + a) % p;
    }
    cout << res << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}

  1. 类比快速幂,位运算做乘法
posted @ 2022-07-21 18:18  wKingYu  阅读(15)  评论(0编辑  收藏  举报