Nyoj modulo of the power of 102

Source: Internet
Author: User

Power modulus time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
Description

Evaluate the remainder of A's power B on C

 

Input
Enter an integer N in the first line to indicate the number of groups of test data (n <100)
Each test group has only one row, with three positive integers A, B, C (1 = <a, B, c <= 1000000000)
Output
Output the result after the B power of a gets the remainder of C.
Sample Input
32 3 53 100 1011 12345 12345
Sample output
3110481

Algorithm analysis:

Big Data issues that need to be exploitedFast modulo Algorithm.


The so-called rapid power is actually the abbreviation of the Rapid power modulo. Simply put, it is to quickly find a power modulo (remainder ). In the process of program design, it is often necessary to find the remainder of a certain number of large numbers. In order to get a Faster Algorithm with a larger computing range, a rapid idempotent algorithm is generated. Calculate the number of a ^ B mod c =. (Result is the result after the remainder is obtained)
Algorithm 1. common algorithms:

int pow( int a, int b ){    int r = 1;    while( b-- )        r *= a;    return r;}result=r%c;

The time complexity of this algorithm is reflected in the while loop, which is O (B). This algorithm has obvious problems. If a and B are too large, it will easily overflow.

Algorithm 2. bipartite

int pow( int a, int b ){    int r = 1, base = a;    while( b != 0 )    {        if( b % 2 )            r *= base;        base *= base;        b /= 2;    }    return r;}result=r%c;

Algorithm 3: Fast Power modulo Algorithm

First, you must understand the formula A ^ B mod c = (a mod c) ^ B mod C (For details, refer to number theory or discrete mathematics)
After learning about this formula, we can first let a get the remainder of C, which can greatly reduce the size of a, so we don't have to think about the improvements. The Code is as follows:

Int POW (int A, int B) {int r = 1; A = A % C; // Add this sentence while (B --) {r * =; // to reduce the value of R and ensure the feasibility of data size, you can change it to r = (R * a) % C ;}result = R % C;

This algorithm has not been improved in terms of time complexity. It is still O (B), but a larger B value is allowed. However, if C is too large, it is very likely that the algorithm times out, we have introduced faster, better, and faster power algorithms. The quick power algorithm depends on the following obvious formula, which I will not prove.




We can see that we have changed the time complexity to O (B/2). Of course, this is a permanent cure. But we can see that when we make k = (A * A) mod C, the status has changed and the final result we require is (k) b/2 mod C instead of the original AB mod C, so we found that this process can be iterated. Of course, there will be one more a mod C for the odd number case, so to complete the iteration, when B is an odd number, we use ans = (ANS * A) % C; to make up for this extra item, then the remaining part can be iterated.
After iteration of the formula above, when B = 0, all the factors are multiplied and the algorithm ends. Therefore, it can be completed in O (log B) time. So we have the final algorithm: quick power algorithm. The Code is as follows:

int Pow(int a, int b, int c) { int r = 1; a = a % c; while(b>0) { if(b % 2 = = 1) r = (r * a) % c;b = b/2; a = (a * a) % c; } return r;}

The code that uses bitwise operations to implement the fast power is as follows: (the power-seeking capability is limited after testing. If there is no intermediate modulo, it will easily overflow)

#include<stdio.h>#include<stdlib.h>_int64 pow( _int64 a, _int64 b );int main(){_int64 a,b,c;int n;scanf("%d",&n);printf("%d\n",100&1);while(n--){scanf("%I64d%I64d%I64d",&a,&b,&c);printf("%I64d\n",pow(a,b)%c);}return 0;}_int64 pow( _int64 a, _int64 b ){    _int64 r = 1, base = a;printf("%I64d  %I64d\n",a,b);    while( b != 0 )    {        if( b & 1 )            r *= base;        base *= base;printf("  %I64d  %I64d\n",r,b);        b >>= 1;    }    return r;}

The code for this question is as follows:

#include<stdio.h>#include<stdlib.h>long long pow(long long a,long long b,long long c);int main(void){long long a,b,c;int n;scanf("%d",&n);while(n--){scanf("%lld%lld%lld",&a,&b,&c);printf("%lld\n",pow(a,b,c));}return 0;}long long pow(long long a,long long b,long long c){int r=1;a=a%c;while(b>0){if(b%2==1)r=(r*a)%c;b=b/2;a=(a*a)%c;}return r;}

The time complexity of this algorithm is O (logb), which can be passed in almost all programming (competition) processes. It is one of the most commonly used algorithms and deserves promotion and learning !!!

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.