给定两个数字a和n,任务是找到a ^ n(pow(a,n))的单个数字之和。在一位数总和中,我们将继续对位求和,直到剩下一位数为止。
例子:
Input : a = 5, n = 4
Output : 4
5^4 = 625 = 6+2+5 = 13
Since 13 has two digits, we
sum again 1 + 3 = 4.
Input : a = 2, n = 8
Output : 4
2^8=256 = 2+5+6 = 13 = 1+3 = 4
天真的方法是先找到a ^ n,然后使用此处讨论的方法找到a ^ n中的数字总和。
上述方法可能会导致溢出。一个更好的解决方案是基于以下观察。
int res = 1;
for (int i=1; i<=n; i++)
{
res = res*a;
res = digSum(res);
}
Here digSum() finds single digit sum
of res. Please refer this for details
of digSum().
上面的伪代码说明:
For example, let a = 5, n = 4.
After first iteration,
res = 5
After second iteration,
res = 7 (Note : 2 + 5 = 7)
After third iteration,
res = 8 (Note : 3 + 5 = 8)
After 4th iteration,
res = 4 (Note : 4 + 0 = 4)
我们可以写类似于快速模幂函数来评估digSum(一^ N),其评价这在日志(n)的步骤。
下面是上述方法的实现:
C++
// CPP program to find single digit
// sum of a^n.
#include
using namespace std;
// This function finds single digit
// sum of n.
int digSum(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Returns single digit sum of a^n.
// We use modular exponentiation technique.
int powerDigitSum(int a, int n)
{
int res = 1;
while (n) {
if (n % 2 == 1) {
res = res * digSum(a);
res = digSum(res);
}
a = digSum(digSum(a) * digSum(a));
n /= 2;
}
return res;
}
// Driver code
int main()
{
int a = 9, n = 4;
cout << powerDigitSum(a, n);
return 0;
}
Java
// Java program to find single digit
// sum of a^n.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// This function finds single digit
// sum of n.
static int digSum(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Returns single digit sum of a^n.
// We use modular exponentiation technique.
static int powerDigitSum(int a, int n)
{
int res = 1;
while (n>0) {
if (n % 2 == 1) {
res = res * digSum(a);
res = digSum(res);
}
a = digSum(digSum(a) * digSum(a));
n /= 2;
}
return res;
}
// Driver code
public static void main(String args[])
{
int a = 9, n = 4;
System.out.print(powerDigitSum(a, n));
}
}
Python 3
# Python 3 Program to find single digit
# sum of a^n.
# This function finds single digit
# sum of n.
def digSum(n) :
if n == 0 :
return 0
elif n % 9 == 0 :
return 9
else :
return n % 9
# Returns single digit sum of a^n.
# We use modular exponentiation technique.
def powerDigitSum(a, n) :
res = 1
while(n) :
if n %2 == 1 :
res = res * digSum(a)
res = digSum(res)
a = digSum(digSum(a) * digSum(a))
n //= 2
return res
# Driver Code
if __name__ == "__main__" :
a, n = 9, 4
print(powerDigitSum(a, n))
# This code is contributed by ANKITRAI1
C#
// C# program to find single
// digit sum of a^n.
class GFG
{
// This function finds single
// digit sum of n.
static int digSum(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ?
9 : (n % 9);
}
// Returns single digit sum of a^n.
// We use modular exponentiation
// technique.
static int powerDigitSum(int a, int n)
{
int res = 1;
while (n > 0)
{
if (n % 2 == 1)
{
res = res * digSum(a);
res = digSum(res);
}
a = digSum(digSum(a) * digSum(a));
n /= 2;
}
return res;
}
// Driver code
static void Main()
{
int a = 9, n = 4;
System.Console.WriteLine(powerDigitSum(a, n));
}
}
// This Code is contributed by mits
PHP
Javascript
输出:
9