64位整数乘法

题目描述

求 a 乘 b 对 p 取模的值,其中 1≤a,b,p≤10^18。

输入

第一行a,第二行b,第三行p。

输出

一个整数,表示a*b mod p的值。

样例输入

2

3

9

样例输出

6

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
LL a,b,p;
void init(){
    cin>>a>>b>>p;
}
LL qmul(LL a,LL b,LL p){
    LL tmp=(long double)a*b/p,ans=a*b-tmp*p;
    return (ans+p)%p;
}
void solve(){
    cout<<qmul(a,b,p)<<endl;
}
int main() {
    init();
    solve();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Rhythm-/p/9318525.html