Given a range from L to R and every Xth tile is painted black and every Yth tile is painted white in that range from L to R. If a tile is painted both white and black, then it is considered to be painted grey. The task is to find the number of tiles that are colored grey in range L to R (both inclusive).
Input: X = 2, Y = 3, L = 6, R = 18 Output: 3 The grey coloured tiles are numbered 6, 12, 18 Input: X = 1, Y = 4, L = 5, R = 10 Output: 1 The only grey coloured tile is 8.
Approach: Since every multiple of X is black and every multiple of Y is white. Any tile which is a multiple of both X and Y would be grey. The terms that are divisible by both X and Y are the terms that are divisible by the lcm of X and Y.
Lcm can be found out using the following formula:
lcm = (x*y) / gcd(x, y)
GCD can be computed in logn time using Euclid’s algorithm. The number of multiples of lcm in range L to R can be found by using a common trick of:
count(L, R) = count(R) - count(L-1)
Number of terms divisible by K less than N is:
Below is the implementation to find the number of grey tiles:
Time Complexity: O(log(x*y))
- Count common elements in two arrays containing multiples of N and M
- Count common prime factors of two numbers
- Count of numbers having only 1 set bit in the range [0, n]
- Count Odd and Even numbers in a range from L to R
- Count numbers in range 1 to N which are divisible by X but not by Y
- Count the numbers divisible by 'M' in a given range
- Count of numbers from range [L, R] whose sum of digits is Y
- Count factorial numbers in a given range
- Count of numbers from range [L, R] that end with any of the given digits
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count all the numbers in a range with smallest factor as K
- Count numbers from range whose prime factors are only 2 and 3
- Count of Numbers in Range where the number does not contain more than K non zero digits
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.