Winter training - fast power modulo

Each topic has a hyperlink, click to jump to the title screen! ! !

Fast power modulo

1. The number of higher priority than the priority%, so that (A B) with m% A B m% identical.
2. (A
A A A)% m = (2 A ^ A ^ 2)% m = (A ^ 2 m% A% m ^ 2)% m = (m A% A% m A m% A% m )% m;
3. As long as fast modulo exponentiation, it is simply added to each of the% mod, and later combined.
% M% m = 4.a A% m;
5. The (A + B)% m = (A + B% m% m)% m
6. The (A
B)% m = (m * A% B% m )% m

Here Insert Picture Description
Here Insert Picture Description

Math Kut

Several write will find a [n] = 2 * pow (3, n), and then substituted into a template to quickly modulo exponentiation.
Here Insert Picture Description
If the relationship is simply done in accordance with the time-out will be subject
Here Insert Picture Description

The number of XOR Equations

First transpose: a = x + (a ^ x), and then look at a certain bit binary number, the discussion can be found Law:
A X + (X ^ A)
. 1 0+ (. 1 ^ 0) =. 1
. 1 1+ (. 1 ^ 1) = 1
0 0 + (0 ^ 0) = 0
0 1+ (1 ^ 0) = 10
can be found, a fourth case (a = 0, X = 1) generates a carry change, a change of values, rule out the fourth case.
Integrated third, four cases can be found, if a certain bit is 0, then, it is only the corresponding bit x 0.
A first look, the second case, we have found that, when a certain bit is 1, the corresponding bit of x may be 1, and may be 0.
Therefore, we only need a binary number is obtained, there can be a number, the answer is 2 ^ n

Notes:
1. The full binary adder 2 is a feed, such as 100110 + 110010 = 1011000;

2. a per once divided by 2, the last bit of its binary will disappear;
3.a = a << 1 corresponds to a multiplied by 2, multiply faster than the left.

This problem not fast exponentiation modulo
Here Insert Picture Description
NOTE:
POW (a, b) function of a and b can be the number of type long long, but it returns an int, the% lld for output will be random values, it should be pow (a, b) be cast, and then output.
printf ( "% lld \ n" , (long long int) pow (2, num1));

Here Insert Picture Description

Published 11 original articles · won praise 12 · views 555

Guess you like

Origin blog.csdn.net/SSnTi/article/details/104259763