Raising Modulo Numbers(ZOJ 2150)
这题其实就是快速求一个高次幂的模。
这是题目的答案
#include<iostream>
#include<cmath> using namespace std; int a[];
int b[]; int mod(int a, int b, int c)
{
int z = ;
while(b)
{
if(b%)
z = (z*a)%c;
b/=;
a = (a*a)%c;
}
return z;
} int main()
{
int T;
cin>>T;
while(T--)
{
int M;
int H;
cin>>M>>H;
for(int i=; i<H; i++)
{
cin>>a[i]>>b[i];
}
int t =;
int mode = ;
while(t<H)
{
int tem0 = a[t]%M;
mode = (mod(tem0,b[t],M) + mode)%M;
t++;
}
cout<<mode<<endl;
}
}
只分析求幂—模的那个函数,先举例吧(a a a a a a a a a a)这是b个a (b =10),通过z来记录二分时多出来的一个a,每次二分后用a来代替a*a,这样就得到了缩小规模的结果。最后肯定是b=1,所以这时只需用现在的a与z结合就是最后的结果了。
int mod(int a, int b, int c)
{
int z = ;
while(b)
{
if(b%)
z = (z*a)%c;
b/=;
a = (a*a)%c;
}
return z;
}
这个函数的写法就是二分法而已,但是刚开始时我写的是这样的
long long F(long long a, long long b, long long m)
{
if(b==)
return a%m;
else if(b%==)
return (F(a,b/,m)*F(a,b/,m))%m;
else
return (F(a,b/,m)*F(a,b/,m)*a%m);
}
这样写的结果是RE,估计是递归太深导致堆栈溢出了,这个写的很差,根本没有起到二分的效果,反而比O(n)还要费时间。
Raising Modulo Numbers(ZOJ 2150)的更多相关文章
- POJ1995 Raising Modulo Numbers
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6373 Accepted: ...
- poj 1995 Raising Modulo Numbers【快速幂】
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5477 Accepted: ...
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
- 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: ...
- 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 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: 5532 Accepted: ...
- Raising Modulo Numbers
Description People are different. Some secretly read magazines full of interesting girls' pictures, ...
随机推荐
- SparkRDD内核
Spark内核 RDD是基础,是spark中一个基础的抽象,是不可变的,比如我们加载进的数据RDD,如果想更改其中的内容是不允许的:分区的集合,可以并行来计算:RDD类中包含了很多基础的操作,例如ma ...
- vue-cli3 set vue.config.js
//config目录下index.js配置文件// see http://vuejs-templates.github.io/webpack for documentation.// path是nod ...
- 解析XML(2)
在输入法非中文状态下使用ctrl+shift+f可以使文档换行.
- CentOS安装wireshark
yum install wireshark-gnome yum install libpcap
- node.js定时任务:node-schedule的使用
安装 npm install node-schedule 使用方法 1:确定时间 例如:2014年2月14日,15:40执行 var schedule = require("node-sch ...
- Logiscope学习网址
Logiscope测试机理 http://blog.csdn.net/cmy673986/article/details/9163247 http://www.cnitblog.com/qiuya ...
- python 爬取腾讯视频评论
import urllib.request import re import urllib.error headers=('user-agent','Mozilla/5.0 (Windows NT 1 ...
- 每天一个linux命令(12):nl命令
版权声明更新:2017-05-16博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- 服务端返回可执行js格式要求
服务端返回的数据,如果有直接执行的代码,那返回的头部格式中的"Content-Type",不能为"text/plain",不然,浏览器是不会执行返回数据的. 返 ...
- MongoDB优化之三:如何排查MongoDB CPU利用率高的问题
遇到这个问题,99.9999% 的可能性是「用户使用上不合理导致」,本文主要介绍从应用的角度如何排查 MongoDB CPU 利用率高的问题. Step1: 分析数据库正在执行的请求 用户可以通过 M ...