快速乘

早就学了,复习一下。
普通 \(\mathcal {O}(log_2(n))\) 的快速乘(龟速乘):

LL QT(LL x, LL y) {
	LL ans = 0;
	for(; y; y >>= 1) {
		if(y & 1) ans = (ans + x) % n; x = (x + x) % n;
	}
	return ans;
}

\(\mathcal {O}(1)\) 快速乘。

LL mul(LL x, LL y) {
	return (x * y - (LL)((long double)x / n * y) * n + n) % n;
}
posted @ 2021-08-18 19:10  Saintex  阅读(51)  评论(0编辑  收藏  举报