CF1041B Buying a TV Set

原题链接: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 a and screen height not greater than b b b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w w w, and the height of the screen is h h h, then the following condition should be met: w h = x y \frac{w}{h}=\frac{x}{y} hw=yx.

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w w w and h h h there is a TV set with screen width w w w and height h 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 w and h h h, beforehand, such that ( w ≤ a ) (w≤a) (wa), ( h ≤ b ) (h≤b) (hb) and ( w h = x y ) (\frac{w}{h}=\frac{x}{y}) (hw=yx).

In other words, Monocarp wants to determine the number of TV sets having aspect ratio x y \frac{x}{y} yx, screen width not exceeding a a a, and screen height not exceeding b 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 a, b b b, x , y ( 1 ≤ a , b , x , y ≤ 1 0 18 ) x, y (1≤a,b,x,y≤10^{18}) x,y(1a,b,x,y1018) — 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 3 possible variants: ( 5 , 3 ) , ( 10 , 6 ) , ( 15 , 9 ) (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) (3,2).

题解

先约个分,二分一下倍数即可 A C \mathcal{AC} 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();}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值