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

  1. 3
  2. 16
  3. 4
  4. 2 3
  5. 3 4
  6. 4 5
  7. 5 6
  8. 36123
  9. 1
  10. 2374859 3029382
  11. 17
  12. 1
  13. 3 18132

Sample Output

  1. 2
  2. 13195
  3. 13
    思路:题目一大串,有用的就是output(, 裸的欧拉降幂,直接打表求欧拉函数直接算即可(扩展欧拉)
  1. typedef long long LL;
  2. typedef pair<LL, LL> PLL;
  3.  
  4. const int maxm = ;
  5.  
  6. int phi[maxm];
  7.  
  8. void getEuler() {
  9. phi[] = ;
  10. for(int i = ; i < maxm; ++i) {
  11. if(!phi[i]) {
  12. for(int j = i; j < maxm; j += i) {
  13. if(!phi[j]) phi[j] = j;
  14. phi[j] = phi[j] / i * (i - );
  15. }
  16. }
  17. }
  18. }
  19.  
  20. LL quick_pow(LL a, LL b, LL p) { //a^b(modp)
  21. LL ret = ;
  22. while(b) {
  23. if(b&) ret = (ret * a) % p;
  24. a = (a * a) % p;
  25. b >>= ;
  26. }
  27. return ret;
  28. }
  29.  
  30. int main() {
  31. int T, H;
  32. getEuler();
  33. LL MOD, A, B;
  34. scanf("%d", &T);
  35. while(T--) {
  36. scanf("%lld", &MOD);
  37. LL ans = ;
  38. scanf("%d", &H);
  39. for(int i = ; i < H; ++i) {
  40. scanf("%lld%lld", &A, &B);
  41. if(B < phi[MOD])
  42. ans = (ans + quick_pow(A, B, MOD)) % MOD;
  43. else
  44. ans = (ans + quick_pow(A, B%phi[MOD]+phi[MOD], MOD)) % MOD;
  45. }
  46. printf("%lld\n", ans);
  47. }
  48. return ;
  49. }

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. python包管理历史

    1.标准库工具distutils,2000年发布,是包安装和发布工具 setup.python 程序,利用distutils 开发 示例: python setup.py install 安装一个包 ...

  2. HDU1029 简单DP

    "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. &quo ...

  3. Spring 各个组件架构

  4. Java最新面试题

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  5. WEB, Flask - Session&Cookie

    参考: https://blog.csdn.net/nunchakushuang/article/details/74652877 http://portal.xiaoxiangzi.com/Prog ...

  6. Centos7 配置subversion

    CentOS7:配置SVN服务器 Posted on 2016-11-10 15:17 eastson 阅读(4266) 评论(0) 编辑 收藏 1. 安装 CentOS通过yum安装subversi ...

  7. JavaScript图形实例:图形的平移和对称变换

    1.1  六瓣花平移变换 平移变换是指图形从一个位置到另一个位置所作的直线移动.如果要把一个位于P(x,y)的点移到新位置P’(x’,y’),如图1,则只要在原坐标上加上平移距离Tx和Ty即可. 即  ...

  8. MacBook OSX VMWare Fusion 11安装 Tools For Windows

    需要加载对应客户机系统的安装文件,可以在/Applications/VMware\ Fusion.app/Contents/Library/isoimages文件夹下找到: 设置虚拟机的光驱: 在虚拟 ...

  9. dir815_FW_102.bin路由器固件解压碰到的坑

    在跟随大神kczwa1进行路由器漏洞分析时,对dir815_FW_102.bin 固件文件用binwalk -e dir815_FW_102.bin命令进行解压时,在根目录squashfs-root下 ...

  10. windows系统下 VUE cli手脚架环境安装

    1.安装 node.js环境  (cmd命令工具里输入 node -v 检测是否安装成功) 2.安装VUE 全局环境 npm install --global vue-cli (cmd命令工具里面安装 ...