cf1556A. A Variety of Operations
题意:
有两个数a,b一开始都是0,现在有三种操作:
- 给a和b都加k
- a加k,b减k
- a减k,b加k
问从a=0,b=0到a=c,b=d最少需要几步?
题解:
k=(c+d)/2
我们可以先用第一种操作将0,0都加到k,k.然后一个减,一个加,实现到c,d,这样需要两步
如果c和d的差为奇数,则无法通过操作实现
代码:
// Problem: A. A Variety of Operations
// Contest: Codeforces - Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2)
// URL: https://codeforces.com/contest/1556/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Data:2021-08-31 23:35:41
// By Jozky#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{
#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{
#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
int main()
{//rd_test();int t;read(t);while (t--) {int c, d;read(c, d);if (c > d)swap(c, d);int w= d - c;if (d == 0 && c == 0)printf("0\n");else if (w == 0)printf("1\n");else {if (w % 2 == 1)printf("-1\n");elseprintf("2\n");}}return 0;//Time_test();
}