我们给了两个数字A和B,我们需要编写一个程序来确定是否可以按照给定的步骤从(1,1)开始到达A和B。从(1,1),并在每个步骤开始选择一个随机数K和乘法K至在先前步骤和K 2到其它数量获得的两个数字中的任一个。
例子:
Input : A = 3, B = 9
Output : yes
Explanation: Starting from A = 1 and B = 1.
We choose k=3 and multiply 3 with the first number
to get A=3 and multiply k2=9 to the second-
number to get B=9.
Input : A = 60, B = 450
Output : yes
Explanation : Starting from A = 1 and B = 1,
Step 1: multiply k=3 and k2 to get 3 and 9
Step 2: Multiply k=5 and k2 = 25 to get to 15 and 225
Step 3: Multiply k2=4 and k=2 to get to A=60 and B=450
解决此问题的想法是密切观察,在每一步中,我们将k和k 2乘以数字。因此,如果可以达到A和B,则每一步的k ^ 3都将作为A * B中的因子。简而言之,如果数字A * B是一个完美的立方体,并且将A和B都除,那么只有执行指定的步骤才能从1和1开始获得数字。
下面是上述想法的实现:
C++
// CPP program to determine if
// A and B can be reached starting
// from 1, 1 following the given steps.
#include
using namespace std;
// function to check is it is possible to reach
// A and B starting from 1 and 1
bool possibleToReach(int a, int b)
{
// find the cuberoot of the number
int c = cbrt(a * b);
// divide the number by cuberoot
int re1 = a / c;
int re2 = b / c;
// if it is a perfect cuberoot and divides a and b
if ((re1 * re1 * re2 == a) && (re2 * re2 * re1 == b))
return true;
else
return false;
}
int main()
{
int A = 60, B = 450;
if (possibleToReach(A, B))
cout << "yes";
else
cout << "no";
return 0;
}
Java
// Java program to determine if
// A and B can be reached starting
// from 1, 1 following the given
// steps.
class GFG {
// function to check is it is
// possible to reach A and B
// starting from 1 and 1
static boolean possibleToReach(int a, int b)
{
// find the cuberoot of the number
int c = (int)Math.cbrt(a * b);
// divide the number by cuberoot
int re1 = a / c;
int re2 = b / c;
// if it is a perfect cuberoot and
// divides a and b
if ((re1 * re1 * re2 == a) &&
(re2 * re2 * re1 == b))
return true;
else
return false;
}
// Driver code
public static void main(String[] args)
{
int A = 60, B = 450;
if (possibleToReach(A, B))
System.out.println("yes");
else
System.out.println("no");
}
}
// This code is contributed by
// Smitha Dinesh Semwal
Python 3
# Python 3 program to determine if
# A and B can be reached starting
# from 1, 1 following the given steps.
import numpy as np
# function to check is it is possible to
# reach A and B starting from 1 and 1
def possibleToReach(a, b):
# find the cuberoot of the number
c = np.cbrt(a * b)
# divide the number by cuberoot
re1 = a // c
re2 = b // c
# if it is a perfect cuberoot and
# divides a and b
if ((re1 * re1 * re2 == a) and
(re2 * re2 * re1 == b)):
return True
else:
return False
# Driver Code
if __name__ == "__main__":
A = 60
B = 450
if (possibleToReach(A, B)):
print("yes")
else:
print("no")
# This code is contributed by ita_c
C#
// C# program to determine if
// A and B can be reached starting
// from 1, 1 following the given
// steps.
using System;
public class GFG{
// function to check is it is
// possible to reach A and B
// starting from 1 and 1
public static bool possibleToReach(int a, int b)
{
// find the cuberoot of the number
int c = (int)Math.Pow(a * b, (double) 1 / 3);
// divide the number by cuberoot
int re1 = a / c;
int re2 = b / c;
// if it is a perfect cuberoot
// and divides a and b
if ((re1 * re1 * re2 == a) &&
(re2 * re2 * re1 == b))
return true;
else
return false;
}
// Driver Code
static public void Main (String []args)
{
int A = 60, B = 450;
if (possibleToReach(A, B))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by Ajit.
PHP
Javascript
输出:
Yes