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. js实现选项卡切换的三种方式

    前两种主要实现一个选项卡的切换,第三种使用闭包看书,构造函数实现多个选项卡的切换: 1.第一种实现实现效果为: 实现代码为: <!doctype html> <!DOCTYPE ht ...

  2. Powershell变量的类型和强类型

    Powershell变量的类型和强类型12 12月, 2011  在 Powershell  tagged Powershell教程 / 变量 / 存储 / 数据 / 类型 by Mooser Lee ...

  3. 【转】ARM Linux 3.x的设备树(Device Tree)

    原文网址:http://blog.csdn.net/21cnbao/article/details/8457546 1.    ARM Device Tree起源 Linus Torvalds在201 ...

  4. repter导出到Excel

     ;                 ,  * );                 sheet1.SetColumnWidth(,  * );                 sheet1.SetC ...

  5. iOS 大牛

    1,http://lixing123.com 2,http://kayosite.com

  6. ps查看内存占用排序

    ps -eo rss,pmem,pcpu,vsize,args | sort -k 1 -r -n | less 解析一下: ps 都知道,是linux,unix显示进程信息的, -e 是显示所有进程 ...

  7. js调用swift相册DEMO(网易新闻)

    关键代码 window.location.href = 'tg:///openCamera' css body{ } img{ width:100%; } #mainTitle{ text-align ...

  8. 虚拟机IOS开发环境搭建教程

    来源:http://www.cnblogs.com/xiaoyaoju/archive/2013/05/21/3091171.html 安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB ...

  9. javascript学习笔记(一)

    学习书籍 Javascript高级程序设计 第3,4章 javascript数据类型 Undefined(undefined) Null(null,空指针) Boolean(true,false) N ...

  10. 使用bootstrapvalidator的remote验证经验

    这里需要说一下,bootstrapvalidator的帮助文档写的比较简单,对于remote验证器的说明更是如此,在经历多方测试之后才明白如何使用这个验证器. 一个典型的ajax验证代码如下: 服务端 ...