通过将它们除以它们的因子来检查 X 和 Y 是否可以在 N 步中相等
给定正整数X 、 Y和N ,任务是检查是否可以在恰好N 次操作中使X等于Y ,其中每个操作:
- X可以除以除 1 之外的任何因子。
- Y可以除以除 1 以外的任何因子。
例子:
Input: X = 4, Y = 21, N = 3
Output: Yes
Explanation: Both X and Y can be made equal by doing the following operations:
X = X/4, Y = Y/3, Y = Y/7 which results in both X and Y as 1.
There are several other ways also to make both equal in three operations.
Like X = X/2, X = X/2, Y = Y/21.
Input: X = 5, Y = 17, N = 3
Output: No
方法:解决问题的想法基于以下观察:
- The problem can be solved by making both the numbers 1.
- Minimum number of times to divide a number to get 1 is to divide the number by itself. And the maximum number of times to get 1 is the sum of exponents of all prime factors.
- Any natural number N can be written in terms of the product of its prime factors as:
N = p1n1.p2n2. . . pknk, where p1, p2 . . . pk are distinct prime numbers. - So the maximum number of times N can be divided is equal to n1 + n2 + …. + nk.
- If the maximum divisor of both X and Y combined is greater or equal to N, both can be made equal otherwise not.
按照下面提到的步骤来实现上述观察:
- 如果N等于 1:
- 如果X和Y中的一个是另一个的因子,那么它们可以相等。
- 否则,它们不能相等。
- 否则,如果 N 小于或等于 X 和 Y 的最大因子数,则可以使它们相等。
- 否则,没有解决方案是可能的。
以下是以优化的方式实现上述方法:
C++
// C++ code to implement above approach
#include
using namespace std;
// Function to find total
// number of times we can
// divide a number before making it 1.
int count(int val)
{
int ans = 0;
// Checking how many times
// the number can be
// divided by 2.
while (val % 2 == 0) {
ans++;
val = val / 2;
}
// Iterating through each number from
// 3 to sqrt(val) and finding how many
// times the number can be divide
for (int i = 3; i <= sqrt(val); i = i + 2) {
while (val % i == 0) {
ans++;
val = val / i;
}
}
if (val > 2)
ans++;
return ans;
}
// Function to return if
// we can make x aand y equal or not
int solve(int x, int y, int n)
{
if (n == 0) {
if (x == y) {
cout << "Yes";
}
else {
cout << "No";
}
}
else if (n == 1) {
if ((x % y == 0 || y % x == 0)
&& x != y) {
cout << "Yes";
}
else {
cout << "No";
}
}
else {
if (count(x) + count(y) >= n) {
cout << "Yes";
}
else {
cout << "No";
}
}
}
// Driver code
int main()
{
int X = 4, Y = 21, N = 3;
// Function call
solve(X, Y, N);
return 0;
}
Java
// Java code to implement above approach
import java.io.*;
class GFG
{
// Function to find total
// number of times we can
// divide a number before making it 1.
static int count(int val)
{
int ans = 0;
// Checking how many times
// the number can be
// divided by 2.
while (val % 2 == 0) {
ans++;
val = val / 2;
}
// Iterating through each number from
// 3 to sqrt(val) and finding how many
// times the number can be divide
for (int i = 3; i <= Math.sqrt(val); i = i + 2) {
while (val % i == 0) {
ans++;
val = val / i;
}
}
if (val > 2)
ans++;
return ans;
}
// Function to return if
// we can make x aand y equal or not
static void solve(int x, int y, int n)
{
if (n == 0) {
if (x == y) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
else if (n == 1) {
if ((x % y == 0 || y % x == 0)
&& x != y) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
else {
if (count(x) + count(y) >= n) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
}
// Driver code
public static void main (String[] args) {
int X = 4, Y = 21, N = 3;
// Function call
solve(X, Y, N);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python3 code to implement above approach
# Function to find total
# number of times we can
# divide a number before making it 1.
def count(val):
ans = 0
# Checking how many times
# the number can be
# divided by 2.
while (val % 2 == 0):
ans += 1
val = val // 2
# Iterating through each number from
# 3 to sqrt(val) and finding how many
# times the number can be divide
for i in range(3, int(val**0.5)+1, 2):
while (val % i == 0):
ans += 1
val = val // i
if (val > 2):
ans += 1
return ans
# Function to return if
# we can make x aand y equal or not
def solve(x, y, n):
if (n == 0):
if (x == y):
print("Yes")
else:
print("No")
elif (n == 1):
if ((x % y == 0 or y % x == 0) and x != y):
print("Yes")
else:
print("No")
else:
if (count(x) + count(y) >= n):
print("Yes")
else:
print("No")
# Driver code
if __name__ == "__main__":
x = 4
y = 21
n = 3
solve(x, y, n)
# This code is contributed by amnindersingh1414.
C#
// C# code to implement above approach
using System;
public class GFG {
// Function to find total
// number of times we can
// divide a number before making it 1.
static int count(int val)
{
int ans = 0;
// Checking how many times
// the number can be
// divided by 2.
while (val % 2 == 0) {
ans++;
val = val / 2;
}
// Iterating through each number from
// 3 to sqrt(val) and finding how many
// times the number can be divide
for (int i = 3; i <= Math.Sqrt(val); i = i + 2) {
while (val % i == 0) {
ans++;
val = val / i;
}
}
if (val > 2)
ans++;
return ans;
}
// Function to return if
// we can make x aand y equal or not
static void solve(int x, int y, int n)
{
if (n == 0) {
if (x == y) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
else if (n == 1) {
if ((x % y == 0 || y % x == 0) && x != y) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
else {
if (count(x) + count(y) >= n) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
}
// Driver code
public static int Main()
{
int X = 4, Y = 21, N = 3;
// Function call
solve(X, Y, N);
return 0;
}
}
// This code is contributed by Taranpreet
Javascript
输出
Yes
时间复杂度: O(√X)+ √Y)
辅助空间: O(1)