已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
快速幂求余算法(OJ T1128)求ab%c(这就是著名的RSA公钥的加密方法)当a,b很大时,直接求解这个问题不太可能,你能想到哪些优化呢?算法1:直观上,也许最容易想到的是利用a*b%c=(a%c)*b)%c,这样每一步都进行这种处理,这就解决了ab可能太大存不下的问题,但这个算法的时间复杂度依然是O(n),根本没有得到优化。当b很大时运行时间会很长算法2:另一种算法利用了分治的思想,可以达到O(logn)。 可以把b按二进制展开为b=p(n)*2n+p(n-1)*2(n-1)+.+p(1)*2+p(0) 其中p(i) (0=i=1) if(k%2=1) b=a*b%m; a=a*a%m; k=k/2; return b; 例题一、高级机密Time Limit:1000MS Memory Limit:65536KTotal Submit:43 Accepted:16 Description 在很多情况下,我们需要对信息进行加密。特别是随着Internet的飞速发展,加密技术就显得尤为重要。 很早以前,罗马人为了在战争中传递信息,频繁地使用替换法进行信息加密。然而在计算机技术高速发展的今天,这种替换法显得不堪一击。因此研究人员正在试图寻找一种易于编码、但不易于解码的编码规则。 目前比较流行的编码规则称为RSA,是由美国麻省理工学院的三位教授发明的。这种编码规则是基于一种求幂取模算法的:对于给出的三个正整数a,b,c,计算a的b次方除以c的余数。 你的任务是编写一个程序,计算abmod c。Input 输入数据只有一行,依次为三个正整数a,b,c,三个正整数之间各以一个空格隔开,并且1=a,b c =1) if(b%2=1) result=a*result%c; a=a*a%c; b/=2; /通过b/2来求得p(i)为1还是0 return result;二、D: Raising Modulo NumbersTime Limit: 1000 ms Case Time Limit: 1000 ms Memory Limit: 30000 KBSubmit: 24 Accepted: 7 DescriptionPeople are different. Some secretly read magazines full of interesting girls pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODKH. The rules follow: Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions AiBi from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players experience it is possible to increase the difficulty by choosing higher numbers. You should write a program that calculates the result and is able to find out who won the game.InputThe input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 = M = 45000). The sum will be divided by this number. Next line contains number of players H (1 = H = 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.OutputFor each assingnement there is the only one line of output. On this line, there is a number, the result of expression(A1B1+A2B2+ . +AHBH)mod M.Sample Input31642 33 44 55 63612312374859 30293821713 18132Sample Output21319513题意给出A1Ah,B1Bh,M,H,求(A1B1+A2B2+ . +AHBH)mod M.做法快速幂取模+累加步步取模HIT取模相当于在原数中不断减去M直到原数小于M,所以累加后取模和步步取模结果相同,因此在累加过程中取模以防止超过long long界限CODE#include long long z,h,a,b,m,ans,pow;int main() scanf(%I64d,&z); while(z-) scanf(%I64d %I64d,&m,&
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025建筑工程承包合同(A)工程文档范本
- 2025商业办公空间装饰装修管理服务合同
- 2025年黑河辅警协警招聘考试真题及参考答案详解一套
- 2025~2025乡村医生考试题库及答案第500期
- 2025年马鞍山辅警协警招聘考试备考题库及1套完整答案详解
- 2025年绥化辅警协警招聘考试备考题库及一套答案详解
- 《安全员》C证考试题库及答案
- 2025年石嘴山辅警协警招聘考试真题含答案详解(综合题)
- 2025年铜仁辅警协警招聘考试备考题库及一套完整答案详解
- 2025年茂名辅警协警招聘考试备考题库附答案详解(达标题)
- YY/T 1556-2017医用输液、输血、注射器具微粒污染检验方法
- YS/T 261-2011锂辉石精矿
- 2023年高中数学平面向量习题及答案平面向量题库
- 生物医学传感生物传感器课件
- 透析患者左心衰护理个案
- 机械设备安全专项检查表
- 医疗机构住院患者压力性损伤风险评估与报告制度
- Q∕SY 1419-2011 油气管道应变监测规范
- 食源性疾病暴发调查以及案例分析
- 高等学校大学物业管理服务方案
- 学校反恐防暴应急演练记录
评论
0/150
提交评论