A ^ B % C

Source: Internet
Author: User

When we mention a ^ B % C, we naturally think of the modulo repeating square hair. A solution can be obtained within the time complexity of log (n.

However, in some cases, it cannot be calculated as follows:

Case 1: C is very large (C> = 2 ^ 32), although it can be within the range of _ int64, however, if two such square numbers will definitely exceed the _ int64 representation range, we cannot simply multiply them. There are two ways to do this:

Method 1: If you can divide the number into several reciprocal prime numbers (using prime numbers) and each number is smaller than 2 ^ 32, you can calculate a ^ B % (This number) for each number) then, we establish a dry homogeneous equation and use the Chinese Remainder Theorem to solve the final answer.

I have a big question.

Method 2: For C smaller than 2 ^ 63, we can use a method similar to the modulo repeated flat method for multiplication. Convert a number to binary. The multiplication of another number and this number is equivalent to multiplying each digit of each binary number and then adding it together.

Unsigned long MUL (unsigned long X, unsigned long y, unsigned long Z)
{
Unsigned long ANW, temp;
X = x % Z; y = Y % Z;
If (x <Y) {temp = x, x = Y, y = temp ;}
ANW = 0;
While (y)
{
If (Y % 2 = 1) ANW = (ANW + x) % Z;
X = (x * 2) % Z;
Y = y/2;
}
Return ANW;
}

Related Questions: 1752 acm.fzu.edu.cn/problem.php of OJ of Fuzhou University

Scenario 2: When B is very large and expressed as a string, you can use the Euler's function, a ^ Euler's function = 1 Mod (M), where a and m are mutually unique. Narrow B to the Euler's function of C. (If a and m need to be mutually exclusive, I feel that I can use the Chinese Remainder Theorem to divide m into two parts, one is the mutual element, and the other is the biggest factor between A and M)

Related Questions: Fuzhou University oj1759 acm.fzu.edu.cn/problem.php

# Include "stdio. H"
# Include "string. H"

Int prime [10000], N;
Void table ()
{
Int I, J;
Bool hash [31625];
Memset (hash, 0, sizeof (hash ));
For (I = 2; I <179; I ++)
If (hash [I] = 0)
For (j = I * I; j <31624; j = J + I)
Hash [J] = 1;
For (I = 2; I <31624; I ++)
If (hash [I] = 0)
Prime [n ++] = I;
}
Int Euler (INT m)
{
Int ANW = m, p [35];
For (I = 0, j = 0; I <n & n! = 1; I ++)
If (a % prime [I] = 0)
{
P [J] = prime [I];
While (N % prime [I] = 0)
N = N/prime [I];
J ++;
}
If (n! = 1)
{
P [J] =;
J ++;
}
For (I = 0; I <j; I ++)
{
ANW = ANW/P [I] * (p [I]-1 );
}
Return ANW;
}
Char ch [1000005];

Int Mod (INT m)
{
Int I, J, K, temp = 0;

For (I = 0; ch [I]! = 0; I ++)
{
Temp = temp * 10 + CH [I]-'0 ';
If (temp> m)
Temp = TEMP % m;
}
}

 

 

Additional scenario 1: A ^ B % C when B = b1 * B2 * B3 * B4 * B5 * b6...

A ^ B = (a ^ B1) ^ B2) ^ B3) ^ B4) ^ B5)

 

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.