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

Description

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 AiBi 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
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define DD double
#define LL long long
#define INF 0x3f3f3f
#define MAX 1010
using namespace std;
int fun(int a,int b,int c)
{
int ans=1;
a=a%c;
while(b)
{
if(b&1)
ans=(a*ans)%c;
b=b/2;
a=(a*a)%c;
}
return ans;
}
int main()
{
int t,i,j;
int n,m,sum;
int x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
sum=fun(x,y,m)+sum;
}
printf("%d\n",sum%m);
}
return 0;
}

  

poj 1995 Raising Modulo Numbers【快速幂】的更多相关文章

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

    题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...

  2. POJ 1995:Raising Modulo Numbers 快速幂

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

  3. POJ1995 Raising Modulo Numbers(快速幂)

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

  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 【快速幂取模】

    题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...

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

    嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...

  7. POJ 1995 Raising Modulo Numbers

    快速幂取模 #include<cstdio> int mod_exp(int a, int b, int c) { int res, t; res = % c; t = a % c; wh ...

  8. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  9. POJ1995:Raising Modulo Numbers(快速幂取余)

    题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...

随机推荐

  1. 转: Android异步加载图像小结

    转:http://blog.csdn.net/sgl870927/article/details/6285535 研究了android从网络上异步加载图像,现总结如下: (1)由于android UI ...

  2. 使用原生js与jQuery分别实现一个简单的tab页签

    tab页签通常适用于空间有限而内容较多同时兼顾页面美观度不给用户一种信息过量视觉疲劳的情形.使用面非常广,下面我们用两种方法简单实现之. 首先,构建页面元素.页签的可点击部分我们通常用列表来承载,包括 ...

  3. tomcat配置虚拟目录的步骤

    1.在tomcat中.....\conf\Catalina\localhost中创建一个test.xml文件 2.然后在\conf的server.xml中的 <Host > 元素里面 添加 ...

  4. 限制apache错误日志大小

    ①配置错误日志 在http.conf配置: ErrorLog "| /opt/lampp/bin/rotatelogs /opt/lampp/logs/%Y_%m_%d_error_log  ...

  5. php设计模式:工厂模式

    php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...

  6. c#反射机制判断同一个类的两个实例的值是否完全一样

    ; i < properties1.Length; i++)            {                string s = properties1[i].DeclaringTyp ...

  7. shell编程的一些例子2

    控制语句: 1.if语句 demo_if #!/bin/bash if [ $# -ne 1 ] then echo "参数多于一个" exit 1 fi if [ -f &quo ...

  8. after I see Little Dorrit

    也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...

  9. js带缩略图的图片切换效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 两台主机打通ssh

    ssh打通基本概念:如果需要通过SSH进行远程登录,我们一般是需要手动输入密码,但如果将SSH之间的权限打通的话,就可以实现无密码登录.这对shell脚本的定时执行有很大的帮助. (一),生成秘钥,先 ...