Raising Modulo Numbers

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 9512 Accepted: 5783

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

(A1^B1+A2^B2+ … +AH^BH)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


解题心得:

  1. 其实直接按照题目中给的公式计算就行了,只不过需要用一下快速幂,这个题主要也就考察了一个快速幂。

#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
ll m,n; ll mod_mult(ll n,ll p) {
ll res = 1;
while(p) {
if(p & 1)
res = (res * n) % m;
n = (n * n) % m;
p >>= 1;
}
return res % m;
} void Solve() {
ll ans = 0;
scanf("%lld%lld",&m,&n);
for(int i=0;i<n;i++){
ll a,b;
scanf("%lld%lld",&a,&b);
ans += mod_mult(a,b);
ans %= m;
}
printf("%lld\n",ans);
} int main() {
int t;
scanf("%d",&t);
while(t--) {
Solve();
}
return 0;
}

POJ:1995-Raising Modulo Numbers(快速幂)的更多相关文章

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

    题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...

  2. POJ 1995:Raising Modulo Numbers 快速幂

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

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

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

  4. POJ1995 Raising Modulo Numbers(快速幂)

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

  5. poj 1995 Raising Modulo Numbers 题解

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

  6. POJ 1995 Raising Modulo Numbers 【快速幂取模】

    题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...

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

    嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...

  8. POJ 1995 Raising Modulo Numbers

    快速幂取模 #include<cstdio> int mod_exp(int a, int b, int c) { int res, t; res = % c; t = a % c; wh ...

  9. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  10. POJ1995:Raising Modulo Numbers(快速幂取余)

    题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...

随机推荐

  1. css透明边框实现

    代码:   css:      .box1{ background: black; padding:20px; } .box2{ background: #fff; border: 10px soli ...

  2. LINQ&EF任我行(二)--LinQ to Object

    (原创:灰灰虫的家http://hi.baidu.com/grayworm)LinQ to Objects是LinQ家庭的核心,其它的LinQ也使用了与LinQ to Objects相同的查询句法.最 ...

  3. Qt开发北斗定位系统融合百度地图API及Qt程序打包发布

    Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...

  4. POJO详解

    转自:http://blog.csdn.net/lushuaiyin/article/details/7436318  一:什么是POJO POJO的名称有多种,pure old java objec ...

  5. 简单的NLog配置文件

    NLog.config <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="h ...

  6. IOS Git源代码管理工具

    .新建一个“本地仓库” $ git init .配置仓库 >告诉git你是谁 git config user.name lnj >告诉git怎么联系你 git config user.em ...

  7. 使用selenium grid遇到的坑,解决不了冲突,只有避免

    背景:使用到grid做分发,已经有两周,运行较稳定,分发也健壮,不知道是不是要因为运行量小,服务器也没出问题,稳定到两周后,发现分发到A服务器(10.40.2.113)和B服务器(10.40.2.11 ...

  8. cgi程序报 Premature end of script headers:

    这段时间写了一个CGI,也是为了应付工作,挺简单的一个程序,总是在调用的时候报:Premature end of script headers: 很让人头疼!   在网上找了些资料,按资料 ---- ...

  9. c# 语法 字符串内插

    结果截屏 参考文章 https://www.cnblogs.com/csproj/p/Interpolated_Strings.html c# 6.0语法 https://www.cnblogs.co ...

  10. 中小学信息学奥林匹克竞赛-理论知识考点--ASCII

    ASCII表说白了就是一张表. 表中记录着:字符 和 数字 的对应关系.比如:字符0对应的ASCII码是48,A对应的是65,a对应的是97. 只要记住这三个,其它的数字,大写,小写字母的ASCII码 ...