题解 | #快速乘#
快速乘
https://www.nowcoder.com/practice/043c66e95fe548d0b8e56c1830330f93?tpId=308&tqId=2403189&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D308
学到了,两个相乘等于某个数相加,加的过程中要相模
#include <iostream>
int main(int argc, char *argv[]) {
long long count, a, b, q;
std::cin >> count;
// a*b等于有b个a相加
while (--count >= 0) {
std::cin >> a >> b >> q;
long long res = 0;
// (a*b)%q = ((a%q)*(b%q))%q = (a%q + a%q +...+ a%q)%q
while (b--) {
res += a % q;
}
std::cout << res % q << std::endl;
}
return 0;
}