zoukankan      html  css  js  c++  java
  • Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2) A. A Variety of Operations

    传送门

    首先如果 $c$,$d$ 的和为奇数则无解,因为三个操作必定使 $a$,$b$ 的和保持偶数

    考虑 $cd$ 和为偶数的情况下的最好的操作

    首先如果 $c=d$ 则一步到位

    如果 $c=d=0$ 甚至不用操作

    剩下的情况,设 $c$,$d$ 的平均数为 $k$,因为 $c+d$ 为偶数且 $c eq d$,则 $k$ 为整数且 $k$ 在 $c$,$d$ 中间

    两步即可操作完:

    1. 将 $a,b$ 同时加 $k$

    2. 若 $c$<$d$ 则将 $a$ 减到 $c$ ,$b$ 加到 $d$ ,显然 $a-c=d-b$;反之同理

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    const int N=2e5+7;
    inline int read()
    {
        int x=0,f=1; char ch=getchar();
        while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
        while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
        return x*f;
    }
    int t,a,b;
    int main()
    {
        cin>>t;
        while(t--)
        {
            a=read(),b=read();
            if(a==0&&b==0) { printf("0
    "); continue; }
            if(a==b) { printf("1
    "); continue; }
            if((a+b)&1) printf("-1
    ") ;
            else printf("2
    ");
        }
        return 0;
    }
  • 相关阅读:
    欧拉函数的一个性质及其证明
    【机器人M号】题解
    【求和】题解
    uva11292贪心基础题目
    hdu 1009 贪心基础题
    近期计划,理清思路,大步向前
    hdu1712 分组背包
    TOJ3596 二维背包
    hdu1114 完全背包
    BestCoder Round #81 (div.2)1001
  • 原文地址:https://www.cnblogs.com/LLTYYC/p/15222556.html
Copyright © 2011-2022 走看看