📌  相关文章
📜  计算(i,j)对,使得(i + j)可被A和B整除

📅  最后修改于: 2021-05-08 18:42:37             🧑  作者: Mango

给定n,m,A和B。任务是计算整数对(x,y)的数量,使得1 \leq X \leq n和1 \leq ÿ \leq m和(x + y)mod A和(x + y)mod B都等于0。

例子:

Input: n = 60, m = 90, A = 5, B = 10
Output: 540

Input: n = 225, m = 452, A = 10, B = 15
Output: 3389

方法:如果(x + y)可被A和B整除,则基本上A和B的LCM是(x + y)的最小除数。因此,我们计算所有小于或等于m并被它们的LCM整除的数字,并在循环迭代时检查当前数字是否可被A和B的LCM整除。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to find the LCM
int find_LCM(int x, int y)
{
    return (x * y) / __gcd(x, y);
}
  
// Function to count the pairs
int CountPairs(int n, int m, int A, int B)
{
    int cnt = 0;
    int lcm = find_LCM(A, B);
  
    for (int i = 1; i <= n; i++)
        cnt += (m + (i % lcm)) / lcm;
  
    return cnt;
}
  
// Driver code
int main()
{
    int n = 60, m = 90, A = 5, B = 10;
  
    cout << CountPairs(n, m, A, B);
  
    return 0;
}


Java
//Java implementation of above approach
import java.util.*;
public class ACE {
  
    static int gcd(int a,int b)
    {
        return b==0 ? a :gcd(b,a%b);
    }
      
    //Function to find the LCM
    static int find_LCM(int x, int y)
    {
     return (x * y) / gcd(x, y);
    }
  
    //Function to count the pairs
    static int CountPairs(int n, int m, int A, int B)
    {
     int cnt = 0;
     int lcm = find_LCM(A, B);
  
     for (int i = 1; i <= n; i++)
         cnt += (m + (i % lcm)) / lcm;
  
     return cnt;
    }
  
    //Driver code
    public static void main(String[] args) {
          
        int n = 60, m = 90, A = 5, B = 10;
  
        System.out.println(CountPairs(n, m, A, B));
  
    }
  
}


Python 3
# Python3 implementation of
# above approach
  
# from math lib import gcd method
from math import gcd
  
# Function to find the LCM 
def find_LCM(x, y) :
  
    return (x * y) // gcd(x, y)
  
# Function to count the pairs 
def CountPairs(n, m, A, B) :
  
    cnt = 0
    lcm = find_LCM(A, B)
  
    for i in range(1, n + 1) :
        cnt += (m + (i % lcm)) // lcm
  
    return cnt
  
# Driver code     
if __name__ == "__main__" :
  
    n, m, A, B = 60, 90, 5, 10
  
    print(CountPairs(n, m, A, B))
  
# This code is contributed
# by ANKITRAI1


C#
// C# implementation of above approach
using System;
  
class GFG
{
    static int gcd(int a,int b)
    {
        return b == 0 ? a : gcd(b, a % b);
    }
      
    // Function to find the LCM
    static int find_LCM(int x, int y)
    {
    return (x * y) / gcd(x, y);
    }
  
    //Function to count the pairs
    static int CountPairs(int n, int m,
                          int A, int B)
    {
        int cnt = 0;
        int lcm = find_LCM(A, B);
      
        for (int i = 1; i <= n; i++)
            cnt += (m + (i % lcm)) / lcm;
      
        return cnt;
    }
  
    // Driver code
    public static void Main() 
    {
        int n = 60, m = 90, A = 5, B = 10;
  
        Console.WriteLine(CountPairs(n, m, A, B));
    }
}
  
// This Code is contributed by mits


PHP


输出:
540

时间复杂度: O(n)