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. 洛谷 P3808 【模板】AC自动机(简单版) 题解

    原题链接 前置知识: 字典树.(会 \(\texttt{KMP}\) 就更好) 显然呢,本题用 字典树 和 \(\texttt{KMP}\) 无法解决问题. 所以我们发明了一个东西: \(\textt ...

  2. Layui-admin-iframe通过页面链接直接在iframe内打开一个新的页面,实现单页面的效果

    前言: 使用Layui-admin做后台管理框架有很长的一段时间了,但是一直没有对框架内iframe菜单栏切换跳转做深入的了解.今天有一个这样的需求就是通过获取超链接中传递过来的跳转地址和对应的tab ...

  3. JSP+Servlet+C3P0+Mysql实现的azhuo商城

    项目简介 项目来源于:https://gitee.com/xuyizhuo/shopping 原仓库中缺失jar包及sql文件异常,现将修改过的源码上传到百度网盘上. 链接:https://pan.b ...

  4. 基于arduino、百度云、采用django、redis鱼缸在线监控

    大家好,今天我给大家分享一下之前做的一个鱼缸远程监控的案例,希望有人喜欢 首先给大家看一下结构框架,由于我之前买的arduino开发板不带wifi功能,所有是通过pc机转发一下上的百度云,最近我刚购买 ...

  5. Pandas和Numpy的一些金融相关的操作(一)

    Pandas和Numpy的一些金融相关的操作 给定一个净值序列,求出最大回撤 # arr是一个净值的np.ndarray i = np.argmax( (np.maximum.acumulate(ar ...

  6. 震惊!程序员的福音!不需要敲代码就能完成复杂的逻辑应用? —— Azure Logic App

    (大家看完标题可能以为是营销号,哈哈哈哈哈哈哈哈哈...客官请留步, 正经博主....好吧) 今天我们的主题是Azure Logic Apps Azure Logic Apps 是什么? 官方解释:h ...

  7. macOS 去掉系统软件更新红点提示

    当前系统提示更新到macOS Catalina .打开终端执行以下命令: 第一步运行: sudo softwareupdate --ignore "macOS Catalina" ...

  8. HDU - 1166 树状数组模板(线段树也写了一遍)

    题意: 汉语题就不说题意了,用到单点修改和区间查询(树状数组和线段树都可以) 思路: 树状数组的单点查询,单点修改和区间查询. 树状数组是巧妙运用二进制的规律建树,建树就相当于单点修改.这里面用到一个 ...

  9. Python3实现xml转json文件

    使用了Python的 xml.etree.ElementTree 库,Python版本Python 3.6.6 from xml.etree import ElementTree LISTTYPE = ...

  10. Axure RP闪退问题

    Axure RP 在mac 环境,当时安装的是8.好久没用了,最近打开,一开就闪退. 网上找了一下,显示的都是各种文件夹没权限的问题,实验了一下不管用. /Applications/develop/A ...