给定两个正整数n和x 。任务是将n表示为x(x a1 + x a2 + ….. + x a3 )的幂的和,这样x(x a1 ,x a2 ,…..,x a3 )的幂数应为最低限度。打印用于使和等于n的x的最小乘方数。
例子:
Input : n = 5, x = 3
Output : 3
5 = 30 + 30 + 31.
We use only 3 power terms of x { 30, 30, 31}
Input : n = 13, x = 4
Output : 4
13 = 40 + 41 + 41 + 41.
We use only four power terms of x.
Input : n = 6, x = 1
Output : 6
如果x = 1,则答案将仅为n(n = 1 +1 + …. n次)。
这个想法是使用霍纳法。任何数字n都可以表示为n = x * a + b,其中0 <= b <= x-1。现在,由于b在0到x – 1之间,因此b应该表示为x 0 b次的总和。
此外,a可以类似的方式分解等等。
解决此问题的算法:
1. Initialize a variable ans to 0.
2. While n > 0
a) ans = ans + n % x
b) n = n/x
3. Return ans.
下面是上述想法的实现:
C++
// C++ program to calculate minimum number
// of powers of x to make sum equal to n.
#include
using namespace std;
// Return minimum power terms of x required
int minPower(int n, int x)
{
// if x is 1, return n since any power
// of 1 is 1 only.
if (x==1)
return n;
// Consider n = a * x + b where a = n/x
// and b = n % x.
int ans = 0;
while (n > 0)
{
// Update count of powers for 1's added
ans += (n%x);
// Repeat the process for reduced n
n /= x;
}
return ans;
}
// Driven Program
int main()
{
int n = 5, x = 3;
cout << minPower(n, x) << endl;
return 0;
}
Java
// Java program to calculate
// minimum numberof powers of
// x to make sum equal to n.
class GFG
{
// Return minimum power
// terms of x required
static int minPower(int n, int x)
{
// if x is 1, return n since
// any power of 1 is 1 only.
if (x==1)
return n;
// Consider n = a * x + b where
// a = n/x and b = n % x.
int ans = 0;
while (n > 0)
{
// Update count of powers
// for 1's added
ans += (n % x);
// Repeat the process for reduced n
n /= x;
}
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 5, x = 3;
System.out.println(minPower(n, x));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to
# calculate minimum number
# of powers of x to make
# sum equal to n.
# Return minimum power
# terms of x required
def minPower(n,x):
# if x is 1, return
# n since any power
# of 1 is 1 only.
if (x==1):
return n
# Consider n = a * x + b where a = n/x
# and b = n % x.
ans = 0
while (n > 0):
# Update count of powers for 1's added
ans += (n%x)
# Repeat the process for reduced n
n //= x
return ans
# Driver code
n = 5
x = 3
print(minPower(n, x))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to calculate
// minimum numberof powers
// of x to make sum equal
// to n.
using System;
class GFG
{
// Return minimum power
// terms of x required
static int minPower(int n, int x)
{
// if x is 1, return n since
// any power of 1 is 1 only.
if (x == 1)
return n;
// Consider n = a * x + b where
// a = n / x and b = n % x.
int ans = 0;
while (n > 0)
{
// Update count of
// powers for 1's
// added
ans += (n % x);
// Repeat the process
// for reduced n
n /= x;
}
return ans;
}
// Driver code
public static void Main ()
{
int n = 5, x = 3;
Console.WriteLine(minPower(n, x));
}
}
// This code is contributed by vt_m.
PHP
0)
{
// Update count of powers
// for 1's added
$ans += ($n % $x);
// Repeat the process
// for reduced n
$n /= $x;
}
return $ans;
}
// Driver Code
$n = 5; $x = 3;
echo(minPower($n, $x));
// This code is contributed by Ajit.
?>
Javascript
输出:
3