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.
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:
Time Complexity: O(n)
- 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]
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Count pairs in array whose sum is divisible by K
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Print k numbers where all pairs are divisible by m
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count pairs with Odd XOR
- Count rotations divisible by 8
- Count rotations divisible by 4
- Count of pairs (x, y) in an array such that x < y
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Count Pairs from two arrays with even sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.