📌  相关文章
📜  查找满足给定方程的K值的从0到n的数字计数

📅  最后修改于: 2021-04-29 08:05:11             🧑  作者: Mango

给定三个正整数a,b和n ,我们的任务是找到满足给定方程((k%a)%b)=((k%b)%的从0到n的所有数字K的总数。一种)

例子:

方法:

为了解决上述问题,我们给定条件((k%a)%b)=((k%b)%a)对于从0到max(a,b)– 1的数字将始终满足。因此,根据上面提供的语句,如果我们有<= b,然后检查从0到b-1的所有数字,则有以下两种情况:

  • 我们计算(k%a)%b,在这种情况下,答案将始终为(k%a),因为(k%a)的值始终小于b。
  • 我们计算(k%b)%a,在这种情况下,答案也总是(k%a),因为(k%b)将返回k,因为k小于b。

同样,我们可以检查a> b的情况。因此,现在我们需要检查所有可以被a和b整除的数字,范围是0到n。这可以在a和b的LCM的帮助下找到。因此,现在我们可以通过对LCM进行n乘除,轻松地找到0到n范围内LCM的倍数。我们将1加到倍数以将0包括在内。然后我们必须将倍数乘以max(a,b),以便找到满足给定条件的所有数字。但是,如果最后一个倍数与max(a,b)的总和超出了我们的n个数字的范围,则我们需要排除多余的数字。

下面是上述方法的实现:

C++
// C++ implementation to Find the total 
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K
  
#include 
using namespace std;
  
// Function to find the values
int findAns(int a, int b, int n)
{
    // Calculate the LCM
    int lcm = (a * b) / __gcd(a, b);
  
    // Calculate the multiples of lcm
    int multiples = (n / lcm) + 1;
  
    // Find the values which satisfies 
    // the given condition
    int answer = max(a, b) * multiples;
  
    // Subtract the extra values
    int lastvalue = lcm * (n / lcm) + max(a, b);
  
    if (lastvalue > n)
        answer = answer - (lastvalue - n - 1);
  
    // Return the final result
    return answer;
}
  
// Driver code
int main()
{
    int a = 1, b = 13, n = 500;
  
    cout << findAns(a, b, n) << endl;
}


Java
// Java implementation to find the total 
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K
class GFG{
  
// Function to find the values
static int findAns(int a, int b, int n)
{
    // Calculate the LCM
    int lcm = (a * b) / __gcd(a, b);
  
    // Calculate the multiples of lcm
    int multiples = (n / lcm) + 1;
  
    // Find the values which satisfies 
    // the given condition
    int answer = Math.max(a, b) * multiples;
  
    // Subtract the extra values
    int lastvalue = lcm * (n / lcm) + Math.max(a, b);
    if (lastvalue > n)
        answer = answer - (lastvalue - n - 1);
  
    // Return the final result
    return answer;
}
  
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);     
} 
  
// Driver code
public static void main(String[] args)
{
    int a = 1, b = 13, n = 500;
    System.out.print(findAns(a, b, n) + "\n");
}
}
  
// This code is contributed by Amit Katiyar


Python3
# Python3 implementation to find the total 
# count of all the numbers from 0 to n which
# satisfies the given equation for a value K
  
# Function to find the values
def findAns(a, b, n):
      
    # Calculate the LCM
    lcm = (a * b) // __gcd(a, b);
  
    # Calculate the multiples of lcm
    multiples = (n // lcm) + 1;
  
    # Find the values which satisfies
    # the given condition
    answer = max(a, b) * multiples;
  
    # Subtract the extra values
    lastvalue = lcm * (n // lcm) + max(a, b);
      
    if (lastvalue > n):
        answer = answer - (lastvalue - n - 1);
  
    # Return the final result
    return answer;
  
def __gcd(a, b):
      
    if(b == 0):
        return a;
    else:
        return __gcd(b, a % b);
          
# Driver code
if __name__ == '__main__':
      
    a = 1;
    b = 13;
    n = 500;
      
    print(findAns(a, b, n));
      
# This code is contributed by 29AjayKumar


C#
// C# implementation to find the total 
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K
using System;
  
class GFG{
  
// Function to find the values
static int findAns(int a, int b, int n)
{
    // Calculate the LCM
    int lcm = (a * b) / __gcd(a, b);
  
    // Calculate the multiples of lcm
    int multiples = (n / lcm) + 1;
  
    // Find the values which satisfies 
    // the given condition
    int answer = Math.Max(a, b) * multiples;
  
    // Subtract the extra values
    int lastvalue = lcm * (n / lcm) + Math.Max(a, b);
    if (lastvalue > n)
    {
        answer = answer - (lastvalue - n - 1);
    }
  
    // Return the readonly result
    return answer;
}
  
static int __gcd(int a, int b) 
{ 
    return b == 0 ? a : __gcd(b, a % b);     
} 
  
// Driver code
public static void Main(String[] args)
{
    int a = 1, b = 13, n = 500;
    Console.Write(findAns(a, b, n) + "\n");
}
}
  
// This code is contributed by sapnasingh4991


输出:
501