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 Ai
Bi 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

题目大意:给一个M, 再给一个n表示接下来有n组数据(a,b) 计算a的b次幂,在将这n组数据加在一起。 然后对M求余。
快速幂求余,a^b%m=[(a%m)^b]%m
同余定理 (a+b+c...)%m=(a%m+b%m+c%m...)%m
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll m;//模
int pow(ll x,ll y)
{
ll res=;
while(y)
{
if(y&)
res=res*x%m;
x=x*x%m;
y>>=;
}
return res%m;//(a+b+c...)%m=(a%m+b%m+c%m..)%m
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll c;
scanf("%lld %lld",&m,&c);
ll a,b,sum=;
for(int i=;i<c;i++)
{
scanf("%lld %lld",&a,&b);
sum+=pow(a,b);
}
printf("%lld\n",sum%m); }
return ;
}

B - Raising Modulo Numbers的更多相关文章

  1. POJ1995 Raising Modulo Numbers

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

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

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

  3. POJ1995 Raising Modulo Numbers(快速幂)

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

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

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

  5. poj 1995 Raising Modulo Numbers 题解

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

  6. poj1995 Raising Modulo Numbers【高速幂】

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

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

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

  8. POJ 1995:Raising Modulo Numbers 快速幂

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

  9. Raising Modulo Numbers

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

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

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

随机推荐

  1. 动态规划-Minimum Distance to Type a Word Using Two Fingers

    2020-01-12 18:28:13 问题描述: 问题求解: 本题还是非常困难的,至少我在看到这个题目的时候是没有想到怎么解决的.我当时联想到的题目是那条grid走两遍的题目,那条题目也很麻烦,使用 ...

  2. 企业级自动化部署方案——ansible实现tomcat自动安装和配置

    共耗时10多个小时 思路一 总体设计 ansible-playbook目录结构 [root@ansible ~]# tree /etc/ansible/roles/tomcat /etc/ansibl ...

  3. 使用Docsify做文档网站的详细配置教程

    使用Docsify做文档网站的详细配置教程 作者:xhemj 没错,它叫Docsify. xhemj的文档中心就是用这个写的 开源地址:https://github.com/docsifyjs/doc ...

  4. Python-标准模块与第三方模块

    标准模块 python官方自己的模块 os os与os.path os.chdir('desktop')        跳转至指定目录 os.listdir()                    ...

  5. Linux下段错误(C语言)

    问题描述:在Linux下编程有时会出现段错误的提醒,出现这种错误有可能是因为以下几种原因 1.数组越界:如果在初始化或者接收输入时内容超过了定义好的数组元素个数时会出现段错误,Linux的数组越界检查 ...

  6. iOS 架构

    一.MVC MVC 全名 Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离 ...

  7. 6.Maven构建过程的各个环节

    构建过程中的各个环节 [1]清理:将以前编译得到的旧的class字节码文件删除,为下一次编译做准备 [2]编译:将Java源程序编译成class字节码文件 [3]测试:自动测试,自动调用junit程序 ...

  8. ASP.NET Core技术研究-探秘Host主机启动过程

    当我们将原有ASP.NET 应用程序升级迁移到ASP.NET Core之后,我们发现代码工程中多了两个类Program类和Startup类. 接下来我们详细探秘一下通用主机Host的启动过程. 一.P ...

  9. 用c#每日更换“必应背景图片”为“桌面壁纸”

    必应每天都会更换背景图片,都非常漂亮,有的时候还十分惊艳,同时还会根据每个地区的特色不同应用不同的图片. 下面用c#抓取必应每天的背景图片,并实现桌面壁纸的每天自动切换 实现思路 1.通过获取&quo ...

  10. 细数Java项目中用过的配置文件(ini 篇)

    Java 菜鸟,会把可变的配置信息写死在代码里:Java 老鸟,会把可变的配置信息提取到配置文件中.坊间流传这么一句非科学的衡量标准,来评判程序员的级别. 那么,项目中的配置信息,你平时都是怎样来实现 ...