반응형
https://www.acmicpc.net/problem/1914
큰 수의 계산을 잘 구현해야 하는 문제이다.
2^100은
10^34 정도 되기 때문에
long long 은 최대 19자리이므로 long long 2개를 붙여쓰면 38자리까지 구현이 가능하다.
#include <bits/stdc++.h>
using namespace std;
void _pow(int n){
long long a = 0, b = 0, mod = 1'000'000'000'000'000'000;
for(int i=0; i<n; i++){
b*=2;
a = 2 * a + 1;
b += a / mod;
a %= mod;
}
if(b>0) cout << b << a << '\n';
else cout << a << '\n';
}
//start : 0 , mid:1, end:2
void moveBlock(int start, int end){
cout << start+1 << ' ' << end+1 << '\n';
}
void hanoi(int n, int start, int mid, int end){
if(n==0) return;
hanoi(n-1, start, end, mid);
//마지막꺼 옮기기
moveBlock(start, end);
hanoi(n-1, mid, start, end);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
_pow(n);
if(n<=20) {
hanoi(n, 0, 1, 2);
}
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 5670번 : 휴대폰 자판 - Trie C++ (0) | 2022.09.28 |
---|---|
백준 1167번 : 트리의 지름 - Java (0) | 2022.09.21 |
백준 1004번 : 어린왕자 - C++ (0) | 2022.09.10 |
백준 2263번 : 트리의 순회 - Java (0) | 2022.09.08 |
백준 9657번 : 돌 게임 3 - C++ (0) | 2022.09.04 |