zoukankan      html  css  js  c++  java
  • 非常可乐

    题意:

    给出一瓶可乐(S毫升)和两个不同容积的杯子(a,b毫升),可以相互倒可乐,要求两人喝掉相同的可乐,如果能平分则输出最少次数,否则输出“NO”。

    分析:

    这题表面看上去是个bfs枚举所有状态暴搜即可,但是细想发现这是个数论题,设g=gcd(a,b),则如果S/g%2!=0,说明无论怎样倒可乐都不可能平分。若有解,则必然存在(a/g)x+(b/g)y=(a+b)/2g。解不定方程即可。

    #include <iostream>
    #include <string>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #define range(i,a,b) for(int i=a;i<=b;++i)
    #define LL long long
    #define rerange(i,a,b) for(int i=a;i>=b;--i)
    #define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
    using namespace std;
    int n,a,b;
    void init(){
    
    }
    int gcd(int a,int b){
        return b?gcd(b,a%b):a;
    }
    void solve(){
        while(cin>>n>>a>>b,n,a,b){
            n/=gcd(a,b);
            if(n&1)cout<<"NO"<<endl;
            else cout<<n-1<<endl;
        }
    }
    int main() {
        init();
        solve();
        return 0;
    }
    View Code
  • 相关阅读:
    HDU1316 fib+高精度
    HDU1868
    HDU2586 LCA
    HDU1113 字符串处理
    HDU1115 几何+多边形重心
    HDU1124
    HDU1110 几何
    HDU1103
    HDU2670 DP
    linux 下查看机器是cpu是几核的
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9323443.html
Copyright © 2011-2022 走看看