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
#include
_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
#include
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 !!!