To solve the power of a in a fast solution by two-point exponentiation

Source: Internet
Author: User

A Primer

How to find the B power of a, it is not simple, a for loop can be achieved!

void Main (void) {    int  A, b;     int 1 ;     >> a >> b;      for (int1; I <= b; i++)    {        *= A;    }     << ans;}

So how to quickly find A's power of B? Can the above code also be optimized?

Of course it's OK! The following describes a method-two to power.

Two-point exponentiation

The so-called two-point exponentiation, that is, the power of B is represented by a binary system, when the bits K-bit is 1 o'clock, it is necessary to multiply a 2^k the second side.

The above code is optimized below:

voidMainvoid){    intA, B; intAns =1; CIN>> a >>b;  while(b! =0)    {        //when the bits K bit is 1 o'clock, you need to multiply the 2^k of a, then save with ans        if(b%2==1) {ans*=A; } A*=A; //divided by 2 each time, converted into bits.b/=2; } cout<<ans;}

For example, when B = 5 o'clock, the binary of B is 101.

Number of Cycles Bits Ans Value A value B Value
0 101 1 A 5
1 Ten1 2 A2 2
2 101 2 A4 1
3 101 32 A16 0

From the above table we can see intuitively 5 power can be converted to (2^0+2^2), that is, a 5 power to a (2^0+2^2) power, that is, a 2^0 and a 2^2 product.

One more example-consolidation

This problem comes from the nine-degree tutorial "People see people love a^b."

Title Description:

An integer representing the last three digits of the a^b.

Input:

The input data contains multiple test instances, one row per instance, consisting of two positive integers a and B, where (1<=a,b<=10000), if a = 0, is = 0, indicates that the input data is closed and not processed.

Output:

For each test instance, the last three bits of the output a^b represent the integers, with each output occupying one row.

Sample input:

2 3

12 6

6789 10000

0 0

Sample output:

8

984

1

For this problem, it is possible to use the above two-part solution method, you can imitate the above code to implement. One thing to note, however, is that we will not be able to define an integer variable or a long long variable to hold the result of the data A = 6789,b=10000,a^b given by the sample. Because the numbers are too large, they cannot be represented in the integer range. So we can save only the last three bits of each calculation result, because the last three bits of the final result depend on the last three bits of their median value.

OK, here's the code:

voidMainvoid){    intA, B;  while(Cin >> a >>b)) {intAns =1; if(A = =0&& b = =0)             Break;  while(b! =0)        {            //when bits k bit is 1 o'clock, the 2^k of a is accumulated.            if(b%2==1) {ans*=A; Ans%= +; } A*=A; A%= +; //divided by 2 each time, converted into bits.b/=2; } cout<<ans; }    }

Two-point exponentiation, quickly solve A's power of B

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.