Raising Modulo Numbers
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5934   Accepted: 3461

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  //Z
16  //m
4  //h
2 3  h个a,b;
3 4
4 5
5 6  //计算(A1B1+A2B2+ ... +AHBH)mod M.
36123
1
2374859 3029382
17
1
3 18132

Sample Output

2
13195
13
快速幂模板题
 #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define Max 45000+10
LL a[Max],b[Max];
int m,h;
LL ans;
int mod_pow(int num,int n)
{
num=num%m;
LL res=;
while(n>)
{
if(n&)
res=res*num%m;
num=num*num%m;
n>>=;
}
return res;
}
int main()
{
int z;
int i,j;
freopen("in.txt","r",stdin);
scanf("%d",&z);
while(z--)
{
ans=;
scanf("%d%d",&m,&h);
// cout<<m<<" "<<h<<endl;
for(i=;i<h;i++)
scanf("%lld%lld",&a[i],&b[i]);
for(i=;i<h;i++)
{
ans+=mod_pow(a[i],b[i]);
ans=ans%m;
}
printf("%d\n",ans%m);
}
}

Raising Modulo Numbers(POJ 1995 快速幂)的更多相关文章

  1. Day7 - J - Raising Modulo Numbers POJ - 1995

    People are different. Some secretly read magazines full of interesting girls' pictures, others creat ...

  2. Mathematics:Raising Modulo Numbers(POJ 1995)

    阶乘总和 题目大意:要你算一堆阶乘对m的模... 大水题,对指数二分就可以了... #include <iostream> #include <functional> #inc ...

  3. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...

  4. poj 1995 快速幂

    题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11  11=2^0+2^1+2^3    => 3^1*3 ...

  5. POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

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

    -->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1  B1 A2  B2 A3  B3 ......... AH  ...

  7. POJ 1995:Raising Modulo Numbers 快速幂

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

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

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

  9. poj 1995 Raising Modulo Numbers【快速幂】

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

随机推荐

  1. C语言预处理指令的初步了解

    所谓预处理是指在进行编译的第一遍扫描(词法扫描和语法分析)之前所作的工作.预处理是C语言的一个重要功能,它由预处理程序负责完成.当对一个源文件进行编译时,系统将自动引用预处理程序对源程序中的预处理部分 ...

  2. 本地windows主机无法访问虚拟机里主机解决办法

    一:设置虚拟机里IP,使其与本地计算机IP在同一网段 本地计算机网络IP设置如下: 虚拟机里ip为192.168.1.9 设置IP步骤请参考:Linux里如何设置IP(RED HAT) 二:将虚拟机网 ...

  3. 图片延迟加载scrollLoading.js应用

     <ul>                     <li><a href="http://news.qq.com/" target="_b ...

  4. 沙湖王 | 用K-均值聚类给女明星们的身材分分类

    沙湖王 | 用K-均值聚类给女明星们的身材分分类 http://www.shahuwang.com/2012/07/21/%E7%94%A8scipy%E5%AE%9E%E7%8E%B0k-means ...

  5. hdu 5640 King's Cake(模拟)

    Problem Description   It is the king's birthday before the military parade . The ministers prepared ...

  6. Tcp实现简单的大小写转换功能

    有这样一个需求: 客户端给读物段发送文本,服务端会将文本转换为大写再返回客户端 而且客户端可以不断的进行文本转换,当客户端输入over时,转换结束. 分析: 既然是操作设备上的数据,那么久可以使用io ...

  7. 推荐一本好书给即将走入工作的程序员and程序媴

    近期买了几本IT届推崇的经典书籍.当中有一本<程序猿修炼之道:专业程序猿必知的33个技巧>.由于这本比較薄,所以先翻着看. 这本书有别于其它的技术书籍,事实上算不上一本技术书籍.它不是教你 ...

  8. [HeadFirst-JSPServlet学习笔记][第三章:实战MVC]

    第三章 实战MVC J2EE如何集成一切 Java2企业版(Java 2 Enterprise Editon,J2EE)是一种超级规范.规定了servlets2.4,JSP2.0,EJB2.1(Ent ...

  9. linux学习记录 常用指令大全

    1.开启关闭服务器(即时生效): service iptasbles start service iptasbles stop 2.在开启了防火墙时,做如下设置,开启相关端口, 修改/etc/sysc ...

  10. C++菱形继承的构造函数

    网上搜了很多,大多是关于菱形虚继承的构造函数应该怎么写,或者就是最简单的,四个类都不带参数的构造函数. 本文旨在记录一下困扰了博主1h的问题,非常浅显,有帮助固然好,如果侮辱谁的智商还见谅,当然无限欢 ...