Quick Power Introduction and its template

Source: Internet
Author: User

1. Number of fast power issues:

The so-called fast power, in fact, is the abbreviation of fast Power modulus, simply speaking, is to quickly find a power mode (residual). In the process of programming, it is often necessary to find some large numbers for the remainder of a number, in order to get a faster, more computational scope of the algorithm, resulting in a fast power-taking modulus algorithm. A fast power is actually a solution to a form like this:an%b . Where A and n can be very large.

The time complexity of the common solution is O (n), and the Fast Power is O (LOGN), the main idea is as follows: N is decomposed into 2, n =a0*0+a1*21...+at*2t-1. It can then be calculated separately, for example: 39=1*20+0*21+0*22+1*23. Note the 2k=2*2k-1, thus reducing the complexity to O (Logn).

Template:

ll Quik_pow (ll A, ll B, ll N) {//A^b%NLL x (a), D (1), while (b > 0) {if (b & 1) d = d*x%n;x = X*x%n;b >>= 1;} return D;}

  

Quick Power Introduction and its template

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.