The principle of the same power and the implementation of C ++ are provided with an algorithm for converting a 10-digit number into an arbitrary array.

Source: Internet
Author: User

At the end of the 18th century, Gauss defined the so-called concept of coequal. This thing is almost everywhere in discrete mathematics and has many functions. In particular, many encryption algorithms are useful now. The same power is also based on a small knowledge of the same remainder, mainly because it is easier to calculate the power of a very large integer and then evaluate the modulo, so it will be used accidentally. So today, I wrote a function to facilitate future use. At the same time, to perform a quick computing with the same power, the binary expansion of the decimal number must be used, by the way, I wrote a function that can expand the 10-digit data by any base. Of course, the returned result is a vector instead of an array.

 

Here, we will start with a brief introduction from Tongyu:

  A between B (mod m) iff (if and only if) a mod m = B mod m. A branch B (mod m) as follows: a module M is the same as B.Here, MOD indicates the modulo operation. For example, 3 mod 2 is the remainder 1 of 3/2.

In terms of distance, for example, 17 ≡ 5 (mod 6) and 17 mod 6 are the same as 5. Because 17/6 is more than 5, and 5/6 is more than 5.

  

The operation of the same cool relation and addition multiplication is still homogeneous, which is actually a + cBytesB + d (mod m), and a * CBytesB * D (mod m );

 

The following describes the same power:

We often need to quickly find bn mod m, where B n m is a relatively large integer, such as B = 12345 n = 5567, so directly calculating is obviously not feasible, so we expand N in binary (that is, convert it to binary). For example, this n is changed to 1010110111111, so we only need to require B mod m, B2 mod m ,... b2 ^ (k-1) mod m, then multiply the binary at the corresponding position by 1. After each multiplication, calculate the remainder of M.

 

Int modularexponentiation (INT base, int exp_in_bin, int modular); <br/> vector <short> basebexpansion (INT num, short B ); </P> <p> int main () {<br/> // test. The hexadecimal expansion of 2413 is 96d. <br/> // The display is not modified, so it is displayed as 9613 <br/> vector <short> A = basebexpansion (2413, 16 ); <br/> // use the iterator to display the result <br/> for (vector <short>: iterator it =. begin (); it! =. End (); It ++) {<br/> cout <* it; <br/>}</P> <p> cout <Endl; <br/> // test data. The result of 981 ^ 937 mod 2537 should be 704 <br/> int x = modularexponentiation (981,937,253 7 ); <br/> cout <x <Endl; <br/>}</P> <p>/** <br/> * calculate the same power value of base ^ exp mod modular. <Br/> * base: base number <br/> * exp_in_bin: index <br/> * Modular: module <br/> * return: same Power <br/> */<br/> int modularexponentiation (INT base, int exp, int modular) {<br/> // expand exp in binary format <br/> vector <short> N = basebexpansion (exp, 2); <br/> int x = 1; // X = base ^ exp mod modular; </P> <p> int power = base % modular; <br/> for (INT I = n. size ()-1; I>-1; I --) {<br/> If (N [I]) {// if n [I] = I <br/> // start from the rightmost end after the binary is expanded. <br/> X = (x * power) % modular; <br/>}< br/> // evaluate B ^ (2 ^ (k-1 )) mod m value <br/> power = (Power * power) % modular; <br/>}</P> <p> return X; <br/>}</P> <p>/** <br/> * calculate the B-base expanded array of numeric num <br/> * num: number to be expanded <br/> * B: base for number expansion <br/> * return: vector after number expansion, stored in the order from left to right, for example, if the binary expansion of 13 is 1101, the storage order is {,} <br/> */<br/> vector <short> basebexpansion (INT num, short B) {<br/> int q = num, I = 0, temp = 0; <br/> vector <short> A; <br/> whi Le (Q! = 0) {<br/>. push_back (Q % B); <br/> q/= B; <br/>}</P> <p> // reverse a <br/> int size =. size (); <br/> for (; I <size/2; I ++) {<br/> temp = A [I]; <br/> A [I] = A [size-1-i]; <br/> A [size-1-i] = temp; <br/>}< br/> return; <br/>}

 

In fact, because this algorithm replaces the N power of B with the two power forms of a certain number, the complexity of computing is greatly reduced, for example, for an index like 12345678987654321, its binary format is 101011110111000101010001100010100100011111010010110001, so the maximum number to be calculated is only 9007199254740992*9007199254740992 = 81129638414606681695789005144064 (in fact, the number of this calculation depends on the value of modular. When modular is not so large, it is obviously impossible to calculate such a large number). Instead, it is necessary to calculate 12345678987654321 power for a B, real-time B is 2. After the power of 5 digits is exceeded, it is almost impossible to use the double type of C ++.

Related Article

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.