Codeforces Round #509 (Div. 2) B. Buying a TV Set

  1. 题目链接
  2. 题意:给你一个a,b,x,y。找到宽度不大于a且不大于高度不高于b的满足其比例等于x/y的所有可能的不同的w,h的数量。
  3. 解题思路:找到 x 比 y 的最简比 x’ y’ ,然后找到 n/x’ 和 m/y’中的较小的值。
  4. AC code:
#include
#include
using namespace std;
typedef long long ll;
ll n,m,x,y,s1,s2,ans;
ll gcd(ll m,ll n){
    while(m>0){ll c=n%m;n=m;m=c;}
    return n;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n>>m>>x>>y;
    ll tmp=gcd(x,y);
    x/=tmp;y/=tmp;
    ans=min(n/x,m/y);
    cout<

我的代码比较长,下面有个短的。__gcd()函数的头文件是algorithm

#include
#include
using namespace std;
int main(){
    long long a,b,x,y,g;
    cin>>a>>b>>x>>y;
    g=__gcd(x,y);cout<x/=g;y/=g;
    cout<);
} 

你可能感兴趣的:(codeforces)