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. linux磁盘管理1-分区格式化挂载,swap,df,du,dd

    一些基础 硬盘接口类型 ide 早期家庭电脑 scsi 早期服务器 sata 目前家庭电脑 sas 目前服务器 raid卡--阵列卡 网卡绑定 ABI 应用程序与OS之间的底层接口 API 应用程序调 ...

  2. dp - 活动选择问题

    算法目前存在问题,待解决.. 活动选择问题是一类任务调度的问题,目标是选出一个最大的互相兼容的活动集合.例如:学校教室的安排问题,几个班级需要在同一天使用同一间教室,但其中一些班级的使用时间产生冲突, ...

  3. C++11 — lambda表达式(匿名函数)

    C++11中lambda表达式的基本语法格式为: [capture](parameters) -> return_type { /* ... */ } 其中 [] 内为外部变量的传递方式: [] ...

  4. Java后端 带File文件及其它参数的Post请求

    http://www.roak.com Java 带File文件及其它参数的Post请求 对于文件上传,客户端通常就是页面,在前端web页面里实现上传文件不是什么难事,写个form,加上enctype ...

  5. 使用gitthub 创建项目搭建博客

    最近在学Andrew Ng 的机器学习,做了上买那个的练习题和变成作业,想着把自己做的编程作业放到GitHub,方便讨论,虽然之前注册了GitHub,但这两年整天在毕设方向的事,做的身心俱疲,再加上自 ...

  6. Hibernate(九)--N+1问题

    1.在利用Hibernate操作数据库的时候,如果在实体类上设置了表的双向关联.这可能会出现Hibernate N+1的问题. 1.1.一对多: 在一方,查找得到了 n 个对象,那么又需要将 n 个对 ...

  7. 「luogu2633」Count on a tree

    「luogu2633」Count on a tree 传送门 树上主席树板子. 每个节点的根从其父节点更新得到,查询的时候差分一下就好了. 参考代码: #include <algorithm&g ...

  8. centos6.5安装图形操作界面

    yum -y install xorg-x11-fonts-Type1 #安装Xwindow yum -y groupinstall "X Window System" #安装GN ...

  9. 中山DAy2——普及

    今天挺不友好的,早上忘记定闹钟,晚了半小时起床,然后早上信心满满打算弄他个300分.结果……132.2分·.WTF??? T1:disease 题意:有n头奶牛,k种细菌(k<=15),给你每头 ...

  10. Sublime设置html头部

    1.Ctrl + N,新建一个文档:2.Ctrl + Shift + P,打开命令模式,再输入 sshtml 进行模糊匹配,将语法切换到html模式:3.输入 !,再按下 Tab键或者 Ctrl + ...