[Modulo operation] and [modulo operation]

Source: Internet
Author: User

In many cases, modulo operations are used. Here we describe some pattern operations and prove them. The practical application of these theories will be recorded and explained in the future.

1. The modulo operation is a remainder operation (recorded as % or mod), which has the characteristics of periodicity. M % N indicates the remainder of N after division of M. When M increases, M % N is cyclical, and the larger N, the longer the cycle, and the period is equal to n.
For example
0% 20 = 2% 1% 20 = 1, 3% 20 = 2, 19% 20 = 3,..., 20 = 19
20% 20 = 39% 21% 20 = 22% 20 = 23% 20 = 3,..., 20 = 19
2. If M % N = R, the following equation can be introduced:
M = K * n + R (K is an integer greater than or equal to 0, r <= m)
3. In the same remainder formula, positive integers A and B take the modulo of n. the remainder of them are the same. Remember to perform a ≡ B mod n or A = B (mod N ).
According to the equation 2, A = KN + B or a-B = KN can be introduced.
Proof: required a = K1 * n + r1
B = k2 * n + R2
Required a-B = (K1-K2) * n + (R1-R2)
A = K * n + (R1-R2) + B
Modulus A and B are equal to N, R1 = R2
Required a = K * n + B (k = K1-K2)
4. modulo operation rules. The modulo operation is similar to the basic four arithmetic operations, except division. The rules are as follows:
(A + B) % N = (a % N + B % N) % N (1)
(A-B) % N = (a % N-B % N) % N (2)
(A * B) % N = (a % N * B % N) % N (3)
AB % N = (a % N) B) % N (4)

Certification: http://hi.baidu.com/ckh%5F0330/blog/item/bb2c4d8873df60ba0e24441e.html

Modulo a ^ B mod C:

Insert a topic directly:

1. When A, B, and C are relatively small, they can use naked violence.
Pseudocode:
V: = 1;
For I: = 1 to B begin
V: = V *;
V: = V mod C;
End

This is the first time we can think of this problem.

2. When A, B, and C are both relatively large
Here we need to consider the size of C. Suppose C * C <2 ^ 64 (if it is larger, we can only perform special processing)
If B is relatively large, the method of 1 is obviously time-consuming, so we can use the so-called binary method.
B = b0 + B1 * 2 ^ 1 + B2 * 2 ^ 2 +... + BN * 2 ^ n
Obviously, a ^ B can be the product of several items.
Pseudocode:
V: = 1;
While B <> 0 do begin
If (B and 1 = 1) Do begin V: = V * A; V: = V mod C; End
A: = A *;
A: = a mod C;
B: = B SHR 1;
End
3. if C is large, we need to use the simulated multiplication to avoid overflow. Of course, if C is already large to 10 ^ 100, we can only use high precision .. only a maximum of c = 2 ^ 64-1 can be processed here.

Pseudocode:
Int MUL (int A, int B, int C)
{
Int ret = 0, TMP = A % C;
While (B)
{
If (B & 0x1) if (Ret + = TMP)> = C) ret-= C;
If (TMP <= 1)> = C) TMP-= C;
B> = 1;
}
Return ret;
}

4. If B has exploded, the logb algorithm is obviously insufficient.
So we can consider using the method of loop section to solve the problem.
If C is small, you can directly obtain the loop section by brute force.
For a ^ B mod C

A. If gcd (a, c) = 1
The starting position of the cycle section must be 1, and the cycle length is a factor of its Euler's function.
Because a ^ P mod c = 1
So P can be viewed as a period.
B. If gcd (a, c )! = 1 does the circular section not start from 1? The answer is no.
Why?
Because assume there is an l'
A mod c = a ^ (1 + l') mod c = A * a ^ l' mod C
Then we can find many B 'that meet the condition, so that a * B' = a mod C (mod C)
The number of solutions is gcd (a, c), and if a ^ L' = B '(mod c) can be found)
The starting position can still be 1.
Of course, in most cases, the starting position of the cyclic section is not 1.

Now we can use brute force to get the starting position of the loop section SPOs and the cycle length Len.

The following formula is used:
You can solve the problem in a relatively fast period.

What if C is very big?

You can use the drawer principle + baby-step, giant-step extension to get the length of the loop section. I haven't thought of a better algorithm for the starting position of the loop section, for the time being, it is only violent-_-. If you have any ideas, please leave a message. Thank you ~

PS: The ACM competition often uses modulo operations and Modulo-wise power operations. High-Performance algorithms must also take into account the scope of processing.

From: http://hi.baidu.com/aekdycoin
Http://hi.baidu.com/ckh%5F0330

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/akof1314/archive/2009/07/29/4391119.aspx

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.