POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M
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 Ai Bi 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
结合律 |
((a+b) mod p + c)mod p = (a + (b+c) mod p) mod p((a*b) mod p * c)mod p = (a * (b*c) mod p) mod p |
交换律 |
(a + b) mod p = (b+a) mod p(a × b) mod p = (b × a) mod p |
分配律 |
((a +b)mod p × c) mod p = ((a × c) mod p + (b × c) mod p) mod p(a×b) mod c=(a mod c * b mod c) mod c(a+b) mod c=(a mod c+ b mod c) mod c(a-b) mod c=(a mod c- b mod c) mod c |
#include<cstdio>
__int64 f(__int64 a,__int64 b,__int64 m)
{
__int64 sun=;
while(b)
{
if(b % != )
{
sun=sun*a%m;
}
a=a*a%m;
b/=;
}
return sun%m;
}
int main()
{
int t,i;
__int64 n,m,sum,a,b;
scanf("%d",&t);
while(t--)
{
sum=;
scanf("%I64d",&m);
scanf("%I64d",&n);
for(i = ; i < n; i++)
{
scanf("%I64d %I64d",&a,&b);
sum+=f(a,b,m);
// printf("---%d--\n",f(a,b,m));
}
printf("%I64d\n",sum%m);
}
}
POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M的更多相关文章
- Raising Modulo Numbers(POJ 1995 快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5934 Accepted: ...
- POJ 1995 快速幂模板
http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...
- poj 1995 快速幂
题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11 11=2^0+2^1+2^3 => 3^1*3 ...
- POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...
- HDU4869:Turn the pokers(快速幂求逆元+组合数)
题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...
- 小白月赛13 小A的路径 (矩阵快速幂求距离为k的路径数)
链接:https://ac.nowcoder.com/acm/contest/549/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- hdu 2065 "红色病毒"问题(快速幂求模)
n=1 --> ans = 2 = 1*2 = 2^0(2^0+1) n=2 --> ans = 6 = 2*3 = 2^1(2^1+1) n=3 --> ans = 20 ...
- codeforce 227E 矩阵快速幂求斐波那契+N个连续数求最大公约数+斐波那契数列的性质
E. Anniversary time limit per test2 seconds memory limit per test256 megabytes inputstandard input o ...
- POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)
题意: 给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路: 感觉很赞的一个题目,据说证明是什 ...
随机推荐
- [USACO4.1]麦香牛块Beef McNuggets By cellur925
题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办法让这种可怕的设想泡汤.奶牛们进行斗争的策略之一是“劣质的包装”.“看,”奶牛们说,“如 ...
- Windows中句柄和ID的区别
写在前面:这里介绍句柄 对于“句柄”,在下一直停留在一知半解的认识层面,近日在下学习Windows编程,决定趁此机会将句柄彻底搞清楚.查阅了一些网络上的资料,发现网络上的讲解大概可以分为两类:一种是以 ...
- 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers
题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...
- 171 Excel Sheet Column Number Excel表列序号 26进制转10进制
给定一个Excel表格中的列名称,返回其相应的列序号.示例: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -&g ...
- .Net应用自定义鼠标样式
(调用系统API的方法) 1.引用命名空间 using System.Runtime.InteropServices; 命名空间提供各种各样支持 COM 互操作 及平台调用服务的成员.using Sy ...
- ASP.Net 控件
简单控件 Label -作用是显示文字,编译后元素是Span 1.文本类 边框: BorderColor 边框颜色 BordersTyle 边框样式 BorderWidth 边框粗细 Literal- ...
- 快速体验openstack-用devstack安装openstack
官网安装说明: --2014年11月15日14:14:21 安装环境:Ubuntu12.04,安装官网的说明遇到了小问题,记录在这里 --http://docs.openstack.org/devel ...
- 如何看Spring源码
想要深入的熟悉了解Spring源码,我觉得第一步就是要有一个能跑起来的极尽简单的框架,下面我就教大家搭建一个最简单的Spring框架,而且是基于Java Config形式的零配置Spring框架. 首 ...
- [BZOJ1257][CQOI2007]余数之和sum 数学+分块
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1257 题目所求为$$Ans=\sum_{i=1}^nk%i$$ 将其简单变形一下$$Ans ...
- react学习文档
转自http://www.ruanyifeng.com/blog/2015/03/react.html,阮一峰老师的博客. 最近想学习react,官方文档的例子不是那么浅显易懂,看了相关博客,觉得阮一 ...