📜  最小值超过X,其除数与X的除数具有不同的奇偶性

📅  最后修改于: 2021-04-24 22:41:01             🧑  作者: Mango

给定一个整数X ,任务是确定大于XY的最小值,以使XY的除数计数具有不同的奇偶校验。

例子:

天真的方法:解决问题的最简单方法是迭代从X +1开始的每个数字,直到获得除数计数与X的奇偶性相反的元素。

时间复杂度: O((1 +√X) 2 )
辅助空间: O(1)

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count divisors of n
int divisorCount(int n)
{
    int x = 0;
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
            if (i == n / i)
                x++;
            else
                x += 2;
        }
    }
    return x;
}
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
int minvalue_y(int x)
{
    // Divisor count of x
    int a = divisorCount(x);
    int y = x + 1;
 
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1)
           == (divisorCount(y) & 1))
        y++;
    return y;
}
 
// Driver Code
int main()
{
    // Given X
    int x = 5;
 
    // Function call
    cout << minvalue_y(x) << endl;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count divisors of n
static int divisorCount(int n)
{
    int x = 0;
    for(int i = 1; i <= Math.sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (i == n / i)
                x++;
            else
                x += 2;
        }
    }
    return x;
}
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Divisor count of x
    int a = divisorCount(x);
    int y = x + 1;
 
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1) == (divisorCount(y) & 1))
        y++;
         
    return y;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given X
    int x = 5;
 
    // Function call
    System.out.println(minvalue_y(x));
}
}
 
// This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to count divisors of n
static int divisorCount(int n)
{
    int x = 0;
    for(int i = 1; i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (i == n / i)
                x++;
            else
                x += 2;
        }
    }
    return x;
}
  
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
      
    // Divisor count of x
    int a = divisorCount(x);
    int y = x + 1;
  
    // Iterate from x + 1 and
    // check for each element
    while ((a & 1) == (divisorCount(y) & 1))
        y++;
          
    return y;
}
  
// Driver Code
public static void Main()
{
      
    // Given X
    int x = 5;
  
    // Function call
    Console.WriteLine(minvalue_y(x));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python program for the above approach
 
# Function to count divisors of n
def divisorCount(n):
    x = 0;
    for i in range(1, n):
        if (n % i == 0):
            if (i == n // i):
                x += 1;
            else:
                x += 2;
        if(i * i > n):
            break;
 
    return x;
 
# Function to find the minimum
# value exceeding x whose count
# of divisors has different parity
# with count of divisors of X
def minvalue_y(x):
   
    # Divisor count of x
    a = divisorCount(x);
    y = x + 1;
 
    # Iterate from x + 1 and
    # check for each element
    while ((a & 1) == (divisorCount(y) & 1)):
        y += 1;
 
    return y;
 
# Driver Code
if __name__ == '__main__':
   
    # Given X
    x = 5;
 
    # Function call
    print(minvalue_y(x));
 
# This code is contributed by 29AjayKumar


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
int minvalue_y(int x)
{
    // Check if x is
    // perfect square
    int n = sqrt(x);
    if (n * n == x)
        return x + 1;
 
    return pow(n + 1, 2);
}
 
// Driver Code
int main()
{
    int x = 5;
    cout << minvalue_y(x) << endl;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
    
class GFG{
    
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Check if x is
    // perfect square
    int n = (int)Math.sqrt(x);
    if (n * n == x)
        return x + 1;
  
    return (int)Math.pow(n + 1, 2);
}
   
// Driver Code
public static void main(String[] args)
{
    int x = 5;
      
    System.out.print(minvalue_y(x));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# value exceeding x whose count
# of divisors has different parity
# with count of divisors of X
def minvalue_y(x):
     
    # Check if x is
    # perfect square
    n = int(pow(x, 1 / 2))
     
    if (n * n == x):
        return x + 1
         
    return(pow(n + 1, 2))
 
# Driver Code
if __name__ == '__main__':
     
    x = 5
 
    print(minvalue_y(x))
 
# This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
     
class GFG{
     
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Check if x is
    // perfect square
    int n = (int)Math.Sqrt(x);
    if (n * n == x)
        return x + 1;
         
    return (int)Math.Pow(n + 1, 2);
}
    
// Driver Code
public static void Main()
{
    int x = 5;
     
    Console.WriteLine(minvalue_y(x));
}
}
 
// This code is contributed by code_hunt


输出:
9

高效的方法:该问题可以基于以下观察得到解决:

  • 如果X是一个完美的正方形,那么X + 1将是答案。
  • 否则, (1 +√X) 2将是答案。

请按照以下步骤解决问题:

  1. 检查X是否是一个完美的正方形。如果发现是真的,则打印X +1
  2. 否则,打印(1 + floor(√X)) 2 )

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
int minvalue_y(int x)
{
    // Check if x is
    // perfect square
    int n = sqrt(x);
    if (n * n == x)
        return x + 1;
 
    return pow(n + 1, 2);
}
 
// Driver Code
int main()
{
    int x = 5;
    cout << minvalue_y(x) << endl;
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
    
class GFG{
    
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Check if x is
    // perfect square
    int n = (int)Math.sqrt(x);
    if (n * n == x)
        return x + 1;
  
    return (int)Math.pow(n + 1, 2);
}
   
// Driver Code
public static void main(String[] args)
{
    int x = 5;
      
    System.out.print(minvalue_y(x));
}
}
 
// This code is contributed by sanjoy_62

Python3

# Python3 program for the above approach
 
# Function to find the minimum
# value exceeding x whose count
# of divisors has different parity
# with count of divisors of X
def minvalue_y(x):
     
    # Check if x is
    # perfect square
    n = int(pow(x, 1 / 2))
     
    if (n * n == x):
        return x + 1
         
    return(pow(n + 1, 2))
 
# Driver Code
if __name__ == '__main__':
     
    x = 5
 
    print(minvalue_y(x))
 
# This code is contributed by Rajput-Ji

C#

// C# program for the above approach
using System;
     
class GFG{
     
// Function to find the minimum
// value exceeding x whose count
// of divisors has different parity
// with count of divisors of X
static int minvalue_y(int x)
{
     
    // Check if x is
    // perfect square
    int n = (int)Math.Sqrt(x);
    if (n * n == x)
        return x + 1;
         
    return (int)Math.Pow(n + 1, 2);
}
    
// Driver Code
public static void Main()
{
    int x = 5;
     
    Console.WriteLine(minvalue_y(x));
}
}
 
// This code is contributed by code_hunt
输出:
9

时间复杂度: O(1)
辅助空间: O(1)