A ^ B mod C's divide and governance Thoughts

Source: Internet
Author: User

A ^ B mod C

Assume 0 <a, B, c <n

1. The original method is to first obtain a ^ B and finally mod C.

However, this method is inefficient, and the time complexity is O (B), and a ^ B must be less than N to avoid overflow. It has great limitations.

 

2. Improvement Method 1:

Assume a> C, there is a ^ B mod c = (a mod c) ^ (B mod C)

This situation is applicable to a> C, but cannot be used when a <C.

In the worst case, a ^ B must be less than N to avoid overflow. This method is not suitable either.

 

Method 2:

We can break down B in a ^ B into (2a + 2B + 2C ...)

For example, 12 ^ 36 = 12 ^ (22 + 25)

12 ^ 36 = 12 ^ 22*12 ^ 25

12 ^ 36 mod 35 = (12 ^ 22 mod 35) * (12 ^ 25 mod 35) mod 35

 

We can know

(12 ^ 21 mod 35)

(12 ^ 22 mod 35)

(12 ^ 23 mod 35)

(12 ^ 24 mod 35)

(12 ^ 25 mod 35)

There is a relationship

(12 ^ 2n mod 35) = (12 ^ 2n-1 mod 35) * 2mod 35

Therefore, the above statements can be obtained in sequence.

 

Final table query (12 ^ 22 mod 35) * (12 ^ 25 mod 35) mod 35

 

However, this method also has a drawback: A * A must be less than N, otherwise it will also cause overflow of results.

Although this method is larger than the preceding two methods, it still cannot meet our requirements.

 

 

Method 3:

Since the threshold for overflow in the previous step is a * A <n.

Then we will try to break down a * A and make the final value of A * a mod C less than N.

 

Here we need to mention a formula a * B mod c = (a mod c) * (B mod C)

 

Assume that in a * a, the first a is X, the second a is Y, and

Y = 2a + 2B + 2C...

So x * Y = x * (2a + 2B + 2C ...)

So x * y MOD c = (x * 2a mod c) + (x * 2B mod C) mod c) + (x * 2C mod C )) moD c ......

 

As we can know

(X ^ 21 mod C)

(X ^ 22 mod C)

(X ^ 23 mod C)

(X ^ 24 mod C)

(X ^ 25 mod C)

The following relationships also exist:

(X ^ 2n mod c) = (x ^ 2n-1 mod c) * 2mod C

Therefore, the above statements can be obtained in sequence.

 

The final value of X * x mod C can also be obtained.

Finally, use method 2 to obtain a ^ B mod C.

 

This method breaks down a again. Therefore, the scope is further expanded.

The range of this method can be obtained when a * 2 <n.

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.