Count pairs (i,j) such that (i+j) is divisible by A and B both
Last Updated :
26 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int find_LCM( int x, int y)
{
return (x * y) / __gcd(x, y);
}
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;
}
int main()
{
int n = 60, m = 90, A = 5, B = 10;
cout << CountPairs(n, m, A, B);
return 0;
}
|
Java
import java.util.*;
public class ACE {
static int gcd( int a, int b)
{
return b== 0 ? a :gcd(b,a%b);
}
static int find_LCM( int x, int y)
{
return (x * y) / gcd(x, y);
}
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;
}
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
from math import gcd
def find_LCM(x, y) :
return (x * y) / / gcd(x, y)
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
if __name__ = = "__main__" :
n, m, A, B = 60 , 90 , 5 , 10
print (CountPairs(n, m, A, B))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
static int find_LCM( int x, int y)
{
return (x * y) / gcd(x, y);
}
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;
}
public static void Main()
{
int n = 60, m = 90, A = 5, B = 10;
Console.WriteLine(CountPairs(n, m, A, B));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
return $b == 0 ? $a : gcd( $b , $a % $b );
}
function find_LCM( $x , $y )
{
return (int)(( $x * $y ) / gcd( $x , $y ));
}
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 ;
}
$n = 60; $m = 90; $A = 5; $B = 10;
echo CountPairs( $n , $m , $A , $B );
?>
|
Javascript
<script>
function gcd(a,b)
{
return b==0 ? a :gcd(b,a%b);
}
function find_LCM(x,y)
{
return Math.floor((x * y) / gcd(x, y));
}
function CountPairs(n,m,A,B)
{
let cnt = 0;
let lcm = find_LCM(A, B);
for (let i = 1; i <= n; i++)
cnt += Math.floor((m + (i % lcm)) / lcm);
return cnt;
}
let n = 60, m = 90, A = 5, B = 10;
document.write(CountPairs(n, m, A, B));
</script>
|
Time Complexity: O(n) for iterating from 1 till n.
Auxiliary Space: O(1) as no extra space is used.
Share your thoughts in the comments
Please Login to comment...