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

结合律

((a+b) mod p + c)mod p = (a + (b+c) mod p) mod p

((a*b) mod p * c)mod p = (a * (b*c) mod p) mod p

交换律

(a + b) mod p = (b+a) mod p

(a × b) mod p = (b × a) mod p

分配律

((a +b)mod p × c) mod p = ((a × c) mod p + (b × c) mod p) mod p

(a×b) mod c=(a mod c * b mod c) mod c

(a+b) mod c=(a mod c+ b mod c) mod c

(a-b) mod c=(a mod c- b mod c) mod c

 #include<cstdio>
__int64 f(__int64 a,__int64 b,__int64 m)
{
__int64 sun=;
while(b)
{
if(b % != )
{
sun=sun*a%m;
}
a=a*a%m;
b/=;
}
return sun%m;
}
int main()
{
int t,i;
__int64 n,m,sum,a,b;
scanf("%d",&t);
while(t--)
{
sum=;
scanf("%I64d",&m);
scanf("%I64d",&n);
for(i = ; i < n; i++)
{
scanf("%I64d %I64d",&a,&b);
sum+=f(a,b,m);
// printf("---%d--\n",f(a,b,m));
}
printf("%I64d\n",sum%m);
}
}

最新文章

  1. 如何查看bash shell 帮助信息?
  2. 170105、MySQL 性能优化的最佳 20+ 条经验
  3. Laravel学习笔记(一)安装配置开发环境
  4. .NET正则基础——.NET正则类及方法应用
  5. ti processor sdk linux am335x evm /bin/create-sdcard.sh hacking
  6. Entity Framework CodeFirst------使用CodeFirst方式建立数据库连接(一)
  7. DOMContentLoaded和window.onload
  8. HDU 5613 Baby Ming and Binary image
  9. 算法学习:并行化初体验_JAVA实现并行化归并算法
  10. Zabbix实战-简易教程--正则(Regxp)
  11. 说说你对用SSH框架进行开发的理解
  12. 解决 winform打开网页 和WebBrowser打开链接360误报拦截的问题
  13. 如何使用无线连接来使Android调试手机
  14. CXF框架入门(重点)
  15. delphi GetKeyState
  16. 20170706pptVBA演示文稿批量删除图片
  17. React鼠标事件
  18. 【2016北京集训】Mushroom
  19. Cloudera 安装
  20. Android中的Handler介绍

热门文章

  1. 大神是怎样用函数式JavaScript计算数组平均值的
  2. 51nod 1133 不重叠的线段(贪心)
  3. NSSet转成NSArray 以及NSSortDescriptor的使用
  4. 转 【TTS】AIX平台数据库迁移到Linux--基于RMAN(真实环境)
  5. ZOJ 3605Find the Marble(dp)
  6. [转]探索 Windows Azure Storage
  7. Neither BindingResult nor plain target object for bean name &#39;user&#39; available as request attribute
  8. thinkphp3.2.3连接sqlserver 2008 R2 数据库
  9. Java开发笔记(九十三)深入理解字节缓存
  10. 自学 iOS - 三十天三十个 Swift 项目 第二天