CF1041B Buying a TV Set

版权声明:大佬您能赏脸,蒟蒻倍感荣幸,还请联系我让我好好膜拜。 https://blog.csdn.net/ShadyPi/article/details/82749661

原题链接:http://codeforces.com/contest/1041/problem/B

Buying a TV Set

Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a a and screen height not greater than b b . Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w w , and the height of the screen is h h , then the following condition should be met: w h = x y \frac{w}{h}=\frac{x}{y} .

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w w and h h there is a TV set with screen width w w and height h h in the shop.

Monocarp isn’t ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w w and h h , beforehand, such that ( w a ) (w≤a) , ( h b ) (h≤b) and ( w h = x y ) (\frac{w}{h}=\frac{x}{y}) .

In other words, Monocarp wants to determine the number of TV sets having aspect ratio x y \frac{x}{y} , screen width not exceeding a a , and screen height not exceeding b b . Two TV sets are considered different if they have different screen width or different screen height.

Input

The first line contains four integers a a , b b , x , y ( 1 a , b , x , y 1 0 18 ) x, y (1≤a,b,x,y≤10^{18}) — the constraints on the screen width and height, and on the aspect ratio.

Output

Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.

Examples
input

17 15 5 3

output

3

input

14 16 7 22

output

0

input

4 2 6 4

output

1

input

1000000000000000000 1000000000000000000 999999866000004473 999999822000007597

output

1000000063

Note

In the first example, there are 3 3 possible variants: ( 5 , 3 ) , ( 10 , 6 ) , ( 15 , 9 ) (5,3), (10,6), (15,9) .

In the second example, there is no TV set meeting the constraints.

In the third example, there is only one variant: ( 3 , 2 ) (3,2) .

题解

先约个分,二分一下倍数即可 A C \mathcal{AC}

代码
#include<bits/stdc++.h>
using namespace std;
long long a,b,x,y,le=1,ri,mid,g;
void in(){scanf("%I64d%I64d%I64d%I64d",&a,&b,&x,&y);}
void ac()
{
	g=__gcd(x,y),x/=g,y/=g;
	ri=min(a/x+1,b/y+1);
	while(le^ri)mid=le+ri>>1,x*mid<=a&&y*mid<=b?le=mid+1:ri=mid;
	printf("%I64d",mid);
}
int main(){in();ac();}

猜你喜欢

转载自blog.csdn.net/ShadyPi/article/details/82749661