HDU 1061 rightmost digit (quick power remainder)

Source: Internet
Author: User

Generally, obtaining the remainder of 10 from the power will time out, and the quick power is used.

# Include <iostream> # include <cstdio> using namespace STD; int mod_exp (int A, int B, int C) // Rapid power remainder a ^ B % c {int res, T; Res = 1% C; t = A % C; while (B) {If (B & 1) {res = res * T % C;} t = T * T % C; B >>= 1;} return res;} int main () {int T; cin> T; while (t --) {int N; CIN> N; cout <mod_exp (N, N, 10) <Endl ;} system ("pause"); Return 0 ;}

The following is an introduction to the quick power:

First, paste the principle of the qinjiu algorithm (Horner algorithm:

Sub-function with item

Extract the previous item from the public factor.

Then, extract the first item in the brackets from the common factor.

Extracting the common factor repeatedlyFinally, convert the function

Ling

......

That is, the request.

The following describes the power of quick bi: (thanks to the author)

Fast modulo Algorithm

I have not found a detailed description and explanation of the quick power algorithm on the website. Here, I provide a complete explanation of the quick power algorithm, using the C language, readers of different languages have to change their positions. After all, many people read c ~

The so-called rapid power is actually the abbreviation of the Rapid power modulo. Simply put, it is to quickly find a power modulo (remainder ). In the process of program design, it is often necessary to find the remainder of a certain number of large numbers. In order to get a Faster Algorithm with a larger computing range, a rapid idempotent algorithm is generated. [Some readers are a bit vague when talking about the "quick power", so I have modified this article and added it in more detail, so that more readers can see it at a glance]

Let's start with a simple example: a ^ B % C =?

Algorithm 1. First, design the algorithm directly:

Int ans = 1;

For (INT I = 1; I <= B; I ++)

{

Ans = ans *;

}

Ans = ans % C;

The time complexity of this algorithm is reflected in the for loop, which is O (B). This algorithm has obvious problems. If a and B are too large, it will easily overflow.

So let's take a look at the first Improvement Solution: before talking about this solution, we should first have a formula: A ^ B % C = (a % C) ^ B % C. this formula should have been learned in discrete mathematics or number theory. However, to facilitate reading, we still provide proof:

Theorem 1: A ^ B % C = (a % C) ^ B % C

 

The formula above is the theorem of the formula below, that is, the remainder of the product is equal to the remainder of the product.

 

After the above formula is proved, we can let a get the remainder of C first, which can greatly reduce the size of,

Therefore, you do not have to think about improvements:

Algorithm 2:

Int ans = 1;

A = A % C; // Add this sentence

For (INT I = 1; I <= B; I ++)

{

Ans = ans *;

}

Ans = ans % C;

Smart readers should be able to think that since a factor is multiplied by the remainder and then the remainder remains unchanged, the newly calculated ans can also perform the remainder, so they can get a better version.

Algorithm 3:

Int ans = 1;

A = A % C; // Add this sentence

For (INT I = 1; I <= B; I ++)

{

Ans = (ANS * A) % C; // the remainder is obtained here.

 

}

Ans = ans % C;

This algorithm has not been improved in terms of time complexity. It is still O (B), but it is much better. However, it is likely to time out when C is too large, we have introduced the following fast power algorithms.

The quick power algorithm depends on the following obvious formula, which I will not prove.

 

Then we can get the following algorithm:

Algorithm 4:

Int ans = 1;

A = A % C;

If (B % 2 = 1)

Ans = (ANS * A) mod C; // if it is an odd number, you need to take one more step and calculate it to ans in advance.

K = (A * A) % C; // We take A2 instead of

For (INT I = 1; I <= B/2; I ++)

{

Ans = (ANS * k) % C;

}

Ans = ans % C;

 

We can see that we have changed the time complexity to O (B/2). Of course, this is a permanent cure. But we can see that when we make k = (A * A) mod C, the status has changed and the final result we require is (k) b/2 mod C instead of the original AB mod C, so we found that this process can be iterated. Of course, there will be an additional a mod C for an odd number of cases, so to complete the iteration, when B is an odd number, we use

Ans = (ANS * A) % C; to make up for this extra item, the remaining part can be iterated.

 

After iteration of the formula above, when B = 0, all the factors are multiplied and the algorithm ends. Therefore, it can be completed in O (log B) time. So we have the final algorithm: quick power algorithm.

Algorithm 5: Fast Power Algorithm

 

Int ans = 1;

A = A % C;

While (B> 0)

{

 

If (B % 2 = 1)

Ans = (ANS * A) % C;

B = B/2;

A = (A * A) % C;

}

Structure the code above, that is, write it as a function:

Int powermod (int A, int B, int C)

{

Int ans = 1;

A = A % C;

While (B> 0)

{

 

If (B % 2 = 1)

Ans = (ANS * A) % C;

B = B/2;

A = (A * A) % C;

}

Return ans;

}

The time complexity of this algorithm is O (logb), which can be passed in almost all programming (competition) processes. It is one of the most common algorithms currently.

The following content is for reference only:

Extension: There is a derivation of the fast power algorithm. You can also think about it from another perspective.

=? To solve this problem, we can also consider the hexadecimal conversion:

Convert hexadecimal B to a binary expression:

Note that the value here is either 0 or 1. If one item is entered, this item is 1, which corresponds to the case where B is an even number in the Process of the above algorithm, 1 corresponds to the case where B is an odd number. [do not reverse it. For your own analysis, contact the 10-to-2-to-2-to-do method.] We will multiply it in sequence. For the calculation of each item, the remainder of the result of the previous item is used to calculate the result of the next item. For the required result, ANS do not need to multiply it. [because the value of this item is 1], if it is one, it is multiplied by this item and then the remainder is obtained. This algorithm is essentially the same as the above algorithm. Readers can analyze it by themselves. I will not talk about it here. I hope this article will help readers master the knowledge points of the quick power algorithm. Of course, if you really need to master it, you won't be able to practice much.

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.