系统会为您提供数字N和数字K。我们的任务是找到N的第k个最小除数。
例子:
Input : N = 12, K = 5
Output : 6
The divisors of 12 after sorting are 1, 2, 3, 4, 6 and 12.
Where the value of 5th divisor is equal to 6.
Input : N = 16, K 2
Output : 2
简单方法:一个简单的方法是从1到√N循环,找到N的所有因子并将其推入向量。最后,对向量排序并从向量打印第K个值。
注意:向量中的元素最初不会被排序,因为我们同时推动了(i)和(n / i)两个因子。这就是为什么需要在打印第K个因子之前对向量进行排序的原因。
下面是上述方法的实现:
C++
// C++ program to find K-th smallest factor
#include
using namespace std;
// function to find the k'th divisor
void findkth(int n, int k)
{
// initialize a vector v
vector v;
// store all the divisors
// so the loop will needs to run till sqrt ( n )
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
v.push_back(i);
if (i != sqrt(n))
v.push_back(n / i);
}
}
// sort the vector in an increasing order
sort(v.begin(), v.end());
// if k is greater than the size of vector
// then no divisor can be possible
if (k > v.size())
cout << "Doesn't Exist";
// else print the ( k - 1 )th value of vector
else
cout << v[k - 1];
}
// Driver code
int main()
{
int n = 15, k = 2;
findkth(n, k);
return 0;
}
Java
// Java program to find K-th smallest factor
import java.util.*;
class GFG{
// function to find the k'th divisor
static void findkth(int n, int k)
{
// initialize a vector v
Vector v = new Vector();
// store all the divisors
// so the loop will needs to run till sqrt ( n )
for (int i = 1; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
v.add(i);
if (i != Math.sqrt(n))
v.add(n / i);
}
}
// sort the vector in an increasing order
Collections.sort(v);
// if k is greater than the size of vector
// then no divisor can be possible
if (k > v.size())
System.out.print("Doesn't Exist");
// else print the ( k - 1 )th value of vector
else
System.out.print(v.get(k - 1));
}
// Driver code
public static void main(String[] args)
{
int n = 15, k = 2;
findkth(n, k);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find K-th smallest factor
from math import sqrt
# function to find the k'th divisor
def findkth(n, k):
# initialize a vector v
v = []
# store all the divisors so the loop
# will needs to run till sqrt ( n )
p = int(sqrt(n)) + 1
for i in range(1, p, 1):
if (n % i == 0):
v.append(i)
if (i != sqrt(n)):
v.append(n / i);
# sort the vector in an increasing order
v.sort(reverse = False)
# if k is greater than the size of vector
# then no divisor can be possible
if (k > len(v)):
print("Doesn't Exist")
# else print the (k - 1)th
# value of vector
else:
print(v[k - 1])
# Driver code
if __name__ == '__main__':
n = 15
k = 2
findkth(n, k)
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find K-th smallest factor
using System;
using System.Collections.Generic;
class GFG{
// function to find the k'th divisor
static void findkth(int n, int k)
{
// initialize a vector v
List v = new List();
// store all the divisors
// so the loop will needs to run till sqrt ( n )
for (int i = 1; i <= Math.Sqrt(n); i++) {
if (n % i == 0) {
v.Add(i);
if (i != Math.Sqrt(n))
v.Add(n / i);
}
}
// sort the vector in an increasing order
v.Sort();
// if k is greater than the size of vector
// then no divisor can be possible
if (k > v.Count)
Console.Write("Doesn't Exist");
// else print the ( k - 1 )th value of vector
else
Console.Write(v[k - 1]);
}
// Driver code
public static void Main(String[] args)
{
int n = 15, k = 2;
findkth(n, k);
}
}
// This code is contributed by PrinciRaj1992
C++
// C++ program to find the K-th smallest factor
#include
using namespace std;
// Function to find the k'th divisor
void findkth ( int n, int k)
{
// initialize vectors v1 and v2
vector v1;
vector v2;
// store all the divisors in the two vectors
// accordingly
for( int i = 1 ; i <= sqrt( n ); i++ )
{
if ( n % i == 0 )
{
v1.push_back ( i );
if ( i != sqrt ( n ) )
v2.push_back ( n / i );
}
}
// reverse the vector v2 to sort it
// in increasing order
reverse(v2.begin(), v2.end());
// if k is greater than the size of vectors
// then no divisor can be possible
if ( k > (v1.size() + v2.size()))
cout << "Doesn't Exist" ;
// else print the ( k - 1 )th value of vector
else
{
// If K is lying in first vector
if(k <= v1.size())
cout<
Java
// Java program to find the K-th smallest factor
import java.util.*;
class GFG
{
// Function to find the k'th divisor
static void findkth ( int n, int k)
{
// initialize vectors v1 and v2
Vector v1 = new Vector();
Vector v2 = new Vector();
// store all the divisors in the two vectors
// accordingly
for( int i = 1 ; i <= Math.sqrt( n ); i++ )
{
if ( n % i == 0 )
{
v1.add ( i );
if ( i != Math.sqrt ( n ) )
v2.add ( n / i );
}
}
// reverse the vector v2 to sort it
// in increasing order
Collections.reverse(v2);
// if k is greater than the size of vectors
// then no divisor can be possible
if ( k > (v1.size() + v2.size()))
System.out.print("Doesn't Exist");
// else print the ( k - 1 )th value of vector
else
{
// If K is lying in first vector
if(k <= v1.size())
System.out.print(v1.get(k - 1));
// If K is lying in second vector
else
System.out.print(v2.get(k-v1.size() - 1));
}
}
// Driver code
public static void main(String[] args)
{
int n = 15, k = 2;
findkth ( n, k) ;
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find the K-th
# smallest factor
import math as mt
# Function to find the k'th divisor
def findkth (n, k):
# initialize vectors v1 and v2
v1 = list()
v2 = list()
# store all the divisors in the
# two vectors accordingly
for i in range(1, mt.ceil(n**(.5))):
if (n % i == 0):
v1.append(i)
if (i != mt.ceil(mt.sqrt(n))):
v2.append(n // i)
# reverse the vector v2 to sort it
# in increasing order
v2[::-1]
# if k is greater than the size of vectors
# then no divisor can be possible
if ( k > (len(v1) + len(v2))):
print("Doesn't Exist", end = "")
# else print the ( k - 1 )th value of vector
else:
# If K is lying in first vector
if(k <= len(v1)):
print(v1[k - 1])
# If K is lying in second vector
else:
print(v2[k - len(v1) - 1])
# Driver code
n = 15
k = 2
findkth (n, k)
# This code is contributed by Mohit kumar
C#
// C# program to find
// the K-th smallest factor
using System;
using System.Collections.Generic;
class GFG{
// Function to find the k'th divisor
static void findkth (int n, int k)
{
// initialize vectors v1 and v2
List v1 = new List();
List v2 = new List();
// store all the divisors in the
// two vectors accordingly
for(int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
v1.Add (i);
if (i != Math.Sqrt (n))
v2.Add (n / i);
}
}
// reverse the vector v2 to sort it
// in increasing order
v2.Reverse();
// if k is greater than the
// size of vectors then no
// divisor can be possible
if (k > (v1.Count + v2.Count))
Console.Write("Doesn't Exist");
// else print the (k - 1)th
// value of vector
else
{
// If K is lying in first vector
if(k <= v1.Count)
Console.Write(v1[k - 1]);
// If K is lying in second vector
else
Console.Write(v2[k - v1.Count - 1]);
}
}
// Driver code
public static void Main(String[] args)
{
int n = 15, k = 2;
findkth (n, k);
}
}
// This code is contributed by gauravrajput1
输出:
3
时间复杂度:√Nlog(√N)
高效的方法:一种有效的方法是将因子存储在两个单独的向量中。也就是说,对于从1到√N的所有i,因子i将存储在单独的向量中,而N / i将存储在单独的向量中。
现在,如果仔细观察,可以看出第一矢量已经按升序排序,第二矢量已经按降序排序。因此,反转第二个矢量并从其所在的两个矢量中打印第K个元素。
下面是上述方法的实现:
C++
// C++ program to find the K-th smallest factor
#include
using namespace std;
// Function to find the k'th divisor
void findkth ( int n, int k)
{
// initialize vectors v1 and v2
vector v1;
vector v2;
// store all the divisors in the two vectors
// accordingly
for( int i = 1 ; i <= sqrt( n ); i++ )
{
if ( n % i == 0 )
{
v1.push_back ( i );
if ( i != sqrt ( n ) )
v2.push_back ( n / i );
}
}
// reverse the vector v2 to sort it
// in increasing order
reverse(v2.begin(), v2.end());
// if k is greater than the size of vectors
// then no divisor can be possible
if ( k > (v1.size() + v2.size()))
cout << "Doesn't Exist" ;
// else print the ( k - 1 )th value of vector
else
{
// If K is lying in first vector
if(k <= v1.size())
cout<
Java
// Java program to find the K-th smallest factor
import java.util.*;
class GFG
{
// Function to find the k'th divisor
static void findkth ( int n, int k)
{
// initialize vectors v1 and v2
Vector v1 = new Vector();
Vector v2 = new Vector();
// store all the divisors in the two vectors
// accordingly
for( int i = 1 ; i <= Math.sqrt( n ); i++ )
{
if ( n % i == 0 )
{
v1.add ( i );
if ( i != Math.sqrt ( n ) )
v2.add ( n / i );
}
}
// reverse the vector v2 to sort it
// in increasing order
Collections.reverse(v2);
// if k is greater than the size of vectors
// then no divisor can be possible
if ( k > (v1.size() + v2.size()))
System.out.print("Doesn't Exist");
// else print the ( k - 1 )th value of vector
else
{
// If K is lying in first vector
if(k <= v1.size())
System.out.print(v1.get(k - 1));
// If K is lying in second vector
else
System.out.print(v2.get(k-v1.size() - 1));
}
}
// Driver code
public static void main(String[] args)
{
int n = 15, k = 2;
findkth ( n, k) ;
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find the K-th
# smallest factor
import math as mt
# Function to find the k'th divisor
def findkth (n, k):
# initialize vectors v1 and v2
v1 = list()
v2 = list()
# store all the divisors in the
# two vectors accordingly
for i in range(1, mt.ceil(n**(.5))):
if (n % i == 0):
v1.append(i)
if (i != mt.ceil(mt.sqrt(n))):
v2.append(n // i)
# reverse the vector v2 to sort it
# in increasing order
v2[::-1]
# if k is greater than the size of vectors
# then no divisor can be possible
if ( k > (len(v1) + len(v2))):
print("Doesn't Exist", end = "")
# else print the ( k - 1 )th value of vector
else:
# If K is lying in first vector
if(k <= len(v1)):
print(v1[k - 1])
# If K is lying in second vector
else:
print(v2[k - len(v1) - 1])
# Driver code
n = 15
k = 2
findkth (n, k)
# This code is contributed by Mohit kumar
C#
// C# program to find
// the K-th smallest factor
using System;
using System.Collections.Generic;
class GFG{
// Function to find the k'th divisor
static void findkth (int n, int k)
{
// initialize vectors v1 and v2
List v1 = new List();
List v2 = new List();
// store all the divisors in the
// two vectors accordingly
for(int i = 1; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
v1.Add (i);
if (i != Math.Sqrt (n))
v2.Add (n / i);
}
}
// reverse the vector v2 to sort it
// in increasing order
v2.Reverse();
// if k is greater than the
// size of vectors then no
// divisor can be possible
if (k > (v1.Count + v2.Count))
Console.Write("Doesn't Exist");
// else print the (k - 1)th
// value of vector
else
{
// If K is lying in first vector
if(k <= v1.Count)
Console.Write(v1[k - 1]);
// If K is lying in second vector
else
Console.Write(v2[k - v1.Count - 1]);
}
}
// Driver code
public static void Main(String[] args)
{
int n = 15, k = 2;
findkth (n, k);
}
}
// This code is contributed by gauravrajput1
输出:
3
时间复杂度:√N