给定数字N ,任务是查找N是否具有相等数量的奇数和偶数因子。
例子:
Input: N = 10
Output: YES
Explanation: 10 has two odd factors (1 and 5) and two even factors (2 and 10)
Input: N = 24
Output: NO
Explanation: 24 has two odd factors (1 and 3) and six even factors (2, 4, 6, 8 12 and 24)
Input: N = 125
Output: NO
天真的方法:查找所有除数,并检查奇数除数的数量是否与偶数除数的数量相同。
下面是上述方法的实现
C++
// C++ solution for the above approach
#include
using namespace std;
#define lli long long int
// Function to check condition
void isEqualFactors(lli N)
{
// Initialize even_count
// and od_count
lli ev_count = 0, od_count = 0;
// loop runs till square root
for (lli i = 1;
i <= sqrt(N) + 1; i++) {
if (N % i == 0) {
if (i == N / i) {
if (i % 2 == 0)
ev_count += 1;
else
od_count += 1;
}
else {
if (i % 2 == 0)
ev_count += 1;
else
od_count += 1;
if ((N / i) % 2 == 0)
ev_count += 1;
else
od_count += 1;
}
}
}
if (ev_count == od_count)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
// Driver Code
int main()
{
lli N = 10;
isEqualFactors(N);
return 0;
}
Java
// Java solution for the above approach
class GFG{
// Function to check condition
static void isEqualFactors(int N)
{
// Initialize even_count
// and od_count
int ev_count = 0, od_count = 0;
// Loop runs till square root
for(int i = 1;
i <= Math.sqrt(N) + 1; i++)
{
if (N % i == 0)
{
if (i == N / i)
{
if (i % 2 == 0)
ev_count += 1;
else
od_count += 1;
}
else
{
if (i % 2 == 0)
ev_count += 1;
else
od_count += 1;
if ((N / i) % 2 == 0)
ev_count += 1;
else
od_count += 1;
}
}
}
if (ev_count == od_count)
System.out.print("YES" + "\n");
else
System.out.print("NO" + "\n");
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
isEqualFactors(N);
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 code for the naive approach
import math
# Function to check condition
def isEqualFactors(N):
# Initialize even_count
# and od_count
ev_count = 0
od_count = 0
# loop runs till square root
for i in range(1, (int)(math.sqrt(N)) + 2) :
if (N % i == 0):
if(i == N // i):
if(i % 2 == 0):
ev_count += 1
else:
od_count += 1
else:
if(i % 2 == 0):
ev_count += 1
else:
od_count += 1
if((N // i) % 2 == 0):
ev_count += 1;
else:
od_count += 1;
if (ev_count == od_count):
print("YES")
else:
print("NO")
# Driver Code
N = 10
isEqualFactors(N)
C#
// C# solution for the above approach
using System;
class GFG{
// Function to check condition
static void isEqualFactors(int N)
{
// Initialize even_count
// and od_count
int ev_count = 0, od_count = 0;
// Loop runs till square root
for(int i = 1;
i <= Math.Sqrt(N) + 1; i++)
{
if (N % i == 0)
{
if (i == N / i)
{
if (i % 2 == 0)
ev_count += 1;
else
od_count += 1;
}
else
{
if (i % 2 == 0)
ev_count += 1;
else
od_count += 1;
if ((N / i) % 2 == 0)
ev_count += 1;
else
od_count += 1;
}
}
}
if (ev_count == od_count)
Console.Write("YES" + "\n");
else
Console.Write("NO" + "\n");
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
isEqualFactors(N);
}
}
// This code is contributed by gauravrajput1
C
// C code for the above approach
#include
#define lli long long int
// Function to check condition
void isEqualFactors(lli N)
{
if ((N % 2 == 0)
&& (N % 4 != 0))
printf("YES\n");
else
printf("NO\n");
}
// Driver Code
int main()
{
lli N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
return 0;
}
C++
// C++ code for the above approach
#include
using namespace std;
#define lli long long int
// Function to check condition
void isEqualFactors(lli N)
{
if ((N % 2 == 0)
and (N % 4 != 0))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
// Driver Code
int main()
{
lli N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
return 0;
}
Java
// JAVA code for the above approach
public class GFG {
// Function to check condition
static void isEqualFactors(int N)
{
if ((N % 2 == 0)
&& (N % 4 != 0))
System.out.println("YES");
else
System.out.println("NO");
}
// Driver code
public static void main(String args[])
{
int N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
}
}
Python
# Python code for the above approach
# Function to check condition
def isEqualFactors(N):
if ( ( N % 2 == 0 ) and ( N % 4 != 0) ):
print("YES")
else:
print("NO")
# Driver Code
N = 10
isEqualFactors(N)
N = 125;
isEqualFactors(N)
C#
// C# code for the above approach
using System;
class GFG {
// Function to check condition
static void isEqualFactors(int N)
{
if ((N % 2 == 0)
&& (N % 4 != 0))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
// Driver code
public static void Main()
{
int N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
}
}
Javascript
YES
时间复杂度: O(sqrt(N))
辅助空间: O(1)
高效的解决方案:
必须进行以下观察以优化上述方法:
- 根据唯一因式分解定理, 质数的乘积可以表示任何数字。因此,N可以表示为:
N = P1A1 * P2A2 * P3A3 * …….. * PkAK where, each Pi is a prime and each Ai is a positive integer.(1 <= i <= K)
- 根据组合定律,N的任何除数都可以采用以下形式:
N = P1B1 * P2B2 * P3B3 * …….. * PKBK where Bi is an integer and 0 <= Bi <= Ai for 1 <= i <= K.
- 如果除数在素数分解中不包含2,则将是奇数。因此,如果P 1 = 2,则B 1必须为0 。它只能以一种方式完成。
- 对于偶数除数,可以将B 1替换为1(或)2(或).. A 1以得到除数。可以用B 1种方式完成。
- 现在,对于其他人,每个B i都可以用0(或)1(或)2…..(或)A i替换为1 <= i <=K。这可以用(A i +1)方式完成。
- 根据基本原则:
- 奇数除数的数量为: X = 1 *(A 2 +1)*(A 3 +1)*….. *(A K +1)。
- 同样,偶数除数为: Y = A 1 *(A 2 +1)*(A 3 +1)*…。 *(A K +1)。
- 不可以除数的等于等于no。 X,Y的奇数除数应该相等。仅当A 1 = 1时才有可能。
因此,可以得出结论,如果一个数的偶数和奇数除数在素因数分解中具有1(只有1)的2的幂,则该数相等。
请按照以下步骤解决问题:
- 对于给定的数字N,检查它是否可以被2整除。
- 如果数字可被2整除,则检查数字是否可被2 2整除。如果是,则该数字将不具有相等数量的奇偶数。如果不是,则该数字将具有相等数量的奇数和偶数因子。
- 如果该数字不能被2整除,则该数字将永远不会有偶数因数,因此它的奇数和偶数因数也将不相等。
下面是上述方法的实现。
C
// C code for the above approach
#include
#define lli long long int
// Function to check condition
void isEqualFactors(lli N)
{
if ((N % 2 == 0)
&& (N % 4 != 0))
printf("YES\n");
else
printf("NO\n");
}
// Driver Code
int main()
{
lli N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
return 0;
}
C++
// C++ code for the above approach
#include
using namespace std;
#define lli long long int
// Function to check condition
void isEqualFactors(lli N)
{
if ((N % 2 == 0)
and (N % 4 != 0))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
// Driver Code
int main()
{
lli N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
return 0;
}
Java
// JAVA code for the above approach
public class GFG {
// Function to check condition
static void isEqualFactors(int N)
{
if ((N % 2 == 0)
&& (N % 4 != 0))
System.out.println("YES");
else
System.out.println("NO");
}
// Driver code
public static void main(String args[])
{
int N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
}
}
Python
# Python code for the above approach
# Function to check condition
def isEqualFactors(N):
if ( ( N % 2 == 0 ) and ( N % 4 != 0) ):
print("YES")
else:
print("NO")
# Driver Code
N = 10
isEqualFactors(N)
N = 125;
isEqualFactors(N)
C#
// C# code for the above approach
using System;
class GFG {
// Function to check condition
static void isEqualFactors(int N)
{
if ((N % 2 == 0)
&& (N % 4 != 0))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
// Driver code
public static void Main()
{
int N = 10;
isEqualFactors(N);
N = 125;
isEqualFactors(N);
}
}
Java脚本
YES
NO
时间复杂度: O(1)
辅助空间: O(1)