给定四个整数A 、 N 、 L和R ,任务是在从L到R的连续整数序列中找到第N个不是A的倍数。假设序列至少包含N 个不能被A整除的数字,并且整数A总是大于 1。
例子:
Input: A = 2, N = 3, L = 1, R = 10
Output: 5
Explanation:
The sequence is 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. Here 5 is the third number which is not a multiple of 2 in the sequence.
Input: A = 3, N = 6, L = 4, R = 20
Output: 11
Explanation :
11 is the 6th number which is not a multiple of 3 in the sequence.
朴素的方法:朴素的方法是在循环中遍历范围[L, R]以找到第N个数字。步骤是:
- 将非倍数和当前数的计数初始化为 0。
- 迭代范围[L, R]直到非倍数的计数不等于N 。
- 如果当前数字不能被 A 整除,则将非倍数的计数加 1。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find Nth number not a
// multiple of A in the range [L, R]
void count_no (int A, int N, int L, int R)
{
// To store the count
int count = 0;
int i = 0;
// To check all the nos in range
for(i = L; i < R + 1; i++)
{
if (i % A != 0)
count += 1;
if (count == N)
break;
}
cout << i;
}
// Driver code
int main()
{
// Given values of A, N, L, R
int A = 3, N = 6, L = 4, R = 20;
// Function Call
count_no (A, N, L, R);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.util.*;
import java.io.*;
class GFG{
// Function to find Nth number not a
// multiple of A in the range [L, R]
static void count_no (int A, int N,
int L, int R)
{
// To store the count
int count = 0;
int i = 0;
// To check all the nos in range
for(i = L; i < R + 1; i++)
{
if (i % A != 0)
count += 1;
if (count == N)
break;
}
System.out.println(i);
}
// Driver code
public static void main(String[] args)
{
// Given values of A, N, L, R
int A = 3, N = 6, L = 4, R = 20;
// Function call
count_no (A, N, L, R);
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to find Nth number not a
# multiple of A in the range [L, R]
def count_no (A, N, L, R):
# To store the count
count = 0
# To check all the nos in range
for i in range ( L, R + 1 ):
if ( i % A != 0 ):
count += 1
if ( count == N ):
break
print ( i )
# Given values of A, N, L, R
A, N, L, R = 3, 6, 4, 20
# Function Call
count_no (A, N, L, R)
C#
// C# program for the above approach
using System;
class GFG{
// Function to find Nth number not a
// multiple of A in the range [L, R]
static void count_no (int A, int N,
int L, int R)
{
// To store the count
int count = 0;
int i = 0;
// To check all the nos in range
for(i = L; i < R + 1; i++)
{
if (i % A != 0)
count += 1;
if (count == N)
break;
}
Console.WriteLine(i);
}
// Driver code
public static void Main()
{
// Given values of A, N, L, R
int A = 3, N = 6, L = 4, R = 20;
// Function call
count_no (A, N, L, R);
}
}
// This code is contributed by sanjoy_62
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find Nth number
// not a multiple of A in range [L, R]
void countNo(int A, int N, int L, int R)
{
// Calculate the Nth no
int ans = L - 1 + N + floor((N - 1) /
(A - 1));
// Check for the edge case
if (ans % A == 0)
{
ans = ans + 1;
}
cout << ans << endl;
}
// Driver Code
int main()
{
// Input parameters
int A = 5, N = 10, L = 4, R = 20;
// Function Call
countNo(A, N, L, R);
return 0;
}
// This code is contributed by avanitrachhadiya2155
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find Nth number
// not a multiple of A in range [L, R]
static void countNo(int A, int N, int L, int R)
{
// Calculate the Nth no
int ans = L - 1 + N + (int)Math.floor((N - 1) / (A - 1));
// Check for the edge case
if (ans % A == 0)
{
ans = ans + 1;
}
System.out.println(ans);
}
// Driver Code
public static void main (String[] args)
{
// Input parameters
int A = 5, N = 10, L = 4, R = 20;
// Function Call
countNo(A, N, L, R);
}
}
// This code is contributed by rag2127
Python3
# Python3 program for the above approach
import math
# Function to find Nth number
# not a multiple of A in range [L, R]
def countNo (A, N, L, R):
# Calculate the Nth no
ans = L - 1 + N \
+ math.floor( ( N - 1 ) / ( A - 1 ) )
# Check for the edge case
if ans % A == 0:
ans = ans + 1;
print(ans)
# Input parameters
A, N, L, R = 5, 10, 4, 20
# Function Call
countNo(A, N, L, R)
C#
// C# program for the above approach
using System;
class GFG {
// Function to find Nth number
// not a multiple of A in range [L, R]
static void countNo(int A, int N, int L, int R)
{
// Calculate the Nth no
int ans = L - 1 + N + ((N - 1) / (A - 1));
// Check for the edge case
if (ans % A == 0)
{
ans = ans + 1;
}
Console.WriteLine(ans);
}
// Driver code
static void Main()
{
// Input parameters
int A = 5, N = 10, L = 4, R = 20;
// Function Call
countNo(A, N, L, R);
}
}
// This code is contributed by divyesh072019.
Javascript
11
时间复杂度: O(R – L)
辅助空间: O(1)
有效的方法:
关键的观察是在范围[1, A – 1]中有A – 1 个不能被A整除的数字。同样,在[A + 1, 2 * A – 1], [2 * A + 1, 3 * A – 1]等范围内有A – 1 个不能被A整除的数字。
在这个观察的帮助下,不能被A整除的第N个数字将是:
要找到范围 [ L, R ] 中的值,我们需要将原点从 ‘0’ 移到 ‘L – 1’,因此我们可以说该范围内不能被 A 整除的第N个数字将是:
然而,有一个边缘情况,当( L – 1 ) + N + floor( ( N – 1 ) / ( A – 1 ) )本身的值是‘A’ 的倍数时,在这种情况下是第 N 个数字将 :
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find Nth number
// not a multiple of A in range [L, R]
void countNo(int A, int N, int L, int R)
{
// Calculate the Nth no
int ans = L - 1 + N + floor((N - 1) /
(A - 1));
// Check for the edge case
if (ans % A == 0)
{
ans = ans + 1;
}
cout << ans << endl;
}
// Driver Code
int main()
{
// Input parameters
int A = 5, N = 10, L = 4, R = 20;
// Function Call
countNo(A, N, L, R);
return 0;
}
// This code is contributed by avanitrachhadiya2155
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find Nth number
// not a multiple of A in range [L, R]
static void countNo(int A, int N, int L, int R)
{
// Calculate the Nth no
int ans = L - 1 + N + (int)Math.floor((N - 1) / (A - 1));
// Check for the edge case
if (ans % A == 0)
{
ans = ans + 1;
}
System.out.println(ans);
}
// Driver Code
public static void main (String[] args)
{
// Input parameters
int A = 5, N = 10, L = 4, R = 20;
// Function Call
countNo(A, N, L, R);
}
}
// This code is contributed by rag2127
蟒蛇3
# Python3 program for the above approach
import math
# Function to find Nth number
# not a multiple of A in range [L, R]
def countNo (A, N, L, R):
# Calculate the Nth no
ans = L - 1 + N \
+ math.floor( ( N - 1 ) / ( A - 1 ) )
# Check for the edge case
if ans % A == 0:
ans = ans + 1;
print(ans)
# Input parameters
A, N, L, R = 5, 10, 4, 20
# Function Call
countNo(A, N, L, R)
C#
// C# program for the above approach
using System;
class GFG {
// Function to find Nth number
// not a multiple of A in range [L, R]
static void countNo(int A, int N, int L, int R)
{
// Calculate the Nth no
int ans = L - 1 + N + ((N - 1) / (A - 1));
// Check for the edge case
if (ans % A == 0)
{
ans = ans + 1;
}
Console.WriteLine(ans);
}
// Driver code
static void Main()
{
// Input parameters
int A = 5, N = 10, L = 4, R = 20;
// Function Call
countNo(A, N, L, R);
}
}
// This code is contributed by divyesh072019.
Javascript
16
时间复杂度: O(1)
辅助空间: O(1)