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, ...
随机推荐
- how to close the old Session - if the same username starts a new Session?
Question: want to close the old Session - if the same username starts a new Session Any ideas how i ...
- sqlite3简单教程整理
一.Ubuntu下安装sqlite3 1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库. 2.安装: 要安装sqlite3,可以在终端提示符后运行下列命令: sud ...
- HTTP- 头部信息
HTTP 头部信息由众多的头域组成,每个头域由一个域名,冒号(:)和域值三部分组成.域名是大小写无关的,域值前可以添加任何数量的空格符,头域可以被扩展为多行,在自每行开始处,使用至少一个空格或制表符. ...
- linux shell编程(三) if 和 for
if 条件判断: 单分支的if语句if 判断条件: then statement1fi双分支的if语句if 判断条件;then statement1 statementelse statement3f ...
- 解析XML(2)
在输入法非中文状态下使用ctrl+shift+f可以使文档换行.
- NULL 与空字符串
空字符串 '' 不占内存空间; NULL占一个字节的空间; 空字符串 的判断用 == <> NULL值用 is null ifnull();
- maven 3.2.5 的安装及简单示例
http://www.mvnrepository.com 一直没有使用maven,它的作用就不说了,这二天需要用到,发现网上都是以前的版本,所以,我一边配置,一边记录. 一 下载maven 现在很多I ...
- QListWidget列表控件:当鼠标选中某行时,系统会自动设置选中的行的行号,用currentRow()返回回来,没有setCheck或setSelect类似函数
列表控件的设计思路: 只有QListWidgetItem自己能改变自己的状态(如checked,selected,颜色等)状态,QListWidget是无法改变其项的状态的. 列表控件是被动接受子项的 ...
- Session 和cookie机制详解
参考: http://blog.csdn.net/fangaoxin/article/details/6952954/ http://blog.csdn.net/hjc1984117/article/ ...
- type_traits.h
type_traits.h // Filename: type_traits.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ht ...