-
[18005] Even or Odd?BOJ 2022. 5. 20. 15:02
https://www.acmicpc.net/problem/18005
18005번: Even or Odd?
Output 2 if the sum of any n consecutive integers in the range from 1 to 1018 must be even, 1 if the sum must be odd, or 0 if the sum could be either even or odd.
www.acmicpc.net
1) 1부터 n까지 더한 경우
2) 2부터 n+1까지 더한 경우
위 2가지만 체크하면 된다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);ll n;cin >> n;ll sum = (n * (n + 1)) / 2LL;ll a = sum, b = sum + n;if (a & 1 and b & 1)cout << 1;else if ((a ^ b) & 1)cout << 0;elsecout << 2;return 0;}
'BOJ' 카테고리의 다른 글
[16168] 퍼레이드 (0) 2022.05.22 [1199] 오일러 회로 (0) 2022.05.22 비밀번호 발음하기 (0) 2022.05.16 [4659] 비밀번호 발음하기 (0) 2022.05.14 [4307] 개미 (0) 2022.05.14