原文:

给定正整数 k,a 和 b,我们需要打印 a^b ie 的最后 k 位数字..功率(a,b)。

input constraint:
k <= 9, a <= 10^6, b<= 10^6

示例:

input : a = 11, b = 3, k = 2
output : 31
explanation : a^b = 11^3 = 1331, hence 
last two digits are 31
input : a = 10, b = 10000, k = 5
output : 00000
explanation : a^b = 1000..........0 (total 
zeros = 10000), hence last 5 digits are 00000

天真的pg电子试玩链接的解决方案

首先计算 a^b,然后通过与 10^k.取模取最后 k 位。当 a^b 太大时,上述pg电子试玩链接的解决方案失败,因为我们在 c/c 中最多只能容纳 2^64 -1。

有效解

有效的方法是每次乘法后只保留 k 个数字。这个想法与中讨论的非常相似,在这里我们讨论了一种寻找的一般方法(a^b)%c,这里的 c 是 10^k.

下面是实现。

c

// c   code to find last k digits of a^b
#include 
using namespace std;
/* iterative function to calculate (x^y)%p in o(log y) */
int power(long long int x, long long int y, long long int p)
{
    long long int res = 1; // initialize result
    x = x % p; // update x if it is more than or
    // equal to p
    while (y > 0) {
        // if y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
// c   function to calculate
// number of digits in x
int numberofdigits(int x)
{
    int i = 0;
    while (x) {
        x /= 10;
        i  ;
    }
    return i;
}
// c   function to print last k digits of a^b
void printlastkdigits(int a, int b, int k)
{
    cout << "last " << k;
    cout << " digits of " << a;
    cout << "^" << b << " = ";
    // generating 10^k
    int temp = 1;
    for (int i = 1; i <= k; i  )
        temp *= 10;
    // calling modular exponentiation
    temp = power(a, b, temp);
    // printing leftmost zeros. since (a^b)%k
    // can have digits less then k. in that
    // case we need to print zeros
    for (int i = 0; i < k - numberofdigits(temp); i  )
        cout << 0;
    // if temp is not zero then print temp
    // if temp is zero then already printed
    if (temp)
        cout << temp;
}
// driver program to test above functions
int main()
{
    int a = 11;
    int b = 3;
    int k = 2;
    printlastkdigits(a, b, k);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java code to find last k digits of a^b
public class gfg
{
    /* iterative function to calculate (x^y)%p in o(log y) */
    static int power(long x, long y, long p)
    {
        long res = 1; // initialize result
        x = x % p; // update x if it is more than or
        // equal to p
        while (y > 0) {
            // if y is odd, multiply x with result
            if ((y & 1) != 0)
                res = (res * x) % p;
            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }
        return (int) res;
    }
    // method  to print last k digits of a^b
    static void printlastkdigits(int a, int b, int k)
    {
        system.out.print("last "   k   " digits of "   a  
                            "^"     b   " = ");
        // generating 10^k
        int temp = 1;
        for (int i = 1; i <= k; i  )
            temp *= 10;
        // calling modular exponentiation
        temp = power(a, b, temp);
        // printing leftmost zeros. since (a^b)%k
        // can have digits less then k. in that
        // case we need to print zeros
        for (int i = 0; i < k - integer.tostring(temp).length() ; i  )
            system.out.print(0);
        // if temp is not zero then print temp
        // if temp is zero then already printed
        if (temp != 0)
            system.out.print(temp);
    }
    // driver method
    public static void main(string[] args)
    {
        int a = 11;
        int b = 3;
        int k = 2;
        printlastkdigits(a, b, k);
    }
}

python 3

# python 3 code to find last
# k digits of a^b
# iterative function to calculate
# (x^y)%p in o(log y)
def power(x, y, p):
    res = 1 # initialize result
    x = x % p # update x if it is more
              # than or equal to p
    while (y > 0) :
        # if y is odd, multiply
        # x with result
        if (y & 1):
            res = (res * x) % p
        # y must be even now
        y = y >> 1 # y = y/2
        x = (x * x) % p
    return res
# function to calculate
# number of digits in x
def numberofdigits(x):
    i = 0
    while (x) :
        x //= 10
        i  = 1
    return i
# function to print last k digits of a^b
def printlastkdigits( a, b, k):
    print("last "   str( k) " digits of "  
                    str(a)   "^"   str(b), end = " = ")
    # generating 10^k
    temp = 1
    for i in range(1, k   1):
        temp *= 10
    # calling modular exponentiation
    temp = power(a, b, temp)
    # printing leftmost zeros. since (a^b)%k
    # can have digits less then k. in that
    # case we need to print zeros
    for i in range( k - numberofdigits(temp)):
        print("0")
    # if temp is not zero then print temp
    # if temp is zero then already printed
    if (temp):
        print(temp)
# driver code
if __name__ == "__main__":
    a = 11
    b = 3
    k = 2
    printlastkdigits(a, b, k)
# this code is contributed
# by chitranayal

c

// c# code to find last k digits of a^b
using system;
class gfg
{
// iterative function to calculate
// (x^y)%p in o(log y)
static int power(long x, long y, long p)
{
    long res = 1; // initialize result
    x = x % p; // update x if it is more
               // than or equal to p
    while (y > 0)
    {
        // if y is odd, multiply x with result
        if ((y & 1) != 0)
            res = (res * x) % p;
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return (int) res;
}
// method to print last k digits of a^b
static void printlastkdigits(int a, int b, int k)
{
    console.write("last "   k   " digits of "  
                            a   "^"   b   " = ");
    // generating 10^k
    int temp = 1;
    for (int i = 1; i <= k; i  )
        temp *= 10;
    // calling modular exponentiation
    temp = power(a, b, temp);
    // printing leftmost zeros. since (a^b)%k
    // can have digits less then k. in that
    // case we need to print zeros
    for (int i = 0;    
             i < k - temp.tostring().length; i  )
        console.writeline(0);
    // if temp is not zero then print temp
    // if temp is zero then already printed
    if (temp != 0)
        console.write(temp);
}
// driver code
public static void main()
{
    int a = 11;
    int b = 3;
    int k = 2;
    printlastkdigits(a, b, k);
}
}
// this code is contributed
// by 29ajaykumar

服务器端编程语言(professional hypertext preprocessor 的缩写)

 0)
    {
        // if y is odd, multiply x
        // with result
        if ($y & 1)
            $res = ($res * $x) % $p;
        // y must be even now
        $y = $y >> 1; // y = y/2
        $x = ($x * $x) % $p;
    }
    return $res;
}
// function to calculate
// number of digits in x
function numberofdigits($x)
{
    $i = 0;
    while ($x)
    {
        $x = (int)$x / 10;
        $i  ;
    }
    return $i;
}
// function to print last k digits of a^b
function printlastkdigits( $a, $b, $k)
{
    echo "last ",$k;
    echo " digits of " ,$a;
    echo "^" , $b , " = ";
    // generating 10^k
    $temp = 1;
    for ($i = 1; $i <= $k; $i  )
        $temp *= 10;
    // calling modular exponentiation
    $temp = power($a, $b, $temp);
    // printing leftmost zeros. since
    // (a^b)%k can have digits less
    // then k. in that case we need
    // to print zeros
    for ($i = 0;
         $i < $k - numberofdigits($temp); $i  )
        echo 0;
    // if temp is not zero then print temp
    // if temp is zero then already printed
    if ($temp)
        echo $temp;
}
// driver code
$a = 11;
$b = 3;
$k = 2;
printlastkdigits($a, $b, $k);
// this code is contributed by ajit
?>

java 描述语言


输出:

last 2 digits of 11^3 = 31

时间复杂度:o(log b) t3】空间复杂度: o(1) 本文由 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。