[关闭]
@Chilling 2017-01-15T17:15:06.000000Z 字数 1171 阅读 1523

CodeForces-16C: Monitor(GCD)

数论


Description

Reca company makes monitors, the most popular of their models is AB999 with the screen size centimeters. Because of some production peculiarities a screen parameters are integer numbers. Recently the screen sides ratio became popular with users. That's why the company wants to reduce monitor AB999 size so that its screen sides ratio becomes , at the same time they want its total area to be maximal of all possible variants. Your task is to find the screen parameters of the reduced size model, or find out that such a reduction can't be performed.

Input
The first line of the input contains 4 integers — a, b, x and y .

Output

If the answer exists, output 2 positive integers — screen parameters of the reduced size model. Output 0 0 otherwise.

Input

800 600 4 3
1920 1200 16 9
1 1 1 2

Output

800 600
1920 1080
0 0

题意: a和b是屏幕原来的尺寸,x和y是需要裁成的比例。要变成x:y的比例并且保证屏幕最大,输出这个最大的屏幕尺寸,如果不行就输出0。

分析:先把x和y这个比例约分,比如4:2就化成2:1,然后用a/x,b/y,取他们中的最小值,用这个最小值分别乘以最简的那个比例。


  1. #include<stdio.h>
  2. #include<algorithm>
  3. #define LL long long
  4. using namespace std;
  5. LL gcd(LL a,LL b) //辗转相除法求最大公因数
  6. {
  7. LL t;
  8. while(b)
  9. {
  10. t=b;
  11. b=a%b;
  12. a=t;
  13. }
  14. return a;
  15. }
  16. int main()
  17. {
  18. LL a,b,x,y,s;
  19. while(scanf("%lld%lld%lld%lld",&a,&b,&x,&y)!=EOF)
  20. {
  21. s=gcd(x,y);
  22. x/=s,y/=s; //比例先约分,化成最简的
  23. s=min(a/x,b/y);
  24. x*=s;
  25. y*=s;
  26. printf("%lld %lld\n",x,y);
  27. }
  28. return 0;
  29. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注