给定一个整数N ,任务是创建两组从1到N的截然不同的元素,以使它们各自总和的gcd大于1。打印各个集合。如果无法进行此类拆分,请打印-1。
例子:
Input: N = 5
Output:
2 4
1 3 5
Explanation:
GCD(Sum({2, 4}), Sum({1, 3, 5}) = GCD(6, 9) = 3
Input: N = 2
Output: -1
Explanation:
For N = 2, it is not possible to divide into two sets having GCD of their sum greater than 1.
方法1:一种简单的方法是将直到N的所有偶数均分成一组,而将所有奇数均分成另一组。
下面的代码是该方法的实现:
C++
// C++ program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
#include
using namespace std;
// Function to create
// and print the two sets
void createSets(int N)
{
// No such split
// possible for N <= 2
if (N <= 2) {
cout << "-1" << endl;
return;
}
// Print the first set
// consisting of even elements
for (int i = 2; i <= N; i += 2)
cout << i << " ";
cout << "\n";
// Print the second set
// consisting of odd ones
for (int i = 1; i <= N; i += 2) {
cout << i << " ";
}
}
// Driver Code
int main()
{
int N = 6;
createSets(N);
return 0;
}
Java
// Java program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
class GFG{
// Function to create
// and print the two sets
static void createSets(int N)
{
// No such split
// possible for N <= 2
if (N <= 2)
{
System.out.println("-1" );
return;
}
// Print the first set
// consisting of even elements
for (int i = 2; i <= N; i += 2)
System.out.print(i + " ");
System.out.print("\n") ;
// Print the second set
// consisting of odd ones
for (int i = 1; i <= N; i += 2)
{
System.out.print(i + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
createSets(N);
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to split N natural numbers
# into two sets having GCD
# of their sums greater than 1
# Function to create
# and print the two sets
def createSets(N):
# No such split
# possible for N <= 2
if (N <= 2):
print("-1");
return;
# Print the first set
# consisting of even elements
for i in range(2, N + 1, 2):
print(i, end=" ");
print("");
# Print the second set
# consisting of odd ones
for i in range(1, N + 1, 2):
print(i, end = " ");
# Driver Code
if __name__ == '__main__':
N = 6;
createSets(N);
# This code is contributed by gauravrajput1
C#
// C# program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
using System;
class GFG{
// Function to create
// and print the two sets
static void createSets(int N)
{
// No such split
// possible for N <= 2
if (N <= 2)
{
Console.WriteLine("-1" );
return;
}
// Print the first set
// consisting of even elements
for (int i = 2; i <= N; i += 2)
Console.Write(i + " ");
Console.Write("\n") ;
// Print the second set
// consisting of odd ones
for (int i = 1; i <= N; i += 2)
{
Console.Write(i + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 6;
createSets(N);
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
#include
using namespace std;
#define ll long long
// Function to create
// and print the two sets
void createSets(ll n)
{
// For n <= 2 such sets
// can never be formed
if (n <= 2) {
cout << "-1";
return;
}
else {
// Check if N is even or odd
// and store the element
// which divides the sum of N
// natural numbers accordingly
ll x = (n % 2 == 0) ? (n / 2)
: ((n + 1) / 2);
// First set
cout << x << endl;
// Print elements of second set
for (ll i = 1; i <= n; i++) {
if (i == x)
continue;
cout << i << " ";
}
}
return;
}
// Driver code
int main()
{
ll N = 7;
createSets(N);
return 0;
}
Java
// Java program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
class GFG{
// Function to create
// and print the two sets
static void createSets(long n)
{
// For n <= 2 such sets
// can never be formed
if (n <= 2)
{
System.out.print("-1");
return;
}
else
{
// Check if N is even or odd
// and store the element
// which divides the sum of N
// natural numbers accordingly
long x = (n % 2 == 0) ? (n / 2) :
((n + 1) / 2);
// First set
System.out.print(x + "\n");
// Print elements of second set
for(int i = 1; i <= n; i++)
{
if (i == x)
continue;
System.out.print(i + " ");
}
}
return;
}
// Driver code
public static void main(String[] args)
{
long N = 7;
createSets(N);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to split N natural numbers
# into two sets having GCD
# of their sums greater than 1
# Function to create
# and print the two sets
def createSets(n):
# For n <= 2 such sets
# can never be formed
if (n <= 2):
print("-1");
return;
else:
# Check if N is even or odd
# and store the element
# which divides the sum of N
# natural numbers accordingly
x = (n // 2) if(n % 2 == 0) else (
(n + 1) // 2);
# First set
print(x, " ");
# Print elements of second set
for i in range(1, n + 1):
if (i == x):
continue;
print(i, end = " ");
return;
# Driver code
if __name__ == '__main__':
N = 7;
createSets(N);
# This code is contributed by 29AjayKumar
C#
// C# program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
using System;
class GFG{
// Function to create
// and print the two sets
static void createSets(long n)
{
// For n <= 2 such sets
// can never be formed
if (n <= 2)
{
Console.Write("-1");
return;
}
else
{
// Check if N is even or odd
// and store the element
// which divides the sum of N
// natural numbers accordingly
long x = (n % 2 == 0) ? (n / 2) :
((n + 1) / 2);
// First set
Console.Write(x + "\n");
// Print elements of second set
for(int i = 1; i <= n; i++)
{
if (i == x)
continue;
Console.Write(i + " ");
}
}
return;
}
// Driver code
public static void Main(String[] args)
{
long N = 7;
createSets(N);
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
2 4 6
1 3 5
方法2:基于N,可以将该方法分为2种情况:
- 如果N为奇数:
- 在这种情况下,N个自然数的总和可被(N + 1)/ 2整除。
- 因此,我们只需要将(N + 1)/ 2放入第一组,其余数字放入第二组。
- 这将自动使GCD的总和大于1。
- 如果N是偶数:
- 在这种情况下,N个自然数的总和可被N / 2整除。
- 因此,我们只需要将N / 2放在第一组中,其余的数字放在第二组中。
- 这将自动使GCD的总和大于1。
下面是上述方法的实现:
C++
// C++ program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
#include
using namespace std;
#define ll long long
// Function to create
// and print the two sets
void createSets(ll n)
{
// For n <= 2 such sets
// can never be formed
if (n <= 2) {
cout << "-1";
return;
}
else {
// Check if N is even or odd
// and store the element
// which divides the sum of N
// natural numbers accordingly
ll x = (n % 2 == 0) ? (n / 2)
: ((n + 1) / 2);
// First set
cout << x << endl;
// Print elements of second set
for (ll i = 1; i <= n; i++) {
if (i == x)
continue;
cout << i << " ";
}
}
return;
}
// Driver code
int main()
{
ll N = 7;
createSets(N);
return 0;
}
Java
// Java program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
class GFG{
// Function to create
// and print the two sets
static void createSets(long n)
{
// For n <= 2 such sets
// can never be formed
if (n <= 2)
{
System.out.print("-1");
return;
}
else
{
// Check if N is even or odd
// and store the element
// which divides the sum of N
// natural numbers accordingly
long x = (n % 2 == 0) ? (n / 2) :
((n + 1) / 2);
// First set
System.out.print(x + "\n");
// Print elements of second set
for(int i = 1; i <= n; i++)
{
if (i == x)
continue;
System.out.print(i + " ");
}
}
return;
}
// Driver code
public static void main(String[] args)
{
long N = 7;
createSets(N);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to split N natural numbers
# into two sets having GCD
# of their sums greater than 1
# Function to create
# and print the two sets
def createSets(n):
# For n <= 2 such sets
# can never be formed
if (n <= 2):
print("-1");
return;
else:
# Check if N is even or odd
# and store the element
# which divides the sum of N
# natural numbers accordingly
x = (n // 2) if(n % 2 == 0) else (
(n + 1) // 2);
# First set
print(x, " ");
# Print elements of second set
for i in range(1, n + 1):
if (i == x):
continue;
print(i, end = " ");
return;
# Driver code
if __name__ == '__main__':
N = 7;
createSets(N);
# This code is contributed by 29AjayKumar
C#
// C# program to split N natural numbers
// into two sets having GCD
// of their sums greater than 1
using System;
class GFG{
// Function to create
// and print the two sets
static void createSets(long n)
{
// For n <= 2 such sets
// can never be formed
if (n <= 2)
{
Console.Write("-1");
return;
}
else
{
// Check if N is even or odd
// and store the element
// which divides the sum of N
// natural numbers accordingly
long x = (n % 2 == 0) ? (n / 2) :
((n + 1) / 2);
// First set
Console.Write(x + "\n");
// Print elements of second set
for(int i = 1; i <= n; i++)
{
if (i == x)
continue;
Console.Write(i + " ");
}
}
return;
}
// Driver code
public static void Main(String[] args)
{
long N = 7;
createSets(N);
}
}
// This code is contributed by sapnasingh4991
Java脚本
输出:
4
1 2 3 5 6 7
时间复杂度: O(N)
辅助空间复杂度: O(1)