CSES – Coin Piles

二分搜

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
int t;
//
bool check(int a,int b){
    if(a > b) swap(a,b);
    if(a*2 > b) return 1;
    return 0;
}
bool equal(int a,int b){
    if(a > b) swap(a,b);
    return a*2 == b;
}
void solve(){
    int a,b; cin>>a>>b;
    if(a > b) swap(a,b);
    if(a*2 == b) return void(cout<<"YES\n");
    int L = 0, R = 1e9;
    while(R-L > 1){
        int m = (L+R) / 2;
        if(a > b) swap(a,b);
        if(equal(a-m,b-m*2)) return void(cout<<"YES\n");
        if(check(a-m,b-m*2)) L = m;
        else R = m;
    }
    cout<<"NO\n";
}
signed main(){
    fastio;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

相關文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *