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条边,路径可以反复走. 思路: 感觉很赞的一个题目,据说证明是什 ...
随机推荐
- 洛谷 P1600 天天爱跑步
https://www.luogu.org/problemnew/show/P1600 (仅做记录) 自己的假方法: 每一次跑从a到b:设l=lca(a,b)对于以下产生贡献: a到l的链上所有的点( ...
- Frequency of String CodeForces - 963D
http://codeforces.com/contest/963/problem/D 题解:https://www.cnblogs.com/Blue233333/p/8881614.html 记M为 ...
- git 标签
如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签. 比如说,我们想为我们的 runoob 项目发布一个"1.0"版本. 我们可以 ...
- 牛客网暑期ACM多校训练营(第五场)
J-plan(贪心) 题目描述 There are n students going to travel. And hotel has two types room:double room and t ...
- Tenegrad评价函数 分类: 图像处理 Opencv 2014-11-12 20:46 488人阅读 评论(0) 收藏
Tenegrad函数式一种常用的图像清晰度评价函数,是一种基于梯度的函数. 在图像处理中,一般认为对焦好的图像具有更尖锐的边缘,故具有更大的梯度函数值. Tenegrad函数使用Sobel算子提取水平 ...
- C. Hongcow Builds A Nation 并查集
http://codeforces.com/contest/745/problem/C 把他们并查集后, 其他没有连去government的点,全部放去同一个并查集,然后选择一个节点数最多的gover ...
- XDocument
XDocument学习(Winform) using System; using System.Collections.Generic; using System.ComponentModel; us ...
- javascript动态创建div循环列表
动态循环加载列表,实现vue中v-for的效果 效果图: 代码: var noApplicationRecord = document.getElementById('noApplicationRec ...
- IIS ARR设置HTTP跳转到HTTPS
GUI Version - Select the website you wish to configure- In the “Features View” panel, double click U ...
- 关于docker入门教程
简介:docker入门教程 docker入门教程翻译自docker官方网站的Docker getting started 教程,官方网站:https://docs.docker.com/linux/s ...