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, ...
随机推荐
- mysql查看和设置timeout 和批量杀死链接进程
查看的语句 show global variables like "%timeout%"; 结果: +-----------------------------+--------- ...
- TF卡
1.我的手机 64G 2.冬冬手机 16G 3.茜茜收音机 8G(创见) 4.父 手机 8G(Kingston) 5. 6. 7.
- table+css与xhtmL+css实现同一登陆框(代码片段)
1.从网上下载了一个不错的登录框样式(table结合css布局): 2.初学xhtmL,自己改写了一下,希望有用 3. 我怎么不会上传截图呢? 1. [代码]xhtml_css组合 < ...
- hadoop_异常_01_ Unable to determine address of the host-falling back to "localhost" address java.net.UnknownHostException: rayner
一.异常现象 安装好hadoop之后,执行格式化namenode命令时,抛出以下异常: // :: WARN net.DNS: Unable to determine local hostname - ...
- Linux基本语法
Shell编程 摘要: Shell历史 Shell的作用是解释用户的命令,用户输入一条命令,Shell就解释执行一条,这条方式称为交互式(interactive),Shell还有一种执行命令的方式称为 ...
- 简单使用XStream
Stream1.什么作用 *可以把JavaBean转换为(序列化为)xml2.XStream的jar *核心jar:xstream-1.4.7.jar *必须依赖包:xpp3_min-1.1.4c(X ...
- VC++6.0编译环境介绍
大家可能一直在用VC开发软件,但是对于这个编译器却未必很了解.原因是多方面的.大多数情况下,我们只停留在"使用"它,而不会想去"了解"它.因为它只是一个工具,我 ...
- Android之SurfaceView实现视频播放
1.案例一 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- Android之SharedPreferences权限
import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; pub ...
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...