查找a^b的最后一位表示大数

给你两个整数,基数a(数字d的数量,1<=d<=1000)和索引b(0<=b<=922*10^15)。你必须找到a^b的最后一位。 例如:

null
Input  : 3 10Output : 9Input  : 6 2Output : 6Input  : 150 53Output : 0

举几个例子之后,我们可以注意到下面的模式。

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

在给定的表格中,我们可以看到循环重复的最大长度为4。 例子: 2*2=4*2=8*2=16*2=32,32中的最后一个数字是2,这意味着在乘以4次数字后,重复它自己。所以算法非常简单。 资料来源: 非常棒。组织 算法:

  1. 由于数字非常大,我们将它们存储为字符串。
  2. 取a底的最后一位数字。
  3. 现在计算b%4。这里b很大。
    • 如果b%4==0,这意味着b完全可以被4整除,所以我们的指数现在将是exp=4,因为通过将数字乘以4倍,我们可以根据上图中的循环表得到最后一位数字。
    • 如果b%4=这意味着b不能完全被4整除,所以我们的指数现在将是exp=b%4,因为通过乘以数字指数,我们可以根据上图中的循环表得到最后一位数字。
    • 现在计算ldigit=pow(基数中的最后一位,exp)。
    • a^b的最后一位数字将是ldigit%10。

下面是上述算法的实现。

C++

// C++ code to find last digit of a^b
#include <bits/stdc++.h>
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.


Python3

# Python 3 code to find last digit of a ^ bimport math# Function to find b % adef Modulo(a, b) :    # Initialize result    mod = 0    # calculating mod of b with a to make    # b like 0 <= b < a    for i in range(0, len(b)) :        mod = (mod * 10 + (int)(b[i])) % a    return mod # return modulo# function to find last digit of a ^ bdef LastDigit(a, b) :    len_a = len(a)    len_b = len(b)    # if a and b both are 0    if (len_a == 1 and len_b == 1 and b[0] == '0' and a[0] == '0') :        return 1    # if exponent is 0    if (len_b == 1 and b[0]=='0') :        return 1    # if base is 0    if (len_a == 1 and 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    if((Modulo(4, b) == 0)) :        exp = 4    else :         exp = Modulo(4, b)    # Find last digit in 'a' and compute its exponent    res = math.pow((int)(a[len_a - 1]), exp)    # Return last digit of result    return res % 10    # Driver program to run test casea = ['1', '1', '7']b = ['3']print(LastDigit(a, b))# This code is contributed to Nikita Tiwari.

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.


PHP

<?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.
?>


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>


输出:

3

本文由 沙申克·米什拉(古卢) .本文由Geeksforgeks团队审阅。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享