Fast power-take modulus algorithm

Source: Internet
Author: User
Tags modulus

The encryption method of RSA public key is a^b%c; when a/b value is large, it is difficult to evaluate directly, so there are the following algorithms:

1: Use the formula (A*B)%c= ((a%c) *b)%c to process each step, as the formula shows ... This algorithm only handles the problem that the data is too big, the time is still not solved.

The code is as follows:

void Mmode () {    int temp=b;     Long long res=1;      while (temp--)    {        res=res*a%c;    }    cout<<res<<Endl;}

2: Fast power modulus algorithm, using the binary method optimization, will be replaced each time res*a res*a^2 optimization time complexity of O (LOGN)

The code is as follows:

 void   X_mmode () { long  long  res=1  ;  long  long  temp=a;  while  (B!=0   2  ==1  ) res  =res*temp%c;        b /=2   =temp*temp%C; } cout  <<res<<endl;}  

The above is the study of the power-taking modulus algorithm

2016.4.19

Fast power-take modulus algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.