




已阅读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私人小额借款合同模板
- 海南生态植物墙施工方案
- 外墙装饰马赛克施工方案
- 锁扣式钢管桩施工方案
- 聊城光伏施工方案设计
- 2025年租赁合同模板
- 加装闸阀施工方案怎么写
- 河北足球场围栏施工方案
- 2025雇佣合同协议书范本
- 忻州家装电地暖施工方案
- cma资料培训课件
- 专利代理所管理制度
- 农村道路交通宣传课件
- 2025年戏剧与影视学专业考研试题及答案
- DZ/T 0275.5-2015岩矿鉴定技术规范第5部分:矿石光片鉴定
- 2025年新教材道德与法治三年级上册第二单元《学科学爱科学》教案设计
- 陕煤集团运销合同协议
- 行政管理毕业论文-我国地方政府行政机构改革问题研究
- 2025年时政真题面试题及答案
- AI基础知识培训
- GB/T 18936-2025禽流感诊断技术
评论
0/150
提交评论