POJ1995:Raising Modulo Numbers
二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html
题目传送门:http://poj.org/problem?id=1995
题目就是求\(\sum_{i=1}^na[i]^{b[i]}mod\) \(m\)。我们只要会快速求\(a^b\)就行了。
我们可以用二进制拆分思想,把\(a^b\)转化成\(a^{(1010...1)_2}\)之类的。然后根据\(a^{x+y}=a^x*a^y\),我们可以将\(a^b\)转化成\(a^{(10000)_2}*a^{(100)_2}*a^{(1)_2}\)之类的形式。当二进制下这一位为\(1\),我们就把它累乘进答案里。
有因为\(a^{2x}=a^x*a^x\),所以对于每一位表示下\(a^{(100..)_2}\),我们可以通过上一位的平方得来。
所以二进制下有多少位,我们就进行多少次运算。
时间复杂度:\(O(nloga)\)
空间复杂度:\(O(1)\)
代码如下:
#include <cstdio>
using namespace std;
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int quick(int x,int y,int m) {
int sum=1;
while(y) {
if(y&1)sum=1ll*sum*x%m;//如果y的二进制当前位置为1,就把x累乘进去
x=1ll*x*x%m;y>>=1;//否则x平方,y右移一位
}
return sum;
}
int main() {
int Z=read();
while(Z--) {
int ans=0,m=read(),n=read();
for(int i=1;i<=n;i++) {
int x=read(),y=read();
ans=(ans+quick(x,y,m))%m;//累加求答案
}printf("%d\n",ans);
}
return 0;
}
POJ1995:Raising Modulo Numbers的更多相关文章
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
- POJ1995 Raising Modulo Numbers
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6373 Accepted: ...
- poj1995 Raising Modulo Numbers【高速幂】
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5500 Accepted: ...
- POJ:1995-Raising Modulo Numbers(快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9512 Accepted: 578 ...
- 【POJ - 1995】Raising Modulo Numbers(快速幂)
-->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1 B1 A2 B2 A3 B3 ......... AH ...
- poj 1995 Raising Modulo Numbers【快速幂】
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5477 Accepted: ...
- Raising Modulo Numbers(POJ 1995 快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5934 Accepted: ...
- poj 1995 Raising Modulo Numbers 题解
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6347 Accepted: ...
随机推荐
- Watering Grass(贪心)
Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters ...
- [JavaScript]WebBrowser控件下IE版本的检测
转载请注明原文地址:https://www.cnblogs.com/litou/p/10772272.htm 在客户端检查用户使用的浏览器类型和版本,都是根据navigator.userAgent属性 ...
- BAPI LIST
[转自 http://blog.csdn.net/minsenwu/article/details/8432081] 库存管理BAPI 库存: 1. BAPI_MATERIAL_AVAILABILIT ...
- QMessageBox简单使用
首先要调用 #include <QMessageBox> 然后 QMessageBox msgBox; msgBox.setWindowTitle("错误"); msg ...
- python基础11 ---函数模块1
函数模块 一.函数模块的作用(为什么要有函数模块) 1.函数模块可以减少代码量 2.函数模块方便阅读 3.函数模块维护性强二.函数模块的本质以及调用方法 1.函数模块的本质就是一个.py结尾的文件,该 ...
- 第7条:用列表推导式来取代map和filter
核心知识点: 1.列表推导式要比内置的map和filter函数清晰,因为它无需额外编写lambda表达式. 2.列表推导式可以跳过输入列表中的某些元素,如果改用map来做,那就必须辅以filter方能 ...
- MyBatis:学习笔记(4)——动态SQL
MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...
- Mysql——JDBC编程 理论介绍
一.JDBC简介(来自俞琰--数据库老师) Java数据库编程主要使用JDBC技术.JDBC是一种用于执行SQL语句的Java API.它由一组用Java编写的类和接口组成.JDBC为开发人员提供了一 ...
- SrpingCloud 之SrpingCloud config分布式配置中心实时刷新
默认情况下是不能及时获取变更的配置文件信息 Spring Cloud分布式配置中心可以采用手动或者自动刷新 1.手动需要人工调用接口 监控中心 2.消息总线实时通知 springbus 动态刷新 ...
- 第二天(1)声明式验证之使用验证框架验证域模型和ModelDriven验证
有一类特殊的属性,即这个属性的类型是另外一个JavaBean,如有一个User类,代码如下: package data; public class User { private String name ...