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. HDOJ 1301最小生成树的Kruskal算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 将结点的字符信息处理成点信息即可,代码如下: #include<bits/stdc++.h ...

  2. PAT-B 1040. 有几个PAT(25)

    1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...

  3. 基于树莓派与YOLOv3模型的人体目标检测小车(一)

    项目介绍: 本科毕业选的深度学习的毕设,一开始只是学习了一下YOLOv3模型, 按照作者的指示在官网上下载下来权重,配好环境跑出来Demo,后来想着只是跑模型会不会太单薄,于是想了能不能做出来个比较实 ...

  4. CSRF的几种防御方法的利弊分析

    本文直接从防御方式开始讨论,防御CSRF有4种方法: 使用POST替代GET 检验HTTP Referer 验证码 Token 使用POST替代GET 一些程序员在开发的时候都是用GET.POST通用 ...

  5. Spring - 数据库开发概述

      Spring 数据库开发 Spring 的 JDBC 模块负责数据库资源管理和镨误处理,大大简化了开发人员对数据库的操作,使得开发人员可以从繁琐的数据库操作中解脱出来,从而将更多的精力投入到编写业 ...

  6. sql select sql查询

    select 一.课上练习代码 1 查询所有学生信息 select * from tb_student; select * from tb_teacher; 2 查询所有课程名称及学分(投影和别名) ...

  7. 算法(algorithm)

    算法是什么? 算法是指令的集合,是为解决特定问题而规定的一系列操作. 它是明确定义的可计算过程,以一个数据集合作为输入,并产生一个数据集合作为输出. 一个算法通常来说具有以下五个特性: 1.输入:一个 ...

  8. 通过jsDelivr + github 搭建一个简易图床

    应用场景: 在大型项目里需要很多图片时,不会直接把图片存储在项目文件夹里,也不推荐直接用数据库存储,而是用第三方存储,cdn,也可以自己搭个存储图片的服务器,等等方式,如果时自己练练手,做做博客,写写 ...

  9. 微服务架构盛行的时代,你需要了解点 Spring Boot

    随着互联网的高速发展,庞大的用户群体和快速的需求变化已经成为了传统架构的痛点. 在这种情况下,如何从系统架构的角度出发,构建出灵活.易扩展的系统来快速响应需求的变化,同时,随着用户量的增加,如何保证系 ...

  10. 命令行工具nslookup查域名DNS服务器

    在使用的操作系统里进入终端, 1.输入 nslookup 回车 2.输入 set type=ns 回车 3.输入域名(不带WWW的),如:baidu.com 回车 操作过程如下, > set t ...