티스토리 뷰
728x90
문제 링크
https://www.acmicpc.net/problem/16723
수학문제이다.
나는 아래처럼 풀었다.
정답을 p라 하고, n이 충분히 크다고 가정하자.
p=2+4+6+8+10+12+...
=2(1+2+3+4+5+6+7+8+...)
=2(1+3+5+...+2(1+2+3+...))
이런식으로 짝수들을 계속 2로 묶어가며 수를 절반씩 줄여갈 수 있다.
이를 적당히 구현해보자!
정답 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
using ll = long long; | |
ll n, ans; | |
int main() { | |
cin >> n; | |
ans = 2 * n; | |
for (ll i = 1; n / 2 > 0; i++, n /= 2) { | |
ans += (1 << i) * (n / 2); | |
} | |
cout << ans; | |
} |
728x90
'알고리즘 > 문제 풀이' 카테고리의 다른 글
[BOJ] 백준 16211 백채원 (0) | 2019.05.13 |
---|---|
[BOJ] 백준 16724 피리 부는 사나이 (1) | 2019.01.20 |
[BOJ] 백준 16722 결! 합! (1) | 2019.01.20 |
[BOJ] 백준 16721 Structure of Balanced Networks (1) | 2019.01.20 |
[BOJ] 백준 16720 BAZE RUNNER (0) | 2019.01.20 |
댓글