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);
}
}

POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M的更多相关文章

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

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

  2. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...

  3. poj 1995 快速幂

    题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11  11=2^0+2^1+2^3    => 3^1*3 ...

  4. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  5. HDU4869:Turn the pokers(快速幂求逆元+组合数)

    题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...

  6. 小白月赛13 小A的路径 (矩阵快速幂求距离为k的路径数)

    链接:https://ac.nowcoder.com/acm/contest/549/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  7. hdu 2065 "红色病毒"问题(快速幂求模)

    n=1  --> ans = 2 = 1*2 = 2^0(2^0+1) n=2  -->  ans = 6 = 2*3 = 2^1(2^1+1) n=3  -->  ans = 20 ...

  8. codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质

    E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...

  9. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

随机推荐

  1. SQL Server OPENQUERY使用

    以下以创建好的ORAC链接服务器为例: A. 执行 SELECT 传递查询 SELECT * FROM OPENQUERY(ORAC, 'SELECT ID,NAME FROM SCOTT.RYB') ...

  2. git修改push的注释信息

    修改还未push的注释: git commit --amend 修改后保存退出. 刚刚push到远端还没有人其他人下载或改动的: git commit --amend进入修改页面修改注释信息,修改后: ...

  3. Spring 中 ApplicationContext 和 BeanFactory 的区别

    //从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...

  4. flask框架模板系统

    flask模板引擎 flask默认使用了Jinja2模板引擎,我们在使用模板的时候,需要在同级目录文件夹下 创建一个templates的文件夹,然后这个文件夹内放置我们想要的模板实例即可: 在正常普通 ...

  5. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

  6. 对dynamic和lambda的学习

    var, object, dynamic的区别以及使用 dynamic(2) – ExpandoObject的使用 .NET中的Lambda表达式与匿名方法

  7. ORA-00445: Background Process "xxxx" Did Not Start After 120 Seconds

    Recent linux kernels have a feature called Address Space Layout Randomization (ASLR).ASLR  is a feat ...

  8. spark yarn cluster模式下任务提交和计算流程分析

    spark可以运行在standalone,yarn,mesos等多种模式下,当前我们用的最普遍的是yarn模式,在yarn模式下又分为client和cluster.本文接下来将分析yarn clust ...

  9. [转]Resolve Team Foundation Version Control conflicts

    本文转自:http://msdn.microsoft.com/en-us/library/ms181432.aspx An advantage of using Team Foundation ver ...

  10. 好用的SqlParamterList

    public class SqlParameterList : List<SqlParameter> { #region Properties /// <summary> // ...