fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1047-C (Div. 3) Maximum Even Sum
최초 업로드: 2025-09-07 18:39:50
최근 수정 시간: 2025-09-07 19:12:59
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 4
# C. Maximum Even Sum [문제 링크](https://codeforces.com/contest/2137/problem/C) ## Problem Statement You are given two integers $a$ and $b$. You are to perform the following procedure: First, choose an integer $k$ such that $b$ is divisible by $k$. Then, simultaneously multiply $a$ by $k$ and divide $b$ by $k$. Find the greatest possible **even** value of $a+b$. If it is impossible to make $a+b$ even, output $-1$ instead. ## Input Each test contains multiple test cases. The first line contains the number of test cases $t$ $(1 \le t \le 10^{4})$. The description of the test cases follows. The first line of each test case contains two integers $a$ and $b$ $(1 \le a, b \le a\cdot b \le 10^{18})$. ## Output For each test case, output the maximum **even** value of $a+b$ on a new line. ## 풀이 a, b가 홀-짝, 짝-짝, 홀-홀, 짝-홀인 경우 4가지로 나누어서 풀었다. 구현할 때, b가 짝수인 경우 두 가지 케이스는 합치는 것이 깔끔하여 합쳐서 구현하였다. ``` c++ #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { ll a, b; cin >> a >> b; if(b%2==0) { if((a*(b/2)+2)%2==1) cout << "-1\n"; else cout << a*(b/2)+2 << '\n'; } else if(a%2==0 && b%2==1) { cout << "-1\n"; } else { cout << a*b+1 << '\n'; } } } ```