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. IIS重新注册

    打开程序-运行-cmd:输入一下命令重新注册IISC:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

  2. BIOS 和UEFI的区别

    BIOS先要对CPU初始化,然后跳转到BIOS启动处进行POST自检,此过程如有严重错误,则电脑会用不同的报警声音提醒,接下来采用读中断的方式加载各种硬件,完成硬件初始化后进入操作系统启动过程:而UE ...

  3. poj 3281 Dining (最大网络流)

    题目链接: http://poj.org/problem?id=3281 题目大意: 有n头牛,f种食物,d种饮料,第i头牛喜欢fi种食物和di种饮料,每种食物或者饮料被一头牛选中后,就不能被其他的牛 ...

  4. [转]Entity Framework and SQL Azure

    本文转自:https://msdn.microsoft.com/zh-cn/library/gg190738 Julie Lerman http://thedatafarm.com April 201 ...

  5. xUtils 简介

    ## xUtils简介* xUtils 包含了很多实用的android工具.* xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(1 ...

  6. 腾讯云COS对象存储的简单使用

    叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...

  7. JDK11源码分析之集合类(一)----HashMap

    一,首先需要拉取JDK11源码: 方便起见我给出芋道源码作者已经拉取好的openJDK11的GitHub地址只需要fork一下克隆到本地导入IDEA中就可以对源码分析了: https://github ...

  8. JAVA一些错误代码

    //算术异常 ArithmeticExecption //空指针异常类 NullPointerException //类型强制转换异常 ClassCastException //数组负下标异常 Neg ...

  9. mysql执行语句汇总

    插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...

  10. Linux之基础命令——文件查看

    cat(连接文件并打印) -n :由 1 开始对所有输出的行数编号. -b :和 -n 相似,只不过对于空白行不编号. -s :当遇到有连续两行以上的空白行,就代换为一行的空白行. [cat a b  ...