给定两个整数N和K ,任务是找到不能被N整除的第K个数。
注意: N的值大于1,因为每个数字都可以被1整除。
例子:
Input: N = 3, K = 6
Output: 8
Explanation:
Numbers which is not divisible by N = 3 – {1, 2, 4, 5, 7, 8, 10}
6th non-divisible number by 3 is 8.
Input: N = 7, K = 97
Output: 113
Explanation:
Numbers which is not divisible by N = 7 – {1, 2, 4, 5, 6, ….}
97th non-divisible number by 7 is 113.
天真的方法:一个简单的解决方案是遍历一个循环,用N查找第K个不可除数。下面是查找第K个步骤的步骤:
- 将不可除数和当前数的计数初始化为0。
- 使用while循环进行迭代,直到不可除数的计数不等于K。
- 如果当前数字不能被N整除,则将不可整数的计数加1。
下面是上述方法的实现:
C++
// C++ implementation to find
// the K'th non divisible
// number by N
#include
using namespace std;
// Function to find
// the K'th non divisible
// number by N
int kthNonDivisible(int N, int K)
{
int find = 0;
int j = 0;
// Loop to find the K non
// divisible number by N
while (find != K) {
j++;
if (j % N != 0)
find++;
}
return j;
}
// Driver Code
int main()
{
int N = 3;
int K = 6;
cout << kthNonDivisible(N, K);
return 0;
}
Java
// Java implementation to find
// the K'th non divisible
// number by N
class GFG{
// Function to find
// the K'th non divisible
// number by N
static int kthNonDivisible(int N, int K)
{
int find = 0;
int j = 0;
// Loop to find the K non
// divisible number by N
while (find != K)
{
j++;
if (j % N != 0)
find++;
}
return j;
}
// Driver code
public static void main(String[] args)
{
int N = 3;
int K = 6;
System.out.print(kthNonDivisible(N, K));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 implementation to find
# the K'th non divisible
# number of N
import math
# Function to find the Kth
# not divisible by N
def kthNonDivisible(n, K):
find = 0
j = 0
# Loop to find the K non
# divisible number by N
while find != K:
j = j + 1
if j % N != 0:
find = find + 1
return j
# Driver Code
N = 3
K = 6
# Function Call
print(kthNonDivisible(N, K))
# This code is contributed by ishayadav181
C#
// C# implementation to find the
// K'th non-divisible number by N
using System;
class GFG {
// Function to find the K'th
// non divisible number by N
static int kthNonDivisible(int N, int K)
{
int find = 0;
int j = 0;
// Loop to find the K non
// divisible number by N
while (find != K)
{
j++;
if (j % N != 0)
find++;
}
return j;
}
// Driver code
public static void Main(String[] args)
{
int N = 3;
int K = 6;
Console.Write(kthNonDivisible(N, K));
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ implementation for
// above approach
#include
using namespace std;
// Function to find the Kth
// not divisible by N
void kthNonDivisible(int N, int K)
{
// Lowest possible value
int L = 1;
// Highest possible value
int H = INT_MAX;
// To store the Kth non
// divisible number of N
int ans = 0;
// Using binary search
while (L <= H)
{
// Calculating mid value
int mid = (L + H) / 2;
// Sol would have the value
// by subtracting all
// multiples of n till mid
int sol = mid - mid / N;
// Check if sol is greater than k
if (sol > K)
{
// H should be reduced to find
// minimum possible value
H = mid - 1;
}
// Check if sol is less than k
// then L will be mid+1
else if (sol < K)
{
L = mid + 1;
}
// Check if sol is equal to k
else
{
// ans will be mid
ans = mid;
// H would be reduced to find any
// more possible value
H = mid - 1;
}
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
int N = 3;
int K = 7;
// Function Call
kthNonDivisible(N, K);
return 0;
}
Java
// Java implementation for
// above approach
class GFG{
// Function to find the Kth
// not divisible by N
public static void kthNonDivisible(int N,
int K)
{
// Lowest possible value
int L = 1;
// Highest possible value
int H = Integer.MAX_VALUE;
// To store the Kth non
// divisible number of N
int ans = 0;
// Using binary search
while (L <= H)
{
// Calculating mid value
int mid = (L + H) / 2;
// Sol would have the value
// by subtracting all
// multiples of n till mid
int sol = mid - mid / N;
// Check if sol is greater than k
if (sol > K)
{
// H should be reduced to find
// minimum possible value
H = mid - 1;
}
// Check if sol is less than k
// then L will be mid+1
else if (sol < K)
{
L = mid + 1;
}
// Check if sol is equal to k
else
{
// ans will be mid
ans = mid;
// H would be reduced to find any
// more possible value
H = mid - 1;
}
}
// Print the answer
System.out.print(ans);
}
// Driver code
public static void main(String[] args)
{
int N = 3;
int K = 7;
// Function Call
kthNonDivisible(N, K);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 implementation for
# above approach
import sys
# Function to find the Kth
# not divisible by N
def kthNonDivisible(N, K):
# Lowest possible value
L = 1
# Highest possible value
H = sys.maxsize
# To store the Kth non
# divisible number of N
ans = 0
# Using binary search
while (L <= H):
# Calculating mid value
mid = (L + H) // 2
# Sol would have the value
# by subtracting all
# multiples of n till mid
sol = mid - mid // N
# Check if sol is greater than k
if (sol > K):
# H should be reduced to find
# minimum possible value
H = mid - 1
# Check if sol is less than k
# then L will be mid+1
elif (sol < K):
L = mid + 1
# Check if sol is equal to k
else:
# ans will be mid
ans = mid
# H would be reduced to find any
# more possible value
H = mid - 1
# Print the answer
print(ans)
# Driver Code
N = 3
K = 7
# Function call
kthNonDivisible(N, K)
# This code is contributed by ANKITKUMAR34
C#
// C# implementation for
// above approach
using System;
class GFG{
// Function to find the Kth
// not divisible by N
static void kthNonDivisible(int N, int K)
{
// Lowest possible value
int L = 1;
// Highest possible value
int H = Int32.MaxValue;
// To store the Kth non
// divisible number of N
int ans = 0;
// Using binary search
while (L <= H)
{
// Calculating mid value
int mid = (L + H) / 2;
// Sol would have the value
// by subtracting all
// multiples of n till mid
int sol = mid - mid / N;
// Check if sol is greater than k
if (sol > K)
{
// H should be reduced to find
// minimum possible value
H = mid - 1;
}
// Check if sol is less than k
// then L will be mid+1
else if (sol < K)
{
L = mid + 1;
}
// Check if sol is equal to k
else
{
// ans will be mid
ans = mid;
// H would be reduced to find
// any more possible value
H = mid - 1;
}
}
// Print the answer
Console.Write(ans);
}
// Driver code
static void Main()
{
int N = 3;
int K = 7;
// Function Call
kthNonDivisible(N, K);
}
}
// This code is contributed by divyesh072019
C++
// C++ implementation to find
// the K'th non-divisible
// number of N
#include
using namespace std;
// Function to find the Kth
// not divisible by N
int kthNonDivisible(int N, int K)
{
return K + floor((K - 1) / (N - 1));
}
// Driver Code
int main()
{
int N = 3;
int K = 6;
// Function Call
cout << kthNonDivisible(N, K);
return 0;
}
Java
// Java implementation to find the
// K'th non-divisible number of N
class GFG{
// Function to find the Kth
// not divisible by N
static int kthNonDivisible(int N, int K)
{
return (int) (K + Math.floor((K - 1) / (N - 1)));
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
int K = 6;
// Function Call
System.out.print(kthNonDivisible(N, K));
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implementation to find
# the K'th non-divisible
# number of N
import math
# Function to find the Kth
# not divisible by N
def kthNonDivisible(N, K):
return K + math.floor((K - 1) / (N - 1))
# Driver Code
N = 3
K = 6
# Function Call
print(kthNonDivisible(N, K))
# This code is contributed by ishayadav181
C#
// C# implementation to find the
// K'th non-divisible number of N
using System;
class GFG{
// Function to find the Kth
// not divisible by N
static int kthNonDivisible(int N, int K)
{
return (int) (K + Math.Floor((double)(K - 1) /
(N - 1)));
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
int K = 6;
// Function Call
Console.Write(kthNonDivisible(N, K));
}
}
// This code is contributed by amal kumar choubey
Javascript
输出
8
另一种方法–使用二进制搜索该想法是使用二进制搜索来解决此问题。此问题的搜索空间为1到最大整数值,中间值计算为搜索空间的中间值与N的倍数之差。
- 如果中间值大于K,则将H的值更新为middle-1。
- 否则,如果中间值大于K,则将L的值更新为中间– 1。
下面是上述方法的实现:
C++
// C++ implementation for
// above approach
#include
using namespace std;
// Function to find the Kth
// not divisible by N
void kthNonDivisible(int N, int K)
{
// Lowest possible value
int L = 1;
// Highest possible value
int H = INT_MAX;
// To store the Kth non
// divisible number of N
int ans = 0;
// Using binary search
while (L <= H)
{
// Calculating mid value
int mid = (L + H) / 2;
// Sol would have the value
// by subtracting all
// multiples of n till mid
int sol = mid - mid / N;
// Check if sol is greater than k
if (sol > K)
{
// H should be reduced to find
// minimum possible value
H = mid - 1;
}
// Check if sol is less than k
// then L will be mid+1
else if (sol < K)
{
L = mid + 1;
}
// Check if sol is equal to k
else
{
// ans will be mid
ans = mid;
// H would be reduced to find any
// more possible value
H = mid - 1;
}
}
// Print the answer
cout << ans;
}
// Driver Code
int main()
{
int N = 3;
int K = 7;
// Function Call
kthNonDivisible(N, K);
return 0;
}
Java
// Java implementation for
// above approach
class GFG{
// Function to find the Kth
// not divisible by N
public static void kthNonDivisible(int N,
int K)
{
// Lowest possible value
int L = 1;
// Highest possible value
int H = Integer.MAX_VALUE;
// To store the Kth non
// divisible number of N
int ans = 0;
// Using binary search
while (L <= H)
{
// Calculating mid value
int mid = (L + H) / 2;
// Sol would have the value
// by subtracting all
// multiples of n till mid
int sol = mid - mid / N;
// Check if sol is greater than k
if (sol > K)
{
// H should be reduced to find
// minimum possible value
H = mid - 1;
}
// Check if sol is less than k
// then L will be mid+1
else if (sol < K)
{
L = mid + 1;
}
// Check if sol is equal to k
else
{
// ans will be mid
ans = mid;
// H would be reduced to find any
// more possible value
H = mid - 1;
}
}
// Print the answer
System.out.print(ans);
}
// Driver code
public static void main(String[] args)
{
int N = 3;
int K = 7;
// Function Call
kthNonDivisible(N, K);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 implementation for
# above approach
import sys
# Function to find the Kth
# not divisible by N
def kthNonDivisible(N, K):
# Lowest possible value
L = 1
# Highest possible value
H = sys.maxsize
# To store the Kth non
# divisible number of N
ans = 0
# Using binary search
while (L <= H):
# Calculating mid value
mid = (L + H) // 2
# Sol would have the value
# by subtracting all
# multiples of n till mid
sol = mid - mid // N
# Check if sol is greater than k
if (sol > K):
# H should be reduced to find
# minimum possible value
H = mid - 1
# Check if sol is less than k
# then L will be mid+1
elif (sol < K):
L = mid + 1
# Check if sol is equal to k
else:
# ans will be mid
ans = mid
# H would be reduced to find any
# more possible value
H = mid - 1
# Print the answer
print(ans)
# Driver Code
N = 3
K = 7
# Function call
kthNonDivisible(N, K)
# This code is contributed by ANKITKUMAR34
C#
// C# implementation for
// above approach
using System;
class GFG{
// Function to find the Kth
// not divisible by N
static void kthNonDivisible(int N, int K)
{
// Lowest possible value
int L = 1;
// Highest possible value
int H = Int32.MaxValue;
// To store the Kth non
// divisible number of N
int ans = 0;
// Using binary search
while (L <= H)
{
// Calculating mid value
int mid = (L + H) / 2;
// Sol would have the value
// by subtracting all
// multiples of n till mid
int sol = mid - mid / N;
// Check if sol is greater than k
if (sol > K)
{
// H should be reduced to find
// minimum possible value
H = mid - 1;
}
// Check if sol is less than k
// then L will be mid+1
else if (sol < K)
{
L = mid + 1;
}
// Check if sol is equal to k
else
{
// ans will be mid
ans = mid;
// H would be reduced to find
// any more possible value
H = mid - 1;
}
}
// Print the answer
Console.Write(ans);
}
// Driver code
static void Main()
{
int N = 3;
int K = 7;
// Function Call
kthNonDivisible(N, K);
}
}
// This code is contributed by divyesh072019
输出
10
时间复杂度: O(logN)
高效方法:问题中的关键发现是,从1到N-1的每个数字都不能被N整除,类似地,N +1到2 * N – 1也不能被N整除。不被N整除的第K个数将是:
下面是上述方法的实现:
C++
// C++ implementation to find
// the K'th non-divisible
// number of N
#include
using namespace std;
// Function to find the Kth
// not divisible by N
int kthNonDivisible(int N, int K)
{
return K + floor((K - 1) / (N - 1));
}
// Driver Code
int main()
{
int N = 3;
int K = 6;
// Function Call
cout << kthNonDivisible(N, K);
return 0;
}
Java
// Java implementation to find the
// K'th non-divisible number of N
class GFG{
// Function to find the Kth
// not divisible by N
static int kthNonDivisible(int N, int K)
{
return (int) (K + Math.floor((K - 1) / (N - 1)));
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
int K = 6;
// Function Call
System.out.print(kthNonDivisible(N, K));
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implementation to find
# the K'th non-divisible
# number of N
import math
# Function to find the Kth
# not divisible by N
def kthNonDivisible(N, K):
return K + math.floor((K - 1) / (N - 1))
# Driver Code
N = 3
K = 6
# Function Call
print(kthNonDivisible(N, K))
# This code is contributed by ishayadav181
C#
// C# implementation to find the
// K'th non-divisible number of N
using System;
class GFG{
// Function to find the Kth
// not divisible by N
static int kthNonDivisible(int N, int K)
{
return (int) (K + Math.Floor((double)(K - 1) /
(N - 1)));
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
int K = 6;
// Function Call
Console.Write(kthNonDivisible(N, K));
}
}
// This code is contributed by amal kumar choubey
Java脚本
输出
8