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 \neq 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\n"); continue; }
        if(a==b) { printf("1\n"); continue; }
        if((a+b)&1) printf("-1\n") ;
        else printf("2\n");
    }
    return 0;
}

 

posted @ 2021-09-03 12:11  LLTYYC  阅读(45)  评论(0编辑  收藏  举报