题解 | #快速乘#

快速乘

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;
}
全部评论

相关推荐

Twilight_m...:还是不够贴近现实,中关村那块60平房子200万怎么可能拿的下来,交个首付还差不多
点赞 评论 收藏
分享
07-16 18:03
门头沟学院 Java
点赞 评论 收藏
分享
08-07 11:33
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务