Raising Modulo Numbers(POJ 1995 快速幂)

时间:2022-08-26 22:14:44
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&colon;Raising Modulo Numbers&lpar;POJ 1995&rpar;

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

  3. POJ 1995 快速幂模板

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

  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 &lpar;快速幂) 求&lpar;A1B1&plus;A2B2&plus; &period;&period;&period; &plus;AHBH&rpar;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. Vue 方法与事件处理器

    按键修饰符 在监听键盘事件时,我们经常需要检测 keyCode.Vue.js 允许为 v-on 添加按键修饰符: <!-- 只有在 keyCode 是 时调用 vm.submit() --&gt ...

  2. Loadrunner将字符串存为参数

    直接代码: Action() { //定义一个字符串 char *URL= "http://linux.cn"; /******************************** ...

  3. &lbrack;BZOJ1407&rsqb;&lbrack;NOI2002&rsqb;Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  4. Laravel 5 多个视图共享数据的方法

    我们都知道模板一般会用到继承,导航栏就是一个很好的例子,但是导航栏的数据如何共享,比如有个导航的文件叫在view/navigation.blade.php 为了简单一点,文件里只有设置了一个变量 {{ ...

  5. LayoutInflater类详解

    http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html   在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于 ...

  6. 【转】Nginx配置文件详细说明

    Nginx配置文件详细说明 在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等 ...

  7. error:安装手电筒程序后在打开程序后报错:你的camera flashlight正在被其他程序占据

    Q:没打开权限 A:设置里面,把这个app的camera权限打开就行了

  8. winform 之公共控件

    Button 按钮 属性: (一).布局: 1.AutoSize:控件是否根据内容调整大小 2.Location:当前按钮位于界面位置 3.Dock:控件锁定到界面位置 -None:不锁定 4.Mar ...

  9. SSH 等效性问题 总提示输入密码问题

    家目录权限问题 .chmod 700 /home/.. 得到的教训就是没事儿不要乱修改家目录权限,一时方便,可能在别的地方载跟头 ~~ 浪费好许时间 哎

  10. day34

    今日内容: 1.开启子进程的两种方式 2.join方法 3.进程之间内存空间相互隔离 4.僵尸进程和孤儿进程 5.守护进程 6.互斥锁 7.进程对象其他相关的属性和方法 1.开启子进程的两种方式 方式 ...