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

3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13

#include<iostream>
#include<cstdio>
using namespace std; int qumulti(int a,int b,int m){
int ans;
if(a==0&&b==0) return 0;
a=a%m;
ans=1;
while(b>0){
if(b&1)///判断是否为奇数,相当于 if(b%2==1)
ans=(ans*a)%m;
a=(a*a)%m;
b=b>>1;///二进制向右移一位,相当于 b=b/2;
}
return ans;
} int main(){
int a,b, m, T , n , ans;
scanf("%d",&T);
while(T--){
ans = 0;
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
ans=(ans+qumulti(a,b,m))%m;
}
printf("%d\n",ans);
}
return 0;
}

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. android快速入门

    1.安装环境(jre  java 运行环境,jdk java 开发工具包) 2.android studio 一.快捷键的使用 1. To open any class in the editor q ...

  2. 如何提高Service的优先级避免被杀死或者杀死后如何再次重启Service?

    2014-01-21 16:45:02 我们知道,当进程长期不活动时,如果系统资源吃紧,会杀死一些Service,或不可见的Activity等所在的进程. 如何避免Service被系统杀死,随便在网上 ...

  3. JavaScript基础--内部类(九)

    js的内部类javascript 中本身提供一些,可以直接使用的类,这种类就是内部类,主要有:ObjectArrayMathBooleanStringRegExpDateNumber 1.内部类分类从 ...

  4. 传智播客JavaWeb day02笔记

    2015年1月21日 今天的主要内容:介绍了几款常用Javaweb服务器,重点介绍了tomcat以及tomcat的安装和怎么样检测安装成功 1.JavaWeb常见服务器 Tomcat(免费但是只支持部 ...

  5. 导入别人的flex项目出现的问题

    1.unable to open 'D:/flex-projects/RoadService/WebContent/WEB-INF/flex/services-config.xml' 这种情况是因为别 ...

  6. springboot系列之-profile

    Spring Boot profile用于分离不同环境的参数配置,通过spring.profile.active参数进行设置. 在Spring Boot中应用程序配置可以使用2种格式:applicat ...

  7. python的复制,深拷贝和浅拷贝的区别

    在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用 一般有三种方法, alist=[1,2,3,[& ...

  8. Windowns的GVIM添加markdown语法支持

    gvim 7.4中其实也是有对markdown的语法文件,但格式支持并不全面,如行业代码``就没有实现. 修改方案: 从github下载plasticboy的markdown语法版本,windowns ...

  9. 全面理解面向对象的 JavaScript (share)

     以下分享自:  http://www.ibm.com/developerworks/cn/web/1304_zengyz_jsoo/   简介: JavaScript 函数式脚本语言特性以及其看似随 ...

  10. sed命令的基本使用

    sed(Stream Editor):流编辑器 一次只读取一行 模式空间 1.sed语法: sed [option] "script" FILE... 2.选项: -n:静默模式, ...