Make more efficient power operation - Quick power

  In the process of solving everyday problems, we often encounter find A b similar problems, for this problem, we have the general solution is the most simple method can be solved, it's time complexity is O (b).
1 int power(int a,int b)    //求a的b次方
2 {
3     int i,ans=1;
4     for(i=0;i<b;i++)
5         ans*=a;
6     return ans;
7 }

But for some topics, b when the data becomes large, may time out, this time we need to optimize our power fast procedure.

 

A fast power
fast power from the principles of the weighting coefficient decimal conversion of the binary, for any a decimal number, we turn it into 2 K . 1 +2 K 2 +2 K . 3 + 2 ... + K n- + ... ( k = 0,1,2 ...), followed by binary conversion, e.g., 10 = 2 . 1 + 2 3 = (1010) 2 . Then for A b times b is, it is true. We can split into b 2 K . 1 +2 K 2 +2 K . 3 + 2 ... + K n- + ... (K = 0,1,2 ...), then A b becomes A (2 ^ K . 1 + K ^ 2 2 + 2 ^ K . 3 + ... + K 2 ^ n- + ...) (K = 0,1,2 ...), may then into A 2 ^ K . 1 × AK ^ 2 2 × A 2 ^ K . 3 × ... × A 2 ^ K . 1 . Thus, we put the original time complexity O (b) compressed to O (log 2 B), which is a very considerable improvement.
How to use code to implement it? Here we need to use two-bit arithmetic and & >>, where & is the bitwise AND, we used the code (n & 1) to determine the n binary 1 is not the last one. n >> 1 the role of a binary number is shifted left one, the effect is equivalent to dividing by two, but time is slightly faster than divided by 2.
To sum up, we can get quickly realized the power of code.
1  long  long fastpow ( long  long n long  long multi )
 2  {
 3      long  long years = 1 , basic = n;
4      while (multi)
 5      {
 6          if (multi & 1 ) = age (years * basis );
7          base = ( base * base );
8          multi >> = 1 ;
9      }
 10      return year;
11 }

But not in practical problem solving in many cases several times exponentiation will overflow, so often let us modulo, let's solve this problem.

 

Second, the fast exponentiation modulo
Solving problems quickly powers modulo arithmetic does not need to make too many changes, coupled to the modulo in the multiplication, but note-taking as much as possible to consider a comprehensive, not necessarily because of what it will explode.
code show as below:
 1 long long fastpow(long long n,long long multi,long long mod)
 2 {
 3     long long ans=1,base=n;
 4     while(multi)
 5     {
 6         if(multi&1) ans=(ans*base)%mod;
 7         base=(base*base)%mod;
 8         multi>>=1;
 9     }
 10      return year% change;
11 }

 

Third, the rapid power matrix

(Construction orz)

 

Fourth, the relevant topics
1. Fast power example  Luogu p1226 :
Code:
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 long long mod,n,multi;
 6 
 7 long long fastmulti(long long n,long long multi,long long mod)
 8 {
 9     long long ans=1,base=n;
10     while(multi)
11     {
12         if(multi&1) ans=(ans*base)%mod;
13         base=(base*base)%mod;
14         multi>>=1;
15     }
16     return ans%mod;
17 }
18 
19 int main()
20 {
21     scanf("%lld%lld%lld",&n,&multi,&mod);
22     long long ans;
23     ans=fastmulti(n,multi,mod);
24     printf("%lld^%lld mod %lld=%lld", n, multi, v, ANS);
25      return  0 ;
26 }
Luogu P1226

 

Author : Houge  Date : 2019.6.2

Update log : 

Guess you like

Origin www.cnblogs.com/CSGOBESTGAMEEVER/p/10964209.html