1. 程式人生 > >B. Buying a TV Set

B. Buying a TV Set

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 aa and screen height not greater than bb. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is ww, and the height of the screen is hh, then the following condition should be met: wh=xywh=xy.

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers ww and hh there is a TV set with screen width ww and height hh 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 ww and hh, beforehand, such that (w≤a)(w≤a), (h≤b)(h≤b) and (wh=xy)(wh=xy).

In other words, Monocarp wants to determine the number of TV sets having aspect ratio xyxy, screen width not exceeding aa, and screen height not exceeding bb. Two TV sets are considered different if they have different screen width or different screen height.

Input

The first line contains four integers aa, bb, xx, yy (1≤a,b,x,y≤10181≤a,b,x,y≤1018) — 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

Copy

17 15 5 3

output

Copy

3

input

Copy

14 16 7 22

output

Copy

0

input

Copy

4 2 6 4

output

Copy

1

input

Copy

1000000000000000000 1000000000000000000 999999866000004473 999999822000007597

output

Copy

1000000063

Note

In the first example, there are 33 possible variants: (5,3)(5,3), (10,6)(10,6), (15,9)(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並且高不超過b的電視機。

現在Monocarp想買一臺電視機,但是他要求這臺電視級的寬高比等於一個給定的分數\frac{x}{y},求有多少種方案。

一句話題意:給你a,b,x,y,求滿足\frac{w}{h}=\frac{x}{y}(w,h)對數,其中1\leq w\leq a1\leq h\leq b

題解:

顯然面對10^{18}範圍內的a,b,不能列舉。

我們考慮,將\frac{x}{y}化簡為最簡分數\frac{q}{p},則有\frac{w}{h}=\frac{q}{p},那麼w=kq,h=kpk為正整數,1\leq w\leq a1\leq h\leq a

考慮到w\left \lfloor \frac{a}{q} \right \rfloor種取值,h\left \lfloor \frac{b}{p} \right \rfloor種取值,那麼答案為min(\left \lfloor \frac{a}{q} \right \rfloor,\left \lfloor \frac{b}{p} \right \rfloor)

時間複雜度:O(log(min(x,y)))(求gcd所用時間)

程式碼:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,x,y,t;
    cin>>a>>b>>x>>y;
    t=__gcd(x,y);
    x=x/t;
    y=y/t;
    long long int ans;
        a=a/x;
        b=b/y;
        ans=min(a,b);
    cout<<ans<<endl;
    return 0;
}