给定两个整数X和Y ,任务是检查是否可以通过使用给定的操作多次来使这两个整数等于0。操作描述如下-
- 选择任意整数Z。
- 使用以下任一值更新值:
- X = X – 2 * Z,Y = Y – 3 * Z
- X = X – 3 * Z,Y = Y – 2 * Z
例子:
Input: X = 6, Y = 9
Output: Yes
Explanation:
Operation 1: Choose Z = 3, X = 6 – 2*3 = 0 and Y = 9 – 3*3 = 0
Since X and Y can be made equal to 0 using 1 operation, the required answer is Yes.
Input: X = 33, Y = 27
Output: Yes
Explanation:
Operation 1: Choose Z = 9, X := 33 – 3*9 = 6 and Y := 27 – 2*9 = 9
Operation 2: Choose Z = 3, X := 6 – 2*3 = 0 and Y := 9 – 3*3 = 0
Since X and Y can be made equal to 0 using 2 operation, the required answer is Yes.
方法:假设X≤Y。如果满足以下两个条件,则答案为“是”:
- (X + Y)mod 5 = 0 :因为在每次操作后,(X + Y)mod 5的值不变。
假设已经选择了任意数字Z。
因此,值(X + Y)将更改为((X - 3Z) + (Y - 2Z))
这等于
(X + Y - 5Z)
为了使该值等于0, X + Y = 5Z 。因此,在两侧取mod时,(X + Y)mod 5必须等于0。
- 3 * X> = 2 * Y,因此减法不会使X和Y的值变为负数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
void ifPossible(int X, int Y)
{
if (X > Y)
swap(X, Y);
// Check for the two conditions
if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
int X = 33, Y = 27;
ifPossible(X, Y);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
static void ifPossible(int X, int Y)
{
if (X > Y)
swap(X, Y);
// Check for the two conditions
if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
System.out.print("Yes");
else
System.out.print("No");
}
static void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
// Driver code
public static void main(String[] args)
{
int X = 33, Y = 27;
ifPossible(X, Y);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to check if X and Y
# can be made equal to zero by
# using given operation any number of times
def ifPossible(X, Y):
if (X > Y):
X, Y = Y, X
# Check for the two conditions
if ((X + Y) % 5 == 0 and 3 * X >= 2 * Y):
print("Yes")
else:
print("No")
# Driver code
X = 33
Y = 27
ifPossible(X, Y)
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to check if X and Y
// can be made equal to zero by
// using given operation any number of times
static void ifPossible(int X, int Y)
{
if (X > Y)
swap(X, Y);
// Check for the two conditions
if ((X + Y) % 5 == 0 && 3 * X >= 2 * Y)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
static void swap(int x, int y)
{
int temp = x;
x = y;
y = temp;
}
// Driver code
public static void Main()
{
int X = 33, Y = 27;
ifPossible(X, Y);
}
}
// This code is contributed by Yash_R
输出:
Yes