codeforces 16 C. Monitor

http://codeforces.com/contest/16/problem/C 题意:给定长宽a,b和分辨率x:y,注意分辨率x:y未必是最简比。问将现有的size裁剪成比例为x:y,使得面积最大的长宽是多少。 思路:可以通过找 x,y能扩大的倍数为k,找到一个最大的k使得k*x<=a&&k;*y<=b。可以二分搞,但其实也可以不用。能扩大的最大的倍数其实就是 min(a/x,b/y). ps:收获了gcd更简单的一种写法。 直接 return b?gcd(b,a%b):a;

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月28日 星期一 22时50分23秒
 4File Name :code/cf/problem/16C.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL a,b,x,y;
34LL ax=0,ay=0;
35LL ans = -1;
36LL GCD;
37LL gcd(LL a,LL b)
38{
39    if (a<b) return gcd(b,a);
40    if (a%b==0) return b;
41    return gcd(b,a%b);
42   // return b?gcd(b,a%b):a;
43}
44
45
46int main()
47{
48	#ifndef  ONLINE_JUDGE 
49	freopen("code/in.txt","r",stdin);
50  #endif
51	cin>>a>>b>>x>>y;
52	GCD = gcd(x,y);
53	x /=GCD;
54	y /=GCD;
55	cout<<x*min(a/x,b/y)<<" "<<y*min(a/x,b/y)<<endl;
56
57  #ifndef ONLINE_JUDGE  
58  fclose(stdin);
59  #endif
60    return 0;
61}
62
63
64
65
66
67
68
69 while ( l != r )
70        {
71            mid = (l+r+1)>>1;
72            if ( x*mid <= a && y*mid <= b ) l = mid;
73            else r = mid-1;
74        }
75        if ( x*l > a || y*l > b ) puts ( "0 0" );
76        else printf ( "%I64d %I64d\n" , l*x , l*y );