找到给定总和为 N 的三元组 (X, Y, Z),两个数字的 GCD 是第三个数字
给定一个正整数N ,任务是找到三个不同的正整数 (X, Y, Z) 的三元组,使得 X + Y + Z = N 和 X = GCD (Y, Z)。
例子:
Input: N = 12
Output: 2 4 6
Explanation: The triplet (2, 4, 6) is set of distinct integers such that 2 + 4 + 6 = 12 and 2 = GCD(4, 6).
Input: N = 5675
Output:1 2835 2839
朴素方法:基本思想是用总和 N 迭代 (X, Y, Z) 的所有可能的三元组,并且对于每个三元组,检查 GCD(Y, Z) = X。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效方法:上述方法可以通过观察进一步优化,对于任何给定的 N,存在以下三种情况:
- 情况 1:如果N是偶数,则有效的三元组是 ( 1 , N/2 , N/2 -1 )。
- 情况 2:如果N为奇数且 ( N/2 ) 为偶数,则有效的三元组为 ( 1 , N/2 + 1 , N/2 -1 )。
- 情况 3:如果N是奇数并且 ( N/2 ) 也是奇数,则有效的三元组是 ( 1 , N/2 – 2 , N/2 + 2 )。
因此,对于任何给定的N ,识别案例并打印其各自的三元组。
下面是该方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to find a triplet (X, Y, Z)
// of distinct integers with their sum
// as N and GCD(Y, Z) = X
int printTriplet(int N)
{
// Case 1 where N is even
if (N % 2 == 0) {
cout << 1 << " " << (N / 2)
<< " " << (N / 2) - 1;
}
else {
// Case 2 where N is Odd
// and N/2 is even
if ((N / 2) % 2 == 0) {
cout << 1 << " "
<< (N / 2) - 1 << " "
<< (N / 2) + 1;
}
// Case 3 where N is Odd
// and N/2 is also odd
else {
cout << 1 << " "
<< (N / 2) - 2 << " "
<< (N / 2) + 2;
}
}
}
// Driver Code
int main()
{
int N = 5875;
printTriplet(N);
return 0;
}
Java
// Java program of the above approach
import java.util.*;
class GFG
{
// Function to find a triplet (X, Y, Z)
// of distinct integers with their sum
// as N and GCD(Y, Z) = X
static void printTriplet(int N)
{
// Case 1 where N is even
if (N % 2 == 0) {
System.out.print(1 + " " + (N / 2) +
" " + ((N / 2) - 1));
} else {
// Case 2 where N is Odd
// and N/2 is even
if ((N / 2) % 2 == 0) {
System.out.print(1 + " " + ((N / 2) - 1) +
" " + ((N / 2) + 1));
}
// Case 3 where N is Odd
// and N/2 is also odd
else {
System.out.print(1 + " " + ((N / 2) - 2) +
" " + ((N / 2) + 2));
}
}
}
// Driver Code
public static void main(String[] args) {
int N = 5875;
printTriplet(N);
}
}
// This code is contributed by 29AjayKumar
Python3
# python3 program of the above approach
# Function to find a triplet (X, Y, Z)
# of distinct integers with their sum
# as N and GCD(Y, Z) = X
def printTriplet(N):
# Case 1 where N is even
if (N % 2 == 0):
print(f"{1} {(N / 2)} {(N / 2) - 1}")
else:
# Case 2 where N is Odd
# and N/2 is even
if ((N // 2) % 2 == 0):
print(f"{1} {(N // 2) - 1} {(N // 2) + 1}")
# Case 3 where N is Odd
# and N/2 is also odd
else:
print(f"{1} {(N // 2) - 2} {(N // 2) + 2}")
# Driver Code
if __name__ == "__main__":
N = 5875
printTriplet(N)
# This code is contributed by rakeshsahni
C#
// C# program of the above approach
using System;
class GFG{
// Function to find a triplet (X, Y, Z)
// of distinct integers with their sum
// as N and GCD(Y, Z) = X
static void printTriplet(int N)
{
// Case 1 where N is even
if (N % 2 == 0)
{
Console.Write(1 + " " + (N / 2) + " " +
((N / 2) - 1));
}
else
{
// Case 2 where N is Odd
// and N/2 is even
if ((N / 2) % 2 == 0)
{
Console.Write(1 + " " + ((N / 2) - 1) + " " +
((N / 2) + 1));
}
// Case 3 where N is Odd
// and N/2 is also odd
else
{
Console.Write(1 + " " + ((N / 2) - 2) + " " +
((N / 2) + 2));
}
}
}
// Driver Code
public static void Main()
{
int N = 5875;
printTriplet(N);
}
}
// This code is contributed by ukasp
Javascript
输出
1 2935 2939
时间复杂度: O(1)
辅助空间: O(1)