给定一个正整数N ,任务是找到一个三元组{X, Y, Z} ,使得X 、 Y和Z 的最小公倍数小于或等于N/2并且X 、 Y 、并且Z等于N 。如果存在多个答案,则打印其中任何一个。
例子:
Input: N = 8
Output: 4 2 2
Explanation:
One possible triplet is {4, 2, 2}. The sum of all the integers is equal to (4+2+2 = 8) and LCM is equal 4 which is equal to N/2( =4).
Input: N = 5
Output: 1 2 2
Explanation:
One possible triplet is {1, 2, 2}. The sum of all the integers is equal to (1+2+2 = 5) and LCM is equal 2 which is equal to N/2( =2).
处理方法:可以根据以下事实解决问题:
Suppose, N = X+Y+Z, then:
- If N is odd then either any one of X, Y or Z is odd or all three are odd numbers.
- If N is even, then all numbers must be even.
- Therefore, the idea is to include minimum possible value in the answer according to the above facts which will decrease the LCM of X, Y and Z.
请按照以下步骤解决问题:
- 将3 个变量x、y和z初始化为0以存储三元组的值。
- 如果N%2不等于0,即N是奇数,则执行以下步骤:
- 如果N是奇数,那么x、y或z中至少有一个应该是奇数,并且在最坏的情况下, x、y、z的 LCM 是N/2 。
- 将x和y的值设置为N/2 ,将z的值设置为1。
- 否则,如果N%4不等于0,则z的值可以是2 , x和y的值可以是N/2-1。否则, x的值可以是N/2 , y和z的值可以是N/4。
- 执行上述步骤后,打印x、y和z 的值。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find a triplet with the
// sum equal to N and LCM less than or
// equal to N
void validTriplet(int N)
{
// Stores the triplets
int x, y, z;
// If N is odd
if ((N % 2) != 0) {
x = N / 2;
y = N / 2;
z = 1;
}
// If N is even
else {
// If N is not a multiple of 4
if ((N % 4) != 0) {
x = (N / 2) - 1;
y = (N / 2) - 1;
z = 2;
}
// If N is a multiple of 4
else {
x = N / 2;
y = N / 4;
z = N / 4;
}
}
// Finally, print the answer
cout << x << " " << y << " " << z << '\n';
}
// Driver Code
int main()
{
// Given Input
int N = 5;
// Function Call
validTriplet(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find a triplet with the
// sum equal to N and LCM less than or
// equal to N
static void validTriplet(int N)
{
// Stores the triplets
int x, y, z;
// If N is odd
if ((N % 2) != 0) {
x = N / 2;
y = N / 2;
z = 1;
}
// If N is even
else {
// If N is not a multiple of 4
if ((N % 4) != 0) {
x = (N / 2) - 1;
y = (N / 2) - 1;
z = 2;
}
// If N is a multiple of 4
else {
x = N / 2;
y = N / 4;
z = N / 4;
}
}
// Finally, print the answer
System.out.print(x + " " + y + " " + z);
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int N = 5;
// Function Call
validTriplet(N);
}
}
// This code is contributed by Potta Lokesh
Python3
# Function to find a triplet with the
# sum equal to N and LCM less than or
# equal to N
def validTriplet(N):
# If N is odd
if (N % 2) != 0:
x = N//2
y = N//2
z = 1
# if N is even
else:
# If N is not a multiple of 4
if (N % 4) != 0:
x = N//2 - 1
y = N//2 - 1
z = 2
# if N is multiple of 4
else:
x = N//2
y = N//4
z = N//4
# Print the result
print(str(x) + " " + str(y) + " " + str(z))
# Driver code
N = 5
validTriplet(N)
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find a triplet with the
// sum equal to N and LCM less than or
// equal to N
static void validTriplet(int N)
{
// Stores the triplets
int x, y, z;
// If N is odd
if ((N % 2) != 0) {
x = N / 2;
y = N / 2;
z = 1;
}
// If N is even
else {
// If N is not a multiple of 4
if ((N % 4) != 0) {
x = (N / 2) - 1;
y = (N / 2) - 1;
z = 2;
}
// If N is a multiple of 4
else {
x = N / 2;
y = N / 4;
z = N / 4;
}
}
// Finally, print the answer
Console.Write(x + " " + y + " " + z );
}
// Driver Code
public static void Main()
{
// Given Input
int N = 5;
// Function Call
validTriplet(N);
}
}
// This code is contributed by ipg2016107.
Javascript
输出
2 2 1
时间复杂度: O(1)
辅助空间: O(1)