N的奇数和偶数个数的绝对差
给定一个正整数N ,任务是找到N的奇数和偶数因子的绝对差值。
例子:
Input: N = 12
Output: 2
Explanation: The even factors of 12 are {2, 4, 6, 12}. Therefore, the count is 4.
The odd factors of 12 are {1, 3}. Therefore, the count is 2.
Hence, the difference between their counts is (4 – 2) = 2.
Input: N = 9
Output: 3
朴素方法:解决给定问题的最简单方法是找到数字N的所有除数,然后找到N的奇数和偶数除数计数的绝对差。
时间复杂度: O(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)
- 因此,因子总数 = (A 1 + 1)*(A 2 + 1)*(A 3 + 1)* ……… *(A 4 + 1) 。让这个计数为T 。
- 奇数因子的总数可以通过排除上式中2的幂来计算。设此计数为O 。
- 偶数因子的总数等于因子的总数与奇数因子的总数之差。
因此,想法是通过使用 Eratosthenes 的筛法找到N的素因数分解中的素因数及其幂,并打印因子总数与奇因子总数的两倍之差的绝对值作为结果,即abs(T – 2*O) 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the smallest prime
// factor of all the numbers using
// Sieve Of Eratosthenes
void sieveOfEratosthenes(int N, int s[])
{
// Stores whether any number
// is prime or not
vector prime(N + 1, false);
// Initialize smallest factor as
// 2 for all the even numbers
for (int i = 2; i <= N; i += 2)
s[i] = 2;
// Iterate over the range [3, N]
for (int i = 3; i <= N; i += 2) {
// If i is prime
if (prime[i] == false) {
s[i] = i;
// Iterate all multiples of i
for (int j = i; j * i <= N;
j += 2) {
// i is the smallest
// prime factor of i * j
if (!prime[i * j]) {
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
// Function to find the absolute
// difference between the count
// of odd and even factors of N
void findDifference(int N)
{
// Stores the smallest
// prime factor of i
int s[N + 1];
// Fill values in s[] using
// sieve of eratosthenes
sieveOfEratosthenes(N, s);
// Stores the total number of
// factors and the total number
// of odd and even factors
int total = 1, odd = 1, even = 0;
// Store the current prime
// factor of the number N
int curr = s[N];
// Store the power of
// current prime factor
int cnt = 1;
// Loop while N is greater than 1
while (N > 1) {
N /= s[N];
// If N also has smallest
// prime factor as curr, then
// increment cnt by 1
if (curr == s[N]) {
cnt++;
continue;
}
// Update only total number
// of factors if curr is 2
if (curr == 2) {
total = total * (cnt + 1);
}
// Update total number of
// factors and total number
// of odd factors
else {
total = total * (cnt + 1);
odd = odd * (cnt + 1);
}
// Update current prime
// factor as s[N] and
// count as 1
curr = s[N];
cnt = 1;
}
// Calculate the number
// of even factors
even = total - odd;
// Print the difference
cout << abs(even - odd);
}
// Driver Code
int main()
{
int N = 12;
findDifference(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the smallest prime
// factor of all the numbers using
// Sieve Of Eratosthenes
static void sieveOfEratosthenes(int N, int s[])
{
// Stores whether any number
// is prime or not
boolean []prime = new boolean[N + 1];
// Initialize smallest factor as
// 2 for all the even numbers
for(int i = 2; i <= N; i += 2)
s[i] = 2;
// Iterate over the range [3, N]
for(int i = 3; i <= N; i += 2)
{
// If i is prime
if (prime[i] == false)
{
s[i] = i;
// Iterate all multiples of i
for(int j = i; j * i <= N; j += 2)
{
// i is the smallest
// prime factor of i * j
if (!prime[i * j])
{
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
// Function to find the absolute
// difference between the count
// of odd and even factors of N
static void findDifference(int N)
{
// Stores the smallest
// prime factor of i
int []s = new int[N + 1];
// Fill values in s[] using
// sieve of eratosthenes
sieveOfEratosthenes(N, s);
// Stores the total number of
// factors and the total number
// of odd and even factors
int total = 1, odd = 1, even = 0;
// Store the current prime
// factor of the number N
int curr = s[N];
// Store the power of
// current prime factor
int cnt = 1;
// Loop while N is greater than 1
while (N > 1)
{
N /= s[N];
// If N also has smallest
// prime factor as curr, then
// increment cnt by 1
if (curr == s[N])
{
cnt++;
continue;
}
// Update only total number
// of factors if curr is 2
if (curr == 2)
{
total = total * (cnt + 1);
}
// Update total number of
// factors and total number
// of odd factors
else
{
total = total * (cnt + 1);
odd = odd * (cnt + 1);
}
// Update current prime
// factor as s[N] and
// count as 1
curr = s[N];
cnt = 1;
}
// Calculate the number
// of even factors
even = total - odd;
// Print the difference
System.out.print(Math.abs(even - odd));
}
// Driver Code
public static void main(String[] args)
{
int N = 12;
findDifference(N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find the smallest prime
# factor of all the numbers using
# Sieve Of Eratosthenes
def sieveOfEratosthenes(N, s):
# Stores whether any number
# is prime or not
prime = [False]*(N + 1)
# Initialize smallest factor as
# 2 for all the even numbers
for i in range(2, N + 1, 2):
s[i] = 2
# Iterate over the range [3, N]
for i in range(3, N, 2):
# If i is prime
if (prime[i] == False):
s[i] = i
# Iterate all multiples of i
for j in range(i, N, 2):
if j * i > N:
break
# i is the smallest
# prime factor of i * j
if (not prime[i * j]):
prime[i * j] = True
s[i * j] = i
# Function to find the absolute
# difference between the count
# of odd and even factors of N
def findDifference(N):
# Stores the smallest
# prime factor of i
s = [0]*(N+1)
# Fill values in s[] using
# sieve of eratosthenes
sieveOfEratosthenes(N, s)
# Stores the total number of
# factors and the total number
# of odd and even factors
total , odd , even =1, 1, 0
# Store the current prime
# factor of the number N
curr = s[N]
# Store the power of
# current prime factor
cnt = 1
# Loop while N is greater than 1
while (N > 1):
N //= s[N]
# If N also has smallest
# prime factor as curr, then
# increment cnt by 1
if (curr == s[N]):
cnt += 1
continue
# Update only total number
# of factors if curr is 2
if (curr == 2):
total = total * (cnt + 1)
# Update total number of
# factors and total number
# of odd factors
else:
total = total * (cnt + 1)
odd = odd * (cnt + 1)
# Update current prime
# factor as s[N] and
# count as 1
curr = s[N]
cnt = 1
# Calculate the number
# of even factors
even = total - odd
# Print the difference
print(abs(even - odd))
# Driver Code
if __name__ == '__main__':
N = 12
findDifference(N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the smallest prime
// factor of all the numbers using
// Sieve Of Eratosthenes
static void sieveOfEratosthenes(int N, int[] s)
{
// Stores whether any number
// is prime or not
bool[] prime = new bool[N + 1];
// Initialize smallest factor as
// 2 for all the even numbers
for (int i = 2; i <= N; i += 2)
s[i] = 2;
// Iterate over the range [3, N]
for (int i = 3; i <= N; i += 2) {
// If i is prime
if (prime[i] == false) {
s[i] = i;
// Iterate all multiples of i
for (int j = i; j * i <= N; j += 2) {
// i is the smallest
// prime factor of i * j
if (!prime[i * j]) {
prime[i * j] = true;
s[i * j] = i;
}
}
}
}
}
// Function to find the absolute
// difference between the count
// of odd and even factors of N
static void findDifference(int N)
{
// Stores the smallest
// prime factor of i
int[] s = new int[N + 1];
// Fill values in s[] using
// sieve of eratosthenes
sieveOfEratosthenes(N, s);
// Stores the total number of
// factors and the total number
// of odd and even factors
int total = 1, odd = 1, even = 0;
// Store the current prime
// factor of the number N
int curr = s[N];
// Store the power of
// current prime factor
int cnt = 1;
// Loop while N is greater than 1
while (N > 1) {
N /= s[N];
// If N also has smallest
// prime factor as curr, then
// increment cnt by 1
if (curr == s[N]) {
cnt++;
continue;
}
// Update only total number
// of factors if curr is 2
if (curr == 2) {
total = total * (cnt + 1);
}
// Update total number of
// factors and total number
// of odd factors
else {
total = total * (cnt + 1);
odd = odd * (cnt + 1);
}
// Update current prime
// factor as s[N] and
// count as 1
curr = s[N];
cnt = 1;
}
// Calculate the number
// of even factors
even = total - odd;
// Print the difference
Console.Write(Math.Abs(even - odd));
}
// Driver Code
public static void Main()
{
int N = 12;
findDifference(N);
}
}
// This code is contributed by ukasp.
Javascript
输出:
2
时间复杂度: O(N*(log log N))
辅助空间: O(N)