POJ:1995-Raising Modulo Numbers(快速幂)
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
解题心得:
- 其实直接按照题目中给的公式计算就行了,只不过需要用一下快速幂,这个题主要也就考察了一个快速幂。
#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(快速幂)的更多相关文章
- POJ 1995 Raising Modulo Numbers (快速幂)
题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- poj 1995 Raising Modulo Numbers【快速幂】
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5477 Accepted: ...
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
- poj 1995 Raising Modulo Numbers 题解
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6347 Accepted: ...
- POJ 1995 Raising Modulo Numbers 【快速幂取模】
题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...
- POJ 1995 Raising Modulo Numbers(快速幂)
嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...
- 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 ...
- ZOJ2150 Raising Modulo Numbers 快速幂
ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...
- POJ1995:Raising Modulo Numbers(快速幂取余)
题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...
随机推荐
- (生产)better-scroll - 下拉刷新
Options 参数 startX: 0 开始的X轴位置 startY: 0 开始的Y轴位置 scrollY: true 滚动方向为 Y 轴 scrollX: 'true' 滚动方向为 X 轴 cli ...
- sass文件转css时注释虽然支持中文,但是出现乱码的解决方法
sass文件转css时注释虽然支持中文,但是出现乱码的解决方法 Scss 注释中文报错问题(windows系统, 已解决)找到ruby的安装目录,里面也有sass模块,类似这样样的路径:F:\Prog ...
- 关于 Android Studio 如何连接手机调试
第一步:设置-> 打开开发者选项,以及USB调试模式 第二步:关于手机->版本号,点击版本号会弹出提示:已经处于开发者模式,无需操作 第三步:设置->在搜索框中输入HDB,此时会弹出 ...
- SAP ECC6.0-中建信息版
平台: SUSE Enterprise Server 类型: 虚拟机镜像 软件包: sap ecc 6.0 ehp7 sps10 commercial erp sap 服务优惠价: 按服务商许可协议 ...
- java之操作mysql常用方法
一般引用mysql-connector-java这个包. package DBManager; import java.sql.Connection; import java.sql.DriverMa ...
- April 10 2017 Week 15 Monday
He alone is poor who does not possess knowledge. 没有知识,才是贫穷. Knowledge, as a important part of wisdom ...
- C#后台unxi时间戳转换为前台JS时间的方法
后台返回的时间是一个格式为 /Date(1530153274362)/ 的unxi时间戳前台转换代码:var matchResult = data.match(/(\d+)/);if (matchRe ...
- 前端高质量知识(一)-JS内存空间详细图解
变量对象与堆内存 var a = 20; var b = 'abc'; var c = true; var d = { m: 20 } 因为JavaScript具有自动垃圾回收机制,所 ...
- pyinstaller打包后的exe退出时,类中的__del__不执行问题
关于pyinstaller打包后的exe退出时,类中的__del__不执行问题,完善中
- System.Web.UI
类: System.Web.UI.Page 所以窗体继承的类