Description

People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment
was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow: 



Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions Ai Bi from all players
including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers. 



You should write a program that calculates the result and is able to find out who won the game. 


Input

The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 <= M <= 45000). The sum will be
divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.

Output

For each assingnement there is the only one line of output. On this line, there is a number, the result of expression

(A1B1+A2B2+ ... +AHBH)mod M.

Sample Input

  1. 3
  2. 16
  3. 4
  4. 2 3
  5. 3 4
  6. 4 5
  7. 5 6
  8. 36123
  9. 1
  10. 2374859 3029382
  11. 17
  12. 1
  13. 3 18132

Sample Output

  1. 2
  2. 13195
  3. 13

  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4.  
  5. int qumulti(int a,int b,int m){
  6. int ans;
  7. if(a==0&&b==0) return 0;
  8. a=a%m;
  9. ans=1;
  10. while(b>0){
  11. if(b&1)///判断是否为奇数,相当于 if(b%2==1)
  12. ans=(ans*a)%m;
  13. a=(a*a)%m;
  14. b=b>>1;///二进制向右移一位,相当于 b=b/2;
  15. }
  16. return ans;
  17. }
  18.  
  19. int main(){
  20. int a,b, m, T , n , ans;
  21. scanf("%d",&T);
  22. while(T--){
  23. ans = 0;
  24. scanf("%d%d",&m,&n);
  25. for(int i=0;i<n;i++){
  26. scanf("%d%d",&a,&b);
  27. ans=(ans+qumulti(a,b,m))%m;
  28. }
  29. printf("%d\n",ans);
  30. }
  31. return 0;
  32. }

Raising Modulo Numbers的更多相关文章

  1. POJ1995 Raising Modulo Numbers

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6373   Accepted: ...

  2. poj 1995 Raising Modulo Numbers【快速幂】

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5477   Accepted: ...

  3. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

  4. Raising Modulo Numbers(POJ 1995 快速幂)

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5934   Accepted: ...

  5. poj 1995 Raising Modulo Numbers 题解

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6347   Accepted: ...

  6. poj1995 Raising Modulo Numbers【高速幂】

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5500   Accepted: ...

  7. 【POJ - 1995】Raising Modulo Numbers(快速幂)

    -->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1  B1 A2  B2 A3  B3 ......... AH  ...

  8. POJ 1995:Raising Modulo Numbers 快速幂

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5532   Accepted: ...

  9. Day7 - J - Raising Modulo Numbers POJ - 1995

    People are different. Some secretly read magazines full of interesting girls' pictures, others creat ...

随机推荐

  1. ConvertFrom-String 命令研究

    -------先上个例子------- $aaa = @'0.027 0.034 0.834 0.1050.346 0.558 0.018 0.0780.001 0.997 0.001 0.0010. ...

  2. c/c++面试题(3)strcat/strcmp/strlen/strcpy的实现

    1.编写一个函数实现strlen以及strcpy函数. strcpy函数. 后面的字符串拷贝到一个字符数组中,要求拷贝好的字符串在字符数组的首 地址,并且只拷贝到'\0'的位置.原型是 char* m ...

  3. css3 弹框功能样式

    .message-box{ position: fixed; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0, 0, 0, ...

  4. cassandra CQL 常用操作

    1. CQL客户端链接 bin/cqlsh ip username password 2. (1)建立keyspace语句,keyspace类似于 mysql 中的数据库,一个数据库中可以有很多表: ...

  5. ACE - Reactor模式源码剖析及具体实现(大量源码慎入)

    原文出自http://www.cnblogs.com/binchen-china,禁止转载. 在之前的文章中提到过Reactor模式和Preactor模式,现在利用ACE的Reactor来实现一个基于 ...

  6. Struts 2开讲了!!!

    首先我们要知道我们为什么要学习Struts2这门技术? Struts2是java程序员所必需学习的一门技术几乎面试都会提起! Struts英文单词是什么意思,怎么理解? 翻译:支柱,支杆,来源于建筑和 ...

  7. HDU 2177

    http://acm.hdu.edu.cn/showproblem.php?pid=2177 威佐夫博奕,面对奇异局势既bk=ak+k时是必败点,其中bk>=ak,k=bk-ak 别的处理和其他 ...

  8. 12-8下午 php语法

    <?php //var_dump(empty($a)); //判断变量是否为空//var_dump(isset($a)); //判断变量是否定义//$a = 10;//unset($a); // ...

  9. LNMP服务器虚拟主机管理lnmp

    安装 系统需求: 需要2 GB硬盘剩余空间 安装步骤: 1.使用putty或类似的SSH工具登陆:登陆后运行:screen -S lnmp如果提示screen命令不存在可以执行:yum install ...

  10. SQL 金额千分位显示

    第一种:select convert(varchar,cast(_money AS MONEY),1) AS _money -----带小数点的第二种: select reverse(stuff(re ...