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. Redis 集群解决方案 Codis

    (来源:开源中国社区 http://www.oschina.net/p/codis) Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生 ...

  2. MyJni撒旦

    package com.baidu.jnitest; import android.os.Bundle; import android.app.Activity; import android.vie ...

  3. Population-based metagenomics analysis reveals markers for gut microbiome composition and diversity

    读paper的时候觉得自己就是个24K纯学渣(=.=)一大堆问题等着我去解决...所以在这里写一个Q&A好了,先列问题,逐步填充答案- ××××××××××××××××××我是分割线么么哒×× ...

  4. 通过j-interop访问WMI实例代码

    代码: import java.net.UnknownHostException; import java.util.logging.Level; import org.jinterop.dcom.c ...

  5. iOS 自定义emoji表情键盘

    之前走了很多弯路,包括自己定以emoji表情,自己创建view类去处理图文混排 ,当把这些焦头烂额的东西处理完了才发现 ,其实系统自带键盘是如此的方便,iOS 系统自带的表情在view,textfie ...

  6. [URAL]刷题记录表

    URAL 1001.  A + B 1002.  简单题,开方计算! 1003.

  7. Android 字体相关总结

    1.Android系统默认支持三种字体,分别为:“sans”, “serif”,  “monospace“  系统缺省方式(经试验缺省采用采用sans): 2.在Android中可以引入其他字体 3. ...

  8. c语言二叉树基本操作

    编译器为vs2013 #include "stdafx.h" #include<malloc.h> #include<stdlib.h> #define O ...

  9. c++对象成员的引用---12

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 在程序中经常需要访问对象中的成员.访问对象中的成员可以有3种方法: 通过对象名和成员运算符访问对象中的成 ...

  10. jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation

    jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation 此错误与crsf有关