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. Faster-RCNN Pytorch实现的minibatch包装

    实际上faster-rcnn对于输入的图片是有resize操作的,在resize的图片基础上提取feature map,而后generate一定数量的RoI. 我想首先去掉这个resize的操作,对每 ...

  2. win7系统中开启wifi热点

    1.进入cmd下 2.输入命令创建一个热点,名称为testwifi,密码为12345678 netsh wlan 3.进入网络和共享中心->更改适配器设置,看到多出一个“无线网络连接2”,选中本 ...

  3. Caffe2 用户手册概览(Caffe2 Tutorials Overview)[1]

    在开始之前,我们很感激你对Caffe2感兴趣,希望Caffe2在你的机器学习作品中是一个高性能的框架.Caffe2致力于模块化,促进深度学习想法和原型的实现. 选择你的学习路线   1. 使用一个现成 ...

  4. 什么是Socket:

    先了解一些前提: 网络由下往上分为 物理层 .数据链路层 . 网络层 . 传输层 . 会话层 . 表现层 和 应用层.通过初步了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对 ...

  5. Python数据类型-5 元组

    元组 我们知道,用方括号括起来的是列表,那么用圆括号括起来的是什么,是元组. 元组也是序列结构,但是是一种不可变序列,你可以简单的理解为内容不可变的列表.除了在内部元素不可修改的区别外,元组和列表的用 ...

  6. WDF驱动中KMDF与UMDF区别

    抄的 早期的Windows 95/98的设备驱动是VxD(Virtual Device Driver),其中x表示某一类设备.从Windows 2000开始,开发驱动程序必以WDM(Windows D ...

  7. 学习不一样的Vue1:环境搭建

    学习不一样的Vue1:环境搭建  发表于 2017-05-31 |  分类于 web前端|  |  阅读次数 11677 首先 首发博客: 我的博客 项目源码: 源码 项目预览: 预览 因为个人的喜好 ...

  8. HttpClient 以post的方式发送请求(由于请求参数太多所以改成以post提交表单的方式)

    1:Util类方法 /** * 发送 Post请求 * * @param url * @param reqXml * @return */ public static String post(Stri ...

  9. CSS - icon图标(icon font)

    1. 概念 这个小红点是图标,图标在CSS中实际上是字体. 2. 为什么出现本质是字体的图标? 2.1 图片增加了总文件的大小. 2.2 图片增加了额外的http请求,大大降低网页的性能. 2.3 图 ...

  10. C++11并发编程1------并发介绍

    也许有人会觉得多线程和并发难用,复杂,还会让代码出现各种各样的问题,不过,其实它是一个强有力的工具,能让程序充分利用硬件资源,让程序运行得更快. 何谓并发: 两个或更多独立得活动同时发生.计算机中就是 ...