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
思路:题目一大串,有用的就是output(, 裸的欧拉降幂,直接打表求欧拉函数直接算即可(扩展欧拉)
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = ; int phi[maxm]; void getEuler() {
phi[] = ;
for(int i = ; i < maxm; ++i) {
if(!phi[i]) {
for(int j = i; j < maxm; j += i) {
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - );
}
}
}
} LL quick_pow(LL a, LL b, LL p) { //a^b(modp)
LL ret = ;
while(b) {
if(b&) ret = (ret * a) % p;
a = (a * a) % p;
b >>= ;
}
return ret;
} int main() {
int T, H;
getEuler();
LL MOD, A, B;
scanf("%d", &T);
while(T--) {
scanf("%lld", &MOD);
LL ans = ;
scanf("%d", &H);
for(int i = ; i < H; ++i) {
scanf("%lld%lld", &A, &B);
if(B < phi[MOD])
ans = (ans + quick_pow(A, B, MOD)) % MOD;
else
ans = (ans + quick_pow(A, B%phi[MOD]+phi[MOD], MOD)) % MOD;
}
printf("%lld\n", ans);
}
return ;
}

Day7 - J - Raising Modulo Numbers POJ - 1995的更多相关文章

  1. Mathematics:Raising Modulo Numbers(POJ 1995)

    阶乘总和 题目大意:要你算一堆阶乘对m的模... 大水题,对指数二分就可以了... #include <iostream> #include <functional> #inc ...

  2. poj 1995 Raising Modulo Numbers【快速幂】

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

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

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

  4. poj 1995 Raising Modulo Numbers 题解

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

  5. 【POJ - 1995】Raising Modulo Numbers(快速幂)

    -->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1  B1 A2  B2 A3  B3 ......... AH  ...

  6. POJ 1995:Raising Modulo Numbers 快速幂

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

  7. POJ1995 Raising Modulo Numbers

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

  8. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

  9. poj1995 Raising Modulo Numbers【高速幂】

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

随机推荐

  1. 【转】bug management process

    What is Bug? A bug is the consequence/outcome of a coding fault What is Defect? A defect is a variat ...

  2. string和 new string的区别

      栈区  存  引用和基本类型,不能存对象,而堆区存对象.==是比较地址,equals()比较对象内容. (1) String str1 = "abcd"的实现过程:首先栈区创建 ...

  3. bash脚本编程

    一.bash中的变量 变量类型: 本地变量:只对当前shell进程有效,对其子shell以及其它shell都无效;   定义变量:[set]Var_name="value" 变量赋 ...

  4. iOS Common Design Patterns:常用设计模式

    原文:http://www.jianshu.com/p/bf431fff235e 我们经常在编程中使用各种设计模式,在iOS中比较常见的设计模式有:单例模式.委托模式.观察者模式,当然实际上在Coco ...

  5. 使用eclipse部署springcloud config从GitHub上获取配置内容出现错误:Auth fail

    Eclipse点击Window > Preferences > General > Network Connections > SSH2 点击"Key Managem ...

  6. 解题报告:luogu P5745 【深基附B例】数列求和

    题目链接:P5745 [深基附B例]数列求和 现在想说:\(O(N)\)的题要不怎么也想不出来,要不灵光乍现,就像这道题. 我们维护一个类似单调队列的加法单调队列: 若相加大于此数,就将队尾元素弹出, ...

  7. CSP-J2019 纪念品

    Description: Solution: 第一天买入,第二天卖出,在干些别的,再把第二天刚卖出的再买回来,就相当于是啥也没干.也就是说手中的物品本身要算在手中的钱中.这也就是为什么 dp 的状态可 ...

  8. 46 求1+2+3+...+n 静态成员函数和静态变量

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 思路: 1)使用构造函数的方法,需要使用sta ...

  9. 在Centos下单机部署kubernetes

    官方安装手册 https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/ ...

  10. sklearn中调用集成学习算法

    1.集成学习是指对于同一个基础数据集使用不同的机器学习算法进行训练,最后结合不同的算法给出的意见进行决策,这个方法兼顾了许多算法的"意见",比较全面,因此在机器学习领域也使用地非常 ...