检查 N 是否可以表示为 3 的不同幂的总和
给定一个正整数N ,任务是检查给定的数字N是否可以表示为3的不同幂的总和。如果发现是真的,则打印“是” 。否则, “否” 。
例子:
Input: N = 28
Output: Yes
Explanation:
The number N(= 28) can be represented (1 + 7) = (30 + 33), which is a perfect power 2.
Input: N = 6
Output: No
方法:解决给定问题的最简单方法是生成3的所有不同幂的所有可能排列,如果存在任何这样的组合,其总和是3的完美幂。由于3 15 > 10 7所以只有16 个不同的幂,即[0, 15] 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to try all permutations
// of distinct powers
bool PermuteAndFind(vector power, int idx,
long SumSoFar, int target)
{
// Base Case
if (idx == power.size()) {
// If the distinct powers
// sum is obtained
if (SumSoFar == target)
return true;
// Otherwise
return false;
}
// If current element not selected
// in power[]
bool select
= PermuteAndFind(power, idx + 1, SumSoFar, target);
// If current element selected in
// power[]
bool notselect = PermuteAndFind(
power, idx + 1, SumSoFar + power[idx], target);
// Return 1 if any permutation
// found
return (select || notselect);
}
// Function to check the N can be
// represented as the sum of the
// distinct powers of 3
void DistinctPowersOf3(int N)
{
// Stores the all distincts powers
// of three to [0, 15]
vector power(16);
power[0] = 1;
for (int i = 1; i < 16; i++)
power[i] = 3 * power[i - 1];
// Function Call
bool found = PermuteAndFind(power, 0, 0L, N);
// print
if (found == true) {
cout << "Yes";
}
else {
cout << "No";
}
}
// Driven Code
int main()
{
int N = 91;
DistinctPowersOf3(N);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to try all permutations
// of distinct powers
public static boolean PermuteAndFind(int[] power, int idx,
int SumSoFar, int target)
{
// Base Case
if (idx == power.length)
{
// If the distinct powers
// sum is obtained
if (SumSoFar == target)
return true;
// Otherwise
return false;
}
// If current element not selected
// in power[]
boolean select = PermuteAndFind(power, idx + 1, SumSoFar, target);
// If current element selected in
// power[]
boolean notselect = PermuteAndFind(power, idx + 1, SumSoFar + power[idx], target);
// Return 1 if any permutation
// found
return (select || notselect);
}
// Function to check the N can be
// represented as the sum of the
// distinct powers of 3
public static void DistinctPowersOf3(int N)
{
// Stores the all distincts powers
// of three to [0, 15]
int[] power = new int[16];
power[0] = 1;
for (int i = 1; i < 16; i++)
power[i] = 3 * power[i - 1];
// Function Call
boolean found = PermuteAndFind(power, 0, 0, N);
// print
if (found == true) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
// Driven Code
public static void main(String args[]) {
int N = 91;
DistinctPowersOf3(N);
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python3 program for the above approach
# Function to try all permutations
# of distinct powers
def PermuteAndFind(power, idx, SumSoFar, target):
# Base Case
if (idx == len(power)):
# If the distinct powers
# sum is obtained
if (SumSoFar == target):
return True
# Otherwise
return False
# If current element not selected
# in power[]
select = PermuteAndFind(power, idx + 1,
SumSoFar, target)
# If current element selected in
# power[]
notselect = PermuteAndFind(power, idx + 1,
SumSoFar + power[idx],
target)
# Return 1 if any permutation
# found
return(select or notselect)
# Function to check the N can be
# represented as the sum of the
# distinct powers of 3
def DistinctPowersOf3(N):
# Stores the all distincts powers
# of three to[0, 15]
power = [0 for x in range(16)]
power[0] = 1
for i in range(1, 16):
power[i] = 3 * power[i - 1]
# Function Call
found = PermuteAndFind(power, 0, 0, N)
# Print
if (found == True):
print("Yes")
else:
print("No")
# Driver Code
N = 91
DistinctPowersOf3(N)
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
class GFG{
// Function to try all permutations
// of distinct powers
public static bool PermuteAndFind(int[] power, int idx,
int SumSoFar, int target)
{
// Base Case
if (idx == power.Length)
{
// If the distinct powers
// sum is obtained
if (SumSoFar == target)
return true;
// Otherwise
return false;
}
// If current element not selected
// in power[]
bool select = PermuteAndFind(power, idx + 1, SumSoFar, target);
// If current element selected in
// power[]
bool notselect = PermuteAndFind(power, idx + 1, SumSoFar + power[idx], target);
// Return 1 if any permutation
// found
return (select || notselect);
}
// Function to check the N can be
// represented as the sum of the
// distinct powers of 3
public static void DistinctPowersOf3(int N)
{
// Stores the all distincts powers
// of three to [0, 15]
int[] power = new int[16];
power[0] = 1;
for (int i = 1; i < 16; i++)
power[i] = 3 * power[i - 1];
// Function Call
bool found = PermuteAndFind(power, 0, 0, N);
// print
if (found == true) {
Console.Write("Yes");
} else {
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
int N = 91;
DistinctPowersOf3(N);
}
}
// This code is contributed by avijitmondal1998.
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether the given
// N can be represented as the sum of
// the distinct powers of 3
void DistinctPowersOf3(int N)
{
// Iterate until N is non-zero
while (N > 0) {
// Termination Condition
if (N % 3 == 2) {
cout << "No";
return;
}
// Right shift ternary bits
// by 1 for the next digit
N /= 3;
}
// If N can be expressed as the
// sum of perfect powers of 3
cout << "Yes";
}
// Driver Code
int main()
{
int N = 91;
DistinctPowersOf3(N);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to check whether the given
// N can be represented as the sum of
// the distinct powers of 3
public static void DistinctPowersOf3(int N)
{
// Iterate until N is non-zero
while (N > 0) {
// Termination Condition
if (N % 3 == 2) {
System.out.println("No");
return;
}
// Right shift ternary bits
// by 1 for the next digit
N /= 3;
}
// If N can be expressed as the
// sum of perfect powers of 3
System.out.println("Yes");
}
// Driver Code
public static void main(String args[])
{
int N = 91;
DistinctPowersOf3(N);
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python3 program for the above approach
# Function to check whether the given
# N can be represented as the sum of
# the distinct powers of 3
def DistinctPowersOf3(N):
# Iterate until N is non-zero
while (N > 0):
# Termination Condition
if (N % 3 == 2):
cout << "No"
return
# Right shift ternary bits
# by 1 for the next digit
N //= 3
# If N can be expressed as the
# sum of perfect powers of 3
print ("Yes")
# Driver Code
if __name__ == '__main__':
N = 91
DistinctPowersOf3(N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check whether the given
// N can be represented as the sum of
// the distinct powers of 3
static void DistinctPowersOf3(int N)
{
// Iterate until N is non-zero
while (N > 0) {
// Termination Condition
if (N % 3 == 2) {
Console.Write("No");
return;
}
// Right shift ternary bits
// by 1 for the next digit
N /= 3;
}
// If N can be expressed as the
// sum of perfect powers of 3
Console.Write("Yes");
}
// Driver Code
public static void Main()
{
int N = 91;
DistinctPowersOf3(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Yes
时间复杂度: O(2 N )
辅助空间: O(2 N )
另一种方法:上述方法也可以通过观察N为三进制形式的事实来优化 (基数 3 )。每个数字是3 i ,三进制数(N)是它们的总和。
要具有3的不同幂,求和为N,在三进制形式中,第i个数字可以是0,1 或 2 (在二进制中,它是 0 和 1)。因此,对于第 i 个位置的每个数字,可以有三种情况:
- 数字可以是0 ,即N中没有3 i数字。
- 数字可以是1 ,即N中有一个3 i数字。
- 数字不能是2 ,因为3 i中有 2 个,因此不是 distinct 。
请按照以下步骤解决问题:
- 迭代直到N变为0并执行以下步骤:
- 如果N%3的值为2 ,则打印“No” 。
- 否则,将N除以3 。
- 完成上述步骤后,如果N的值为0 ,则打印“Yes” 。否则, “否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check whether the given
// N can be represented as the sum of
// the distinct powers of 3
void DistinctPowersOf3(int N)
{
// Iterate until N is non-zero
while (N > 0) {
// Termination Condition
if (N % 3 == 2) {
cout << "No";
return;
}
// Right shift ternary bits
// by 1 for the next digit
N /= 3;
}
// If N can be expressed as the
// sum of perfect powers of 3
cout << "Yes";
}
// Driver Code
int main()
{
int N = 91;
DistinctPowersOf3(N);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to check whether the given
// N can be represented as the sum of
// the distinct powers of 3
public static void DistinctPowersOf3(int N)
{
// Iterate until N is non-zero
while (N > 0) {
// Termination Condition
if (N % 3 == 2) {
System.out.println("No");
return;
}
// Right shift ternary bits
// by 1 for the next digit
N /= 3;
}
// If N can be expressed as the
// sum of perfect powers of 3
System.out.println("Yes");
}
// Driver Code
public static void main(String args[])
{
int N = 91;
DistinctPowersOf3(N);
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python3 program for the above approach
# Function to check whether the given
# N can be represented as the sum of
# the distinct powers of 3
def DistinctPowersOf3(N):
# Iterate until N is non-zero
while (N > 0):
# Termination Condition
if (N % 3 == 2):
cout << "No"
return
# Right shift ternary bits
# by 1 for the next digit
N //= 3
# If N can be expressed as the
# sum of perfect powers of 3
print ("Yes")
# Driver Code
if __name__ == '__main__':
N = 91
DistinctPowersOf3(N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check whether the given
// N can be represented as the sum of
// the distinct powers of 3
static void DistinctPowersOf3(int N)
{
// Iterate until N is non-zero
while (N > 0) {
// Termination Condition
if (N % 3 == 2) {
Console.Write("No");
return;
}
// Right shift ternary bits
// by 1 for the next digit
N /= 3;
}
// If N can be expressed as the
// sum of perfect powers of 3
Console.Write("Yes");
}
// Driver Code
public static void Main()
{
int N = 91;
DistinctPowersOf3(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
Yes
时间复杂度: O(log 3 N)
辅助空间: O(1)