Related Articles

# Count pairs (i,j) such that (i+j) is divisible by A and B both

• Last Updated : 23 May, 2021

Given n, m, A and B. The task is to count the number of pairs of integers (x, y) such that 1  n and 1  m and (x+y) mod A and (x+y) mod B both equals to 0.
Examples:

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

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

Approach: If (x+y) is divisible by both A and B then basically LCM of A and B is the smallest divisor of (x+y). So we calculate all numbers that is less than or equal to m and divisible by LCM of them and when iterating with the loop then we check if the present number is divisible by LCM of A and B.
Below is the implementation of the above approach:

## C++

 // C++ implementation of above approach#include using namespace std; // Function to find the LCMint find_LCM(int x, int y){    return (x * y) / __gcd(x, y);} // Function to count the pairsint 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 codeint main(){    int n = 60, m = 90, A = 5, B = 10;     cout << CountPairs(n, m, A, B);     return 0;}

## Java

 //Java implementation of above approachimport 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 methodfrom math import gcd # Function to find the LCMdef find_LCM(x, y) :     return (x * y) // gcd(x, y) # Function to count the pairsdef 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 approachusing 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

 

## Javascript

 
Output:
540

Time Complexity: O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up