POJ1995 Raising Modulo Numbers【整数快速幂】
来源:互联网 发布:中文数据库 编辑:程序博客网 时间:2024/05/01 18:06
题目链接:
http://poj.org/problem?id=1995
题目大意:
N个人在一起玩游戏,每个人默写两个数字Ai、Bi,在同一个时间公开给其他玩家看。游戏的目的是
为了看谁能够在最快的时间求出所有的Ai^Bi的和对M取模的值。那么问题来了:你能够快速算出
(A1B1+A2B2+ ... +AHBH)mod M 的值吗?
思路:
用二分整数快速幂算法计算出每一个Ai^Bi,然后依次相加取模。
AC代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#define LL __int64using namespace std;LL QuickMod(LL a,LL b,LL m){ LL ans = 1; while(b) { if(b&1) { ans = ans*a%m; b--; } a = a*a%m; b >>= 1; } return ans;}int main(){ int N,M,T; scanf("%d",&T); while(T--) { scanf("%d%d",&M,&N); LL a,b,ans = 0; for(int i = 1; i <= N; ++i) { scanf("%I64d%I64d",&a,&b); ans = (ans + QuickMod(a,b,M)) % M; } printf("%I64d\n",ans); } return 0;}
0 0
- POJ1995 Raising Modulo Numbers【整数快速幂】
- poj1995 Raising Modulo Numbers (整数快速幂)
- POJ1995 Raising Modulo Numbers 快速幂
- poj1995 Raising Modulo Numbers【快速幂】
- poj1995 Raising Modulo Numbers 整数型快速幂 算法的应用
- POJ1995 ZOJ2150 Raising Modulo Numbers【快速模幂】
- Raising Modulo Numbers(poj1995快速模运算)
- POJ1995 Raising Modulo Numbers 快速幂取模
- POJ1995 Raising Modulo Numbers
- poj1995 Raising Modulo Numbers
- POJ1995-Raising Modulo Numbers
- 【poj1995】Raising Modulo Numbers
- POJ1995 Raising Modulo Numbers
- poj1995 Raising Modulo Numbers
- poj1995-Raising Modulo (快速幂)
- poj1995——Raising Modulo Numbers(快速幂取余)
- POJ1995 Raising Modulo Numbers(快速幂取模,模板题)
- Raising Modulo Numbers ---- 快速幂
- 线性链条件随机场CRF
- 图解HTTPS协议加密解密全过程
- 三种最典型的大数据存储技术路线
- 一个类的实例化对象所占空间的大小
- centos5.8本地挂载yum源,安装软件包
- POJ1995 Raising Modulo Numbers【整数快速幂】
- poj 2065 SETI (高斯消元)
- gcov、lcov与genhtml 使用心得
- What is junction object and how to create it?
- UNIX sigwait函数的用法
- [LeetCode][190][Reverse Bits]
- iOS GCD dispatch_async 多线程
- 网盘存储原理探究
- 高效的SQLSERVER分页查询(推荐)