给定四个整数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整除的A – 1个数字,依此类推。
借助此观察,无法被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
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.
Java脚本
16
时间复杂度: O(1)
辅助空间: O(1)