POJ 1995:Raising Modulo Numbers 快速幂
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 5532 | Accepted: 3210 |
Description
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
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
(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
题意就是求(A1B1+A2B2+ ... +AHBH)mod M.
原来求一个数A的B次幂都是一级一级循环,时间复杂度是O(n),且最后取余M的话很容易溢出。
快速幂:比方说求2的9次幂,它的思想是求2的4次幂*2的4次幂*2,这样2的4次幂只需求一次。2的4次幂怎么求,还是和原来一样,是2的2次幂*2的2次幂,2的2次幂等于2*2;
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; long long getresult(long long m,long long n,long long k)
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m) % k;
n = n >> 1;
m = (m*m) % k;
}
return b;
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); long long Test,i,n,k,temp1,temp2,result;
cin>>Test; while(Test--)
{
result = 0; cin >> k >> n;
for (i = 1; i <= n; i++)
{
cin >> temp1 >> temp2;
result += getresult(temp1, temp2, k);
result = result%k;
}
cout << (result%k) << endl;
}
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: 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> ...
随机推荐
- eos 智能合约开发体验
eos编译安装 eos 特性 数据存储 eos投票智能合约开发 eos投票智能合约部署测试 注意避坑 eos编译安装 ERROR: Could not find a package configura ...
- 5G时代能携号转网,你会提前换新手机吗?
每一次网络制式的变革,总能引发业界和大众的强烈关注.毕竟网络制式的迭代,能为社会的方方面面带来极大嬗变.而5G时代即将开启,必然会出现更多弄潮儿,也会带来让人心动的美好生活.不过,鉴于5G本身的特殊性 ...
- phpMydmin的GetShell思路
phpMyadmin简介 phpMyadmin是一个以PHP为基础的MySQL数据库管理工具,使网站管理员可通过Web接口管理数据库 . 信息收集 此部分主要需要收集的是网站物理路径,否则后续无法通过 ...
- Vmware 和 VisualSVN-Server端口冲突
安装 VisualSVN-Server 时,发现他和 Vmware 在端口 443 冲突: 先把本地自启动的 Vmware 全部停止,并改成手工启动服务: 这样可以节省资源,再安装 svn服务时 ...
- wincc的服务器-客户机模式具体做法(全抄-未测试)
一.原来的工作方式:在同一工作组中4台计算机其windows名分别为A.B.C.D且都已安装好wincc5.0+sp2,原来在每台计算机上运行的均是单用户,4台计算机上实际运行的是一个相同的项目,最先 ...
- JuJu团队11月25号工作汇报
JuJu团队11月25号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 实现随机采样函数,进行onehot处理 预处理数据集,将数据集转为矩阵读入 数据集预处理比想象中麻 ...
- springboot启动总是启动不起来UnsatisfiedDependencyException异常
采用debug模式启动总是停留在 点击后: 鼠标放在ex上: org.springframework.beans.factory.UnsatisfiedDependencyException: Err ...
- JS - 查找字符串中的某个值,截取其之前。和之后的值
var str = "11:222"; /* * 截取 “ :”之前和之后的值 */document.write(str.split(':')[0]) //输出11doc ...
- 【转载】Android Gradle Build Error:Some file crunching failed, see logs for details解决办法
Android Gradle Build Error:Some file crunching failed, see logs for details解决办法 转载请标明出处: http://www. ...
- namenode节点无法自动切换主从
当停掉主namenode节点,从节点无法切换到active状态,有两种可能导致这种问题 1.查看namenode上的zkfc日志,发现没有fuser命令,需要手动安装 yum install -y p ...