1. Number of fast power issues:
The so-called fast power, in fact, is the abbreviation of fast Power modulus, simply speaking, is to quickly find a power mode (residual). In the process of programming, it is often necessary to find some large numbers for the remainder of a number, in order to get a faster, more computational scope of the algorithm, resulting in a fast power-taking modulus algorithm. A fast power is actually a solution to a form like this:an%b . Where A and n can be very large.
The time complexity of the common solution is O (n), and the Fast Power is O (LOGN), the main idea is as follows: N is decomposed into 2, n =a0*0+a1*21...+at*2t-1. It can then be calculated separately, for example: 39=1*20+0*21+0*22+1*23. Note the 2k=2*2k-1, thus reducing the complexity to O (Logn).
Template:
ll Quik_pow (ll A, ll B, ll N) {//A^b%NLL x (a), D (1), while (b > 0) {if (b & 1) d = d*x%n;x = X*x%n;b >>= 1;} return D;}
Quick Power Introduction and its template