题目链接
http://noi-test.zzstep.com/contest/0x00%E3%80%8C%E5%9F%BA%E6%9C%AC%E7%AE%97%E6%B3%95%E3%80%8D%E4%BE%8B%E9%A2%98/0102%2064%E4%BD%8D%E6%95%B4%E6%95%B0%E4%B9%98%E6%B3%95
分析
直接乘会爆 l o n g l o n g long long longlong,一种方法是类比快速幂,将一个因数按二进制位拆解,但每次操作复杂度是 l o g log log 的;另一种方法是借助 a ∗ b m o d p = a ∗ b − ⌊ a ∗ b p ⌋ ∗ p a * b \ \ mod \ \ p = a * b - \lfloor {a * b \over p} \rfloor * p a∗b mod p=a∗b−⌊pa∗b⌋∗p,可将复杂度降到 O ( 1 ) O(1) O(1)
AC代码
#include <cstdio>
typedef long long ll;
inline ll read() {
ll num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num;
}
inline ll qmul(ll a, ll b, ll p) {
return (a * b - (ll)((long double)a * b / p) * p + p) % p;
}
int main() {
ll a = read(), b = read(), p = read();
printf("%lld", qmul(a, b, p));
return 0;
}