Given n, m, A and B. The task is to count the number of pairs of integers (x, y) such that 1 x n and 1 y 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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `return` `$b` `== 0 ? ` `$a` `: gcd(` `$b` `, ` `$a` `% ` `$b` `); ` `} ` ` ` `// Function to find the LCM ` `function` `find_LCM(` `$x` `, ` `$y` `) ` `{ ` ` ` `return` `(int)((` `$x` `* ` `$y` `) / gcd(` `$x` `, ` `$y` `)); ` `} ` ` ` `// Function to count the pairs ` `function` `CountPairs(` `$n` `, ` `$m` `, ` `$A` `, ` `$B` `) ` `{ ` ` ` `$cnt` `= 0; ` ` ` `$lcm` `= find_LCM(` `$A` `, ` `$B` `); ` ` ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$cnt` `+= (int)((` `$m` `+ (` `$i` `% ` `$lcm` `)) / ` ` ` `$lcm` `); ` ` ` ` ` `return` `$cnt` `; ` `} ` ` ` `// Driver code ` `$n` `= 60; ` `$m` `= 90; ` `$A` `= 5; ` `$B` `= 10; ` `echo` `CountPairs(` `$n` `, ` `$m` `, ` `$A` `, ` `$B` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Choose points from two ranges such that no point lies in both the ranges
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count numbers upto N which are both perfect square and perfect cube
- Count subsequences which contains both the maximum and minimum array element
- Ways to form n/2 pairs such that difference of pairs is minimum
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count number of pairs in array having sum divisible by K | SET 2
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count pairs in array whose sum is divisible by K
- Count of pairs of Array elements which are divisible by K when concatenated
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count the pairs in an array such that the difference between them and their indices is equal
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.