1. Use bitwise multiplication.
After a multiplier is converted to a binary system, the bitwise operation is used to multiply the multiplier.
- /*
- * Input: positive integer k and positive integer m
- * Output: k * m
- */
- _ Int 64 km (_ int64 k, _ int64 m ){
- _ Int64 x = k;
- Int W = (INT) floor (log (m)/log (2)-1;
- _ Int64 E = 1 <W;
- For (INT I = 0; I <= W; I ++ ){
- X <= 1;
- If (M & E)
- X + = K;
- E >>= 1;
- }
- Return X;
- }
/* <Br/> * input: positive integer k and positive integer m <br/> * output: k * m <br/> */<br/>__ int 64 km (_ int64 k, _ int64 m) {<br/> _ int64 x = k; <br/> int w = (int) floor (log (m)/log (2)-1; <br/> _ int64 e = 1 <w; <br/> for (int I = 0; I <= w; I ++) {<br/> x <= 1; <br/> if (m & e) <br/> x + = k; <br/> e >>= 1; <br/>}< br/> return x; <br/>}
2. Multiplication using bits
After the exponent is converted to a binary value, the bitwise operation is used to complete the multiplication.
Pseudocode:
C ++ implementation
[Cpp]
View plaincopyprint?
- /*
- * Input: positive integers v mod m and g mod m
- * Output: g ^ v mod m
- */
- _ Int64 gvmm (_ int64 g, _ int64 v, _ int64 m ){
- Int w = (int) floor (log (v)/log (2)-1;
- _ Int64 E = 1 <W;
- _ Int64 x = g;
- For (INT I = 0; I <= W; I ++ ){
- X = (x * X) % m;
- If (V & E ){
- X = (G * X) % m;
- }
- E >>= 1;
- }
- Return X;
- }
/* <Br/> * input: positive integer v mod m and G mod m <br/> * output: G ^ V mod m <br/> */<br/>__ int64 gvmm (_ int64 g, _ int64 V, _ int64 m) {<br/> int W = (INT) floor (log (v)/log (2)-1; <br/> _ int64 E = 1 <W; <br/> _ int64 x = g; <br/> for (INT I = 0; I <= W; I ++) {<br/> X = (x * X) % m; <br/> If (V & E) {<br/> X = (G * X) % m; <br/>}< br/> E >>= 1; <br/>}< br/> return X; <br/>}
Test of the multiplication party:
Use common algorithms and bitwise algorithms for comparison.
[Cpp]
View plaincopyprint?
- # Include <iostream>
- # Include <math. h>
- # Include <time. h>
- Using namespace STD;
- /*
- * Input: positive integers v mod m and G mod m
- * Output: g ^ v mod m
- */
- _ Int64 gvmm (_ int64 g, _ int64 v, _ int64 m ){
- Int w = (int) floor (log (v)/log (2)-1;
- _ Int64 e = 1 <w;
- _ Int64 x = g;
- For (int I = 0; I <= w; I ++ ){
- X = (x * x) % m;
- If (v & e ){
- X = (g * x) % m;
- }
- E >>= 1;
- }
- Return x;
- }
- /*
- * Common algorithms for verification results
- */
- Int yanzheng (_ int64 g, _ int64 v, _ int64 m ){
- _ Int64 x = 1;
- For (int I = 0; I <v; I ++ ){
- X * = g;
- X % = m;
- }
- Return x;
- }
- Int main (){
- Clock_t begin = clock ();
- Cout <yanzheng (23229,1892123, 23894) <endl;
- Clock_t end = clock ();
- Double cost = (double) (end-begin)/CLOCKS_PER_SEC;
- Printf ("putong: % lf seconds \ n", cost );
- Begin = clock ();
- Cout <gvmm (23229,1892123, 23894) <endl;
- End = clock ();
- Cost = (double) (end-begin)/CLOCKS_PER_SEC;
- Printf ("weiyunsuan: % lf seconds \ n", cost );
- System ("pause ");
- }
# Include <iostream> <br/> # include <math. h> <br/> # include <time. h> <br/> using namespace STD; </P> <p>/* <br/> * input: positive Integers v mod m and G mod m <br/> * output: G ^ V mod m <br/> */<br/>__ int64 gvmm (_ int64 G, _ int64 V, _ int64 m) {<br/> int W = (INT) floor (log (v)/log (2)-1; <br/> _ int64 E = 1 <W; <br/> _ int64 x = g; <br/> for (INT I = 0; I <= W; I ++) {<br/> X = (x * X) % m; <br/> If (V & E) {<br/> X = (G * X) % m; <br/>}< br/> E >>= 1; <br/>}< br/> return X; <br/>}< br/>/* <br/> * common algorithm of the verification result <br/> */<br/> int yanzheng (_ int64 G, _ int64 V, _ int64 m) {<br/> _ int64 x = 1; <br/> for (INT I = 0; I <V; I ++) {<br/> X * = g; <br/> X % = m; <br/>}< br/> return X; <br/>}</P> <p> int main () {<br/> clock_t begin = clock (); <br/> cout <yanzheng (23229,1892123, 23894) <Endl; <br/> clock_t end = clock (); <br/> double cost = (double) (end-begin)/clocks_per_sec; <br/> printf ("Putong: % lf seconds \ n", cost); <br/> begin = clock (); <br/> cout <gvmm (23229,1892123, 23894) <Endl; <br/> end = clock (); <br/> Cost = (double) (end-begin)/clocks_per_sec; <br/> printf ("weiyunsuan: % lf seconds \ n", cost); <br/> system ("pause"); <br/>}< br/>
According to the slightly complex multiplication operations given in the program, the efficiency is (after multiple measurements, it is almost the same order of magnitude)
21963
Putong: 0.071000 seconds
21963
Weiyunsuan: 0.001000 seconds
A common algorithm is 50-70 times slower than a bit algorithm.