给定从L到R的范围,并且在从L到R的范围内,每个Xth磁贴都被涂成黑色,并且每个Yth磁贴都被涂成白色。如果一个磁贴同时被涂成白色和黑色,则认为它被涂成灰色。任务是找到在L到R(包括两端)范围内呈灰色显示的图块数量。
例子:
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.
方法:由于X的每个倍数都是黑色,Y的每个倍数都是白色。 X和Y的倍数的任何图块都是灰色的。可被X和Y整除的术语是可被X和Y的Icm整除的术语。
Lcm可以使用以下公式求出:
lcm = (x*y) / gcd(x, y)
可以使用Euclid算法在登录时间计算GCD。可以使用以下常见技巧找到L到R范围内的1cm的倍数:
count(L, R) = count(R) - count(L-1)
被K除以N的项数为:
floor(N/K)
以下是查找灰色图块数量的实现:
C++
// C++ implementation to find the number of
// grey tiles
#include
using namespace std;
// Function to count the numbe ro fgrey tiles
int findTileCount(int x, int y, int l, int r)
{
int lcm = (x * y) / __gcd(x, y);
// Number multiple of lcm less than L
int countl = (l - 1) / lcm;
// Number of multiples of lcm less than R+1
int countr = r / lcm;
return countr - countl;
}
// Driver code
int main()
{
int x = 2, y = 3, l = 6, r = 18;
cout << findTileCount(x, y, l, r);
return 0;
}
Java
// Java implementation to find the
// number of grey tiles
import java.io.*;
class GFG {
// Function to count the number
// of grey tiles
static int findTileCount(int x, int y,
int l, int r)
{
int lcm = (x * y) / __gcd(x, y);
// Number multiple of lcm less than L
int countl = (l - 1) / lcm;
// Number of multiples of
// lcm less than R+1
int countr = r / lcm;
return countr - countl;
}
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Driver code
public static void main (String[] args) {
int x = 2, y = 3, l = 6, r = 18;
System.out.println(findTileCount(x, y, l, r));
}
}
// This code is contributed ajit
Python3
# Python3 implementation to find the number of
# grey tiles
# from math lib import gcd method
from math import gcd
# Function to count the numbe of grey tiles
def findTileCount(x, y, l, r) :
lcm = (x * y) // gcd(x, y)
# Number multiple of lcm less than L
count1 = (l - 1) // lcm
# Number of multiples of lcm less than R+1
countr = r // lcm
return countr - count1
# Driver code
if __name__ == "__main__" :
x, y, l, r = 2, 3, 6, 18
print(findTileCount(x, y, l, r))
# This code is contributed by
# ANKITRAI1
C#
// C# implementation to find the
// number of grey tiles
using System;
class GFG
{
// Function to count the number
// of grey tiles
static int findTileCount(int x, int y,
int l, int r)
{
int lcm = (x * y) / __gcd(x, y);
// Number multiple of lcm less than L
int countl = (l - 1) / lcm;
// Number of multiples of
// lcm less than R+1
int countr = r / lcm;
return countr - countl;
}
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Driver code
public static void Main()
{
int x = 2, y = 3, l = 6, r = 18;
Console.Write(findTileCount(x, y, l, r));
}
}
// This code is contributed
// by Kirti_Mangal
PHP
$b)
return __gcd($a - $b, $b);
return __gcd($a, $b - $a);
}
// Driver code
$x = 2; $y = 3; $l = 6; $r = 18;
echo findTileCount($x, $y, $l, $r);
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>
Javascript
输出:
3
时间复杂度: O(log(x * y))