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;

/* ***********************************************
Author :111qqz
Created Time :2015年12月28日 星期一 22时50分23秒
File Name :code/cf/problem/16C.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6LL a,b,x,y;
 7LL ax=0,ay=0;
 8LL ans = -1;
 9LL GCD;
10LL gcd(LL a,LL b)
11{
12    if (a<b) return gcd(b,a);
13    if (a%b==0) return b;
14    return gcd(b,a%b);
15   // return b?gcd(b,a%b):a;
16}
 1int main()
 2{
 3	#ifndef  ONLINE_JUDGE 
 4	freopen("code/in.txt","r",stdin);
 5  #endif
 6	cin>>a>>b>>x>>y;
 7	GCD = gcd(x,y);
 8	x /=GCD;
 9	y /=GCD;
10	cout<<x*min(a/x,b/y)<<" "<<y*min(a/x,b/y)<<endl;
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}
1 while ( l != r )
2        {
3            mid = (l+r+1)>>1;
4            if ( x*mid <= a && y*mid <= b ) l = mid;
5            else r = mid-1;
6        }
7        if ( x*l > a || y*l > b ) puts ( "0 0" );
8        else printf ( "%I64d %I64d\n" , l*x , l*y );