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. Objective-C 计算代码运行时间

    今天看到一篇关于iOS应用性能优化的文章,其中提到计算代码的运行时间,觉得非常有用,值得收藏.不过在模拟器和真机上是有差异的,以此方法观察程序运行状态,提高效率. 第一种:(最简单的NSDate) N ...

  2. mongodb 基本语法

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  3. windows 守护进程

    use Win32::Process::Info; while (1==1){ use Sys::Hostname; use HTTP::Date qw(time2iso str2time time2 ...

  4. 解决kibana 4 关于响应时间的问题

    "message" => " 10.252.142.174 [12/Sep/2016:16:43:47 +0800] \"GET /resources/j ...

  5. 匹配“is outside location”

    <pre name="code" class="html">is outside location 怎么匹配? . 匹配除换行外的所有单个字符,通常 ...

  6. 【转】 i2c驱动调试经验

    原文网址:http://blog.csdn.net/cmm20071020/article/details/7179958 把一个i2c驱动从2.6.21升级到2.6.39 上网查到一篇帖子,讲了驱动 ...

  7. Java内存回收(垃圾回收)机制总结

    一.背景: Java程序员编写程序时,对于新建的对象,当不再需要此对象时,不必去释放这个对象所占用的空间,这个工作是由Java虚拟机自己完成的 ,即内存回收或垃圾回收. 二.如何知道一个对象所占用的空 ...

  8. jquery datepicker日期控件用法

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...

  9. linux环境下java读取sh脚本并执行

    Process process;           String cmd = "/home/ty/t.sh";//这里必须要给文件赋权限 chmod u+x fileName; ...

  10. [置顶] vi、akw和sed总结