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. Tomcat启动报Error listenerStart错误 | "beans" 必须匹配 DOCTYPE 根 "null" | java.lang.reflect.MalformedParameterizedTypeException

    maven打包发布工程时,发布上去却报错FAIL - Deployed application at context path /ch but context failed to start 在服务器 ...

  2. 分析一点python源代码

    偶然看了一下python的部分源代码,感觉python的作者写的代码真心很美,简洁美观,学习之. 举几个例子抛砖引玉一下: def removedirs(name): ""&quo ...

  3. shell脚本学习(3)文件判断

    shell常用的文件判断运算符如下: -e 文件是否存在 -f  文件是否是普通文件(不是目录.设备文件.链接文件) -s  表示文件大小不为0 -d 表示文件是否是目录 -b 表示是块设备(光驱.软 ...

  4. Linux安装 NTFS 支持

    my system kernel Version is 2.6.18-128.el5 在这个地址下载了 kernel-module-ntfs-2.6.18-128.el5-2.1.27-0.rr.10 ...

  5. Leetcode back(215) to be continue

    solution discussion https://leetcode.com/problems/kth-largest-element-in-an-array/description/ -- 21 ...

  6. ABI 管理

    https://developer.android.google.cn/ndk/guides/abis.html 不同 Android 手机使用不同的 CPU,因此支持不同的指令集.CPU 与指令集的 ...

  7. Codeforces 760A Petr and a calendar

    题目链接:http://codeforces.com/problemset/problem/760/A 题意:日历需要多少列. #include <bits/stdc++.h> using ...

  8. BestCoder Round #91 1001 Lotus and Characters

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意: Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的 ...

  9. 贪心,POJ(2709)

    题目链接:http://poj.org/problem?id=2709 解题报告: #include <stdio.h> #include <algorithm> #inclu ...

  10. 轻量级HTTP服务器Nginx(Nginx性能优化技巧)

    轻量级HTTP服务器Nginx(Nginx性能优化技巧)   文章来源于南非蚂蚁   一.编译安装过程优化 1.减小Nginx编译后的文件大小在编译Nginx时,默认以debug模式进行,而在debu ...