Raising Modulo Numbers_快速幂取模算法
Description
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
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
【题意】给出h组a,b的值,求a1^b1+a2^b2+...+an^bn之和mod p的值
【思路】快速幂二进制取模算法
参考资料:http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html
http://blog.csdn.net/zhangv123/article/details/47953221
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; int fun(long long int a,long long int b,long long int p)
{
long long int res=;
while(b)
{
if(b&) res=res*a%p;
a=a*a%p;
b>>=;
}
return res;
}
int main()
{
long long int t;
long long int a,b,h,p;
scanf("%lld",&t);
while(t--)
{
long long int ans=;
scanf("%lld%lld",&p,&h);
for(int i=;i<=h;i++)
{
scanf("%lld%lld",&a,&b);
ans=ans+fun(a,b,p); }
ans=ans%p;
printf("%I64d\n",ans);
}
return ;
}
Raising Modulo Numbers_快速幂取模算法的更多相关文章
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- POJ1995:Raising Modulo Numbers(快速幂取余)
题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...
- Powmod快速幂取模
快速幂取模算法详解 1.大数模幂运算的缺陷: 快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算 ...
- 《Java语言实现快速幂取模》
快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算过程中最麻烦的就是我们的5^1003这个过程 ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模
小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- 二分求幂/快速幂取模运算——root(N,k)
二分求幂 int getMi(int a,int b) { ; ) { //当二进制位k位为1时,需要累乘a的2^k次方,然后用ans保存 == ) { ans *= a; } a *= a; b / ...
- 快速幂取模(POJ 1995)
http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c ...
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20309 Accepted: ...
随机推荐
- OUT函数及其熟练使用,split的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Jquery操作select小结
每次操作select都要查资料,干脆总结一下. 为select设置placeholder <select class="form-control selOP" placeho ...
- 闲聊CSS之关于clearfix--清除浮动[转]
.clearfix:after { content: " "; display: block; clear: both; height:; } .clearfix { zoom:; ...
- shell学习记录003-cat命令
cat 命令一般用于文件的查看 cat -s file #可以去除文件中多余的上下空行 cat -T file #Python编程中会用到的制表符会在该命令中体现出来 cat -n file ...
- python 爬虫
import urllib2 as url import re urls = 'http://www.php100.com/html/it/' headers = {'User-Agent':'Moz ...
- 使用ContentResolver添加数据、查询数据
import java.util.ArrayList;import java.util.HashMap;import java.util.Map; import android.os.Bundle;i ...
- ASP.NET MVC的Ajax.ActionLink 的HttpMethod="Get" 一个重复请求的BUG
这段时间使用BootStrap+Asp.net Mvc5开发项目,Ajax.ActionLink遇到一个重复提交的BUG,代码如下: @model IList<WFModel.WF_Temp&g ...
- Delphi的TListView控件拖放选定行操作
http://www.tansoo.cn/?p=401 Delphi的TListView控件拖放选定行操作的例子,效果图如下:TListView控件拖动选定行到指定位置 具体实现步骤: 一.新建一个D ...
- JS编码,解码. asp.net(C#)对应解码,编码
escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...
- 五大要求让BPM与企业对接
BPM(即业务流程管理)在中国已经有多年的发展历史,但人们经常提到的还是企业对流程的迫切需要,鲜有人讨论什么样的企业才能实施BPM,或者换句话说BPM的本身对企业有什么要求.不是所有的工作都适合BPM ...