计算小于 N 的数,其与 A 的模数等于 B
给定三个非负整数A 、 B和N ,其中A非零,任务是找到小于或等于N且与A取模后得到值B的整数个数。
例子:
Input: A = 6, B = 3, N = 15
Output: 3
Explanation: The numbers 3, 9 and 15 are less than or equal to N (= 15) and their modulo with A (= 6) is equal to B (= 3). Therefore, the count such numbers is 3.
Input: A = 4, B = 1, C = 8
Output: 2
方法:给定的问题可以通过使用基于数学的观察来解决。请按照以下步骤解决问题:
- 如果B的值至少为 A ,则打印0 ,因为不可能有任何这样的数字与A的模得到值B 。
- 否则,如果B的值为0 ,则打印C / A的值作为与A模数得到值B的此类数字的计数。
- 否则,请执行以下步骤:
- 用C / A的底值初始化一个变量,比如 ans。
- 如果(ans * A + B)的值小于或等于N ,则将ans的值增加1 。
- 完成上述步骤后,将ans的值打印为与A模数为B的数字的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count numbers less than
// N, whose modulo with A gives B
void countValues(int A, int B, int C)
{
// If the value of B at least A
if (B >= A) {
cout << 0;
return;
}
// If the value of B is 0 or not
if (B == 0) {
cout << C / A;
return;
}
// Stores the resultant count
// of numbers less than N
int ans = C / A;
if (ans * A + B <= C) {
// Update the value of ans
ans++;
}
// Print the value of ans
cout << ans;
}
// Driver Code
int main()
{
int A = 6, B = 3, N = 15;
countValues(A, B, N);
return 0;
}
Java
// Java program for the above approach
public class MyClass
{
// Function to count numbers less than
// N, whose modulo with A gives B
static void countValues(int A, int B, int C)
{
// If the value of B at least A
if (B >= A) {
System.out.println(0);
return;
}
// If the value of B is 0 or not
if (B == 0) {
System.out.println(C / A);
return;
}
// Stores the resultant count
// of numbers less than N
int ans = C / A;
if (ans * A + B <= C) {
// Update the value of ans
ans++;
}
// Print the value of ans
System.out.println(ans);
}
// Driver Code
public static void main(String args[])
{
int A = 6, B = 3, N = 15;
countValues(A, B, N);
}
}
// This code in contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to count numbers less than
# N, whose modulo with A gives B
def countValues(A, B, C):
# If the value of B at least A
if (B >= A):
print(0)
return
# If the value of B is 0 or not
if (B == 0):
print(C // A)
return
# Stores the resultant count
# of numbers less than N
ans = C//A
if (ans * A + B <= C):
# Update the value of ans
ans += 1
# Print the value of ans
print(ans)
# Driver Code
if __name__ == '__main__':
A = 6
B = 3
N = 15
countValues(A, B, N)
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count numbers less than
// N, whose modulo with A gives B
static void countValues(int A, int B, int C)
{
// If the value of B at least A
if (B >= A)
{
Console.Write(0);
return;
}
// If the value of B is 0 or not
if (B == 0)
{
Console.Write(C / A);
return;
}
// Stores the resultant count
// of numbers less than N
int ans = C / A;
if (ans * A + B <= C)
{
// Update the value of ans
ans++;
}
// Print the value of ans
Console.Write(ans);
}
// Driver code
public static void Main()
{
int A = 6, B = 3, N = 15;
countValues(A, B, N);
}
}
// This code is contributed by sanjoy_62
Javascript
输出:
3
时间复杂度: O(1)
辅助空间: O(1)