Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2) A. A Variety of Operations
阿新 • • 發佈:2021-09-03
首先如果 $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; }