Result |
Time Limit |
Memory limit |
Run times |
AC times |
Judge |
|
3 S |
8192 K |
1756 |
336 |
Standard |
Calculate
For large valuesB,P, AndMUsing an efficient algorithm. (That's right, this problem has a time dependency !!!.)
Input
Three integer values (in the orderB,P,M) Will be read one number per line.BAndPAre integers in the range 0 to 2147483647 random Sive.MIs an integer in the range 1 to 46340 random Sive.
Output
The result of the computation. A single integer.
Sample Input
3181321717176532374859302938236123
Sample output
13213195/*
*/
- # Include <stdio. h>
- # Include <memory>
- Int main ()
- {
- Int A, B, N;
- While (scanf ("% d", & A, & B, & N )! = EOF)
- {
- Bool B2 [35];
- Memset (B2, 0, sizeof (B2 ));
- Int TMP = B, I, D = 1;
- For (I = 0; TMP> 0; I ++)
- {
- B2 [I] = TMP % 2;
- TMP = tmp/2;
- }
- For (; I> = 0; I --)
- {
- D = D * D % N;
- Int AA = A % N;
- If (B2 [I] = 1)
- D = D * Aa % N;
- }
- Printf ("% d/N", d );
- }
- Return 0;
- }
Method 2: ultra-short code
# Include <cstdio>
Int B, P, M, K;
Int main ()
{
While (scanf ("% d", & B, & P, & M )! = EOF)
{
For (k = 1, B = B % m; P/= 2, B = B * B % m)
If (P % 2) K = K * B % m;
Printf ("% d \ n", K % m );
}
Return 0;
}
Method 3: lower recursion time
# Include <cstdio>
Int m;
Int bigmod (int A, int B)
{
If (! B) return 1;
Int d = bigmod (a, B> 1) % m;
Return B & 1? (D * D % M * A) % m :( D * D) % m;
}
Int main ()
{
Int A, B;
While (scanf ("% d", & A, & B, & M )! = EOF)
{
Int ans = bigmod (a % m, B) % m;
Printf ("% d \ n", ANS );
}
Return 0;
}
The shortest code for rapid modulo operation (k = 1, A = A % m; B; B >>= 1, A = A * A % m) if (B & 1) k = K * A % m; find a ^ B (mod m) K % m as the answer.