Pronađite zadnju znamenku a^b za velike brojeve

Pronađite zadnju znamenku a^b za velike brojeve
Isprobajte na GfG Practice

Dana su vam dva cijela broja s bazom a (broj znamenki d tako da je 1 <= d <= 1000) and the index b (0 <= b <= 922*10^15). You have to find the last digit of a^b.
Primjeri: 
 

 Input : 3 10   
Output : 9

Input : 6 2
Output : 6

Input : 150 53
Output : 0


 


Nakon nekoliko primjera možemo primijetiti donji uzorak.
 

 Number | Last digits that repeat in cycle   
1 | 1
2 | 4 8 6 2
3 | 9 7 1 3
4 | 6 4
5 | 5
6 | 6
7 | 9 3 1 7
8 | 4 2 6 8
9 | 1 9


U datoj tablici možemo vidjeti da je maksimalna duljina ponavljanja ciklusa 4. 
Primjer: 2*2 = 4*2 = 8*2 = 16*2 = 32 zadnja znamenka u 32 je 2 što znači da se nakon množenja 4 puta znamenka ponavlja. Dakle, algoritam je vrlo jednostavan.
izvor: Brilliants.org
Algoritam:
 

  1. Od broj su vrlo veliki pohranjujemo ih kao niz.
  2. Uzmite posljednju znamenku u bazi a.
  3. Sada izračunajte b%4. Ovdje je b vrlo velik. 
    • Ako je b%4==0 to znači da je b potpuno djeljiv s 4 pa će naš eksponent sada biti exp = 4 jer množenjem broja 4 puta dobivamo zadnju znamenku prema tablici ciklusa u gornjem dijagramu.
    • Ako je b%4!=0 to znači da b nije u potpunosti djeljiv s 4 pa će naš eksponent sada biti exp=b%4 jer množenjem eksponenta broja puta dobivamo posljednju znamenku prema tablici ciklusa u gornjem dijagramu.
    • Sada izračunajte ldigit = pow( last_digit_in_base exp).
    • Zadnja znamenka od a^b bit će ldigit%10.


Ispod je implementacija gornjeg algoritma. 
 

C++
   // C++ code to find last digit of a^b   #include          using     namespace     std  ;   // Function to find b % a   int     Modulo  (  int     a       char     b  [])   {      // Initialize result      int     mod     =     0  ;      // calculating mod of b with a to make      // b like 0  <= b  < a      for     (  int     i     =     0  ;     i      <     strlen  (  b  );     i  ++  )      mod     =     (  mod     *     10     +     b  [  i  ]     -     '0'  )     %     a  ;      return     mod  ;     // return modulo   }   // function to find last digit of a^b   int     LastDigit  (  char     a  []     char     b  [])   {      int     len_a     =     strlen  (  a  )     len_b     =     strlen  (  b  );      // if a and b both are 0      if     (  len_a     ==     1     &&     len_b     ==     1     &&     b  [  0  ]     ==     '0'     &&     a  [  0  ]     ==     '0'  )      return     1  ;      // if exponent is 0      if     (  len_b     ==     1     &&     b  [  0  ]     ==     '0'  )      return     1  ;      // if base is 0      if     (  len_a     ==     1     &&     a  [  0  ]     ==     '0'  )      return     0  ;      // if exponent is divisible by 4 that means last      // digit will be pow(a 4) % 10.      // if exponent is not divisible by 4 that means last      // digit will be pow(a b%4) % 10      int     exp     =     (  Modulo  (  4       b  )     ==     0  )     ?     4     :     Modulo  (  4       b  );      // Find last digit in 'a' and compute its exponent      int     res     =     pow  (  a  [  len_a     -     1  ]     -     '0'       exp  );      // Return last digit of result      return     res     %     10  ;   }   // Driver program to run test case   int     main  ()   {      char     a  []     =     '117'       b  []     =     '3'  ;      cout      < <     LastDigit  (  a       b  );      return     0  ;   }   
Java
   // Java code to find last digit of a^b   import     java.io.*  ;   import     java.math.*  ;   class   GFG     {      // Function to find b % a      static     int     Modulo  (  int     a       char     b  []  )      {      // Initialize result      int     mod     =     0  ;      // calculating mod of b with a to make      // b like 0  <= b  < a      for     (  int     i     =     0  ;     i      <     b  .  length  ;     i  ++  )      mod     =     (  mod     *     10     +     b  [  i  ]     -     '0'  )     %     a  ;      return     mod  ;     // return modulo      }      // Function to find last digit of a^b      static     int     LastDigit  (  char     a  []       char     b  []  )      {      int     len_a     =     a  .  length       len_b     =     b  .  length  ;      // if a and b both are 0      if     (  len_a     ==     1     &&     len_b     ==     1     &&     b  [  0  ]     ==     '0'     &&     a  [  0  ]     ==     '0'  )      return     1  ;      // if exponent is 0      if     (  len_b     ==     1     &&     b  [  0  ]     ==     '0'  )      return     1  ;      // if base is 0      if     (  len_a     ==     1     &&     a  [  0  ]     ==     '0'  )      return     0  ;      // if exponent is divisible by 4 that means last      // digit will be pow(a 4) % 10.      // if exponent is not divisible by 4 that means last      // digit will be pow(a b%4) % 10      int     exp     =     (  Modulo  (  4       b  )     ==     0  )     ?     4     :     Modulo  (  4       b  );      // Find last digit in 'a' and compute its exponent      int     res     =     (  int  )(  Math  .  pow  (  a  [  len_a     -     1  ]     -     '0'       exp  ));      // Return last digit of result      return     res     %     10  ;      }      // Driver program to run test case      public     static     void     main  (  String     args  []  )     throws     IOException      {      char     a  []     =     '117'  .  toCharArray  ()     b  []     =     {     '3'     };      System  .  out  .  println  (  LastDigit  (  a       b  ));      }   }   // This code is contributed by Nikita Tiwari.   
Python
   def   last_digit  (  a     b  ):   a   =   int  (  a  )   b   =   int  (  b  )   # if a and b both are 0   if   a   ==   0   and   b   ==   0  :   return   1   # if exponent is 0   if   b   ==   0  :   return   1   # if base is 0   if   a   ==   0  :   return   0   # if exponent is divisible by 4 that means last   # digit will be pow(a 4) % 10.   # if exponent is not divisible by 4 that means last   # digit will be pow(a b%4) % 10   if   b   %   4   ==   0  :   res   =   4   else  :   res   =   b   %   4   # Find last digit in 'a' and compute its exponent   num   =   pow  (  a     res  )   # Return last digit of num   return   num   %   10   a   =   '117'   b   =   '3'   print  (  last_digit  (  a    b  ))   # This code is contributed by Naimish14.   
C#
   // C# code to find last digit of a^b.   using     System  ;   class     GFG     {      // Function to find b % a      static     int     Modulo  (  int     a       char  []     b  )      {          // Initialize result      int     mod     =     0  ;      // calculating mod of b with a      // to make b like 0  <= b  < a      for     (  int     i     =     0  ;     i      <     b  .  Length  ;     i  ++  )      mod     =     (  mod     *     10     +     b  [  i  ]     -     '0'  )     %     a  ;      // return modulo      return     mod  ;      }      // Function to find last digit of a^b      static     int     LastDigit  (  char  []     a       char  []     b  )      {      int     len_a     =     a  .  Length       len_b     =     b  .  Length  ;      // if a and b both are 0      if     (  len_a     ==     1     &&     len_b     ==     1     &&         b  [  0  ]     ==     '0'     &&     a  [  0  ]     ==     '0'  )      return     1  ;      // if exponent is 0      if     (  len_b     ==     1     &&     b  [  0  ]     ==     '0'  )      return     1  ;      // if base is 0      if     (  len_a     ==     1     &&     a  [  0  ]     ==     '0'  )      return     0  ;      // if exponent is divisible by 4      // that means last digit will be       // pow(a 4) % 10. if exponent is      //not divisible by 4 that means last      // digit will be pow(a b%4) % 10      int     exp     =     (  Modulo  (  4       b  )     ==     0  )     ?     4         :     Modulo  (  4       b  );      // Find last digit in 'a' and       // compute its exponent      int     res     =     (  int  )(  Math  .  Pow  (  a  [  len_a     -     1  ]      -     '0'       exp  ));      // Return last digit of result      return     res     %     10  ;      }      // Driver program to run test case      public     static     void     Main  ()      {          char  []     a     =     '117'  .  ToCharArray  ()      b     =     {     '3'     };          Console  .  Write  (  LastDigit  (  a       b  ));      }   }   // This code is contributed by nitin mittal.   
JavaScript
    <  script  >   // Javascript code to find last digit of a^b   // Function to find b % a   function     Modulo  (  a       b  )   {          // Initialize result      let     mod     =     0  ;      // calculating mod of b with a to make      // b like 0  <= b  < a      for     (  let     i     =     0  ;     i      <     b  .  length  ;     i  ++  )      mod     =     (  mod     *     10     +     b  [  i  ]     -     '0'  )     %     a  ;      return     mod  ;     // return modulo   }   // function to find last digit of a^b   function     LastDigit  (  a       b  )   {      let     len_a     =     a  .  length  ;         let     len_b     =     b  .  length  ;      // if a and b both are 0      if     (  len_a     ==     1     &&     len_b     ==     1     &&      b  [  0  ]     ==     '0'     &&     a  [  0  ]     ==     '0'  )      return     1  ;      // if exponent is 0      if     (  len_b     ==     1     &&     b  [  0  ]     ==     '0'  )      return     1  ;      // if base is 0      if     (  len_a     ==     1     &&     a  [  0  ]     ==     '0'  )      return     0  ;      // if exponent is divisible by 4 that      // means last digit will be pow(a 4)      // % 10. if exponent is not divisible      // by 4 that means last digit will be      // pow(a b%4) % 10      exp     =     (  Modulo  (  4       b  )     ==     0  )     ?     4     :      Modulo  (  4       b  );      // Find last digit in 'a' and compute      // its exponent      res     =     Math  .  pow  (  a  [  len_a     -     1  ]     -     '0'       exp  );      // Return last digit of result      return     res     %     10  ;   }   // Driver program to run test case   let     a     =     '117'  ;   let     b     =     '3'  ;   document  .  write  (  LastDigit  (  a       b  ));   // This code is contributed by _saurabh_jaiswal    <  /script>   
PHP
      // php code to find last digit of a^b   // Function to find b % a   function   Modulo  (  $a     $b  )   {   // Initialize result   $mod   =   0  ;   // calculating mod of b with a to make   // b like 0  <= b  < a   for   (  $i   =   0  ;   $i    <   strlen  (  $b  );   $i  ++  )   $mod   =   (  $mod   *   10   +   $b  [  $i  ]   -   '0'  )   %   $a  ;   return   $mod  ;   // return modulo   }   // function to find last digit of a^b   function   LastDigit  (  $a     $b  )   {   $len_a   =   strlen  (  $a  );   $len_b   =   strlen  (  $b  );   // if a and b both are 0   if   (  $len_a   ==   1   &&   $len_b   ==   1   &&   $b  [  0  ]   ==   '0'   &&   $a  [  0  ]   ==   '0'  )   return   1  ;   // if exponent is 0   if   (  $len_b   ==   1   &&   $b  [  0  ]   ==   '0'  )   return   1  ;   // if base is 0   if   (  $len_a   ==   1   &&   $a  [  0  ]   ==   '0'  )   return   0  ;   // if exponent is divisible by 4 that   // means last digit will be pow(a 4)   // % 10. if exponent is not divisible    // by 4 that means last digit will be   // pow(a b%4) % 10   $exp   =   (  Modulo  (  4     $b  )   ==   0  )   ?   4   :   Modulo  (  4     $b  );   // Find last digit in 'a' and compute   // its exponent   $res   =   pow  (  $a  [  $len_a   -   1  ]   -   '0'     $exp  );   // Return last digit of result   return   $res   %   10  ;   }   // Driver program to run test case   $a   =   '117'  ;   $b   =   '3'  ;   echo   LastDigit  (  $a     $b  );   // This code is contributed by nitin mittal.   ?>   

Izlaz: 

 3  


Ovaj je članak pregledao tim geeksforgeeks.