题干:

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

题目大意:

第一行测试样例个数t,第二行mod,第三行读入个数m,然后读m行(a,b),求a^b的和%mod即可。

AC代码:(改成scanf读入反而更慢了是什么鬼125ms)

#include<cstdio>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
ll m;
ll qpow(ll a,ll k) {ll ans = 1;while(k) {if(k&1) ans=(ans*a)%m;k>>=1;a=(a*a)%m;}return ans%m;
}int main()
{int t;ll sum = 0,a,b;cin>>t;while(t--) {ll n;scanf("%lld%lld",&m,&n);sum = 0;while(n--) {scanf("%lld%lld",&a,&b);sum += qpow(a,b);sum%=m;}printf("%lld\n",sum);}return 0 ;
}

【POJ - 1995】Raising Modulo Numbers(裸的快速幂)相关推荐

  1. poj 1995 Raising Modulo Numbers 二分快速幂

    题意:给定n对Ai,Bi,求所有Ai的Bi次方之和对M取模的结果: 思路:二分法求快速幂: #include<cstdio> #include<cstring> #includ ...

  2. Raising Modulo Numbers

    http://poj.org/problem?id=1995 题解:快速幂 /* *@Author: STZG *@Language: C++ */ //#include <bits/stdc+ ...

  3. 【poj1995】Raising Modulo Numbers

    problem T组数据,每组包含n对ai,bi和一个p. 每组输出一个答案,∑ni=1aibi%p∑i=1naibi%p \sum_{i=1}^n ai^{bi} \%p. solution 快速幂 ...

  4. POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]

    http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...

  5. POJ 3233 Matrix Power Serie (矩阵快速幂)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. ...

  6. POJ - 2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目链接:点击查看 题目大意:给出 n 个长度不大于 10 的字符串表示病毒串,再给出一个长度 len ,问长度为 len 的字符串中,有多少个字符串不含有病毒串作为子串 题目分析:因为病毒串的长度和 ...

  7. POJ - 3613 Cow Relays(Floyd思想+矩阵快速幂+动态规划)

    题目链接:点击查看 题目大意:给定一张由T(T<=100)条边构成的无向图,点的编号为1~1000,之间的整数,求从起点S到终点E恰好经过N(N<=1e6)条边(可重复经过)的最短路 题目 ...

  8. Poj1995--Raising Modulo Numbers(快速幂)

    题目: http://poj.org/problem?id=1995 Input The input consists of Z assignments. The number of them is ...

  9. c语言的幂乘积表达式,POJ 1845 Sumdiv [素数分解 快速幂取模 二分求和等比数列]

    大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. ,其中 为素数 2) 约数和 ...

最新文章

  1. IOS初级:NSKeyedArchiver
  2. 资料分享:推荐一本《简单粗暴TensorFlow 2.0》开源电子书!
  3. GitHub聊天通信开源UI框架stfalcon-studio/ChatKit使用教程
  4. java任务分解_Spark如何将切片分解为任务/执行者/工作者?
  5. 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
  6. 简谈WP,IOS,Android智能手机OS
  7. matlab批量对图片进行添加椒盐噪声并批量保存到文件夹
  8. VTK:PolyData之MiscCellData
  9. 自定义浏览器协议,实现web程序调用本地程序
  10. web开发中的 emmet 效率提升工具
  11. WPF中的鼠标事件详解
  12. 少年班招生大扩容,清华、北大加入争夺行列,每年增至近800人
  13. js ul 清缓存_JavaScript使用ul中li标签实现删除效果
  14. 压力测试jmeter入门教程
  15. 韩顺平老师多用户即时通讯系统功能扩展:发送离线消息
  16. 服务器邮箱验证失败是什么意思,发送邮件时,提示“邮件发送失败:SMTP验证失败”?...
  17. vue 动态显示图片报错 404
  18. 微信小程序实现押金管理(支付押金、申请退还押金、押金明细)
  19. C++:使用高斯-勒让德正交估计柯西主值 (CPV) 某些奇异积分(附完整源码)
  20. 伯克利计算机科学录取率,加州大学伯克利分校计算机科学

热门文章

  1. 【数据结构与算法】哈希算法
  2. [剑指offer][JAVA]面试题第[18]题[删除链表的节点]
  3. textarea回车不换行 小程序_微信小程序商城到底值得不值得开通?
  4. 计算机专业的第二批本科大学,第二批本科院校
  5. Snap svg 主要对象
  6. java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法
  7. Linux编程练习 --多线程3--mutex
  8. Windows进程系列(2) -- Svchost进程
  9. python爬虫抓取文本_Python实现可获取网易页面所有文本信息的网易网络爬虫功能示例...
  10. ie浏览器网页版进入_IE浏览器打开网页速度很慢的解决办法