[BOJ] 1463 1로 만들기

Time Lapse :None

1463.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <vector>
#include <memory.h>
#include <iostream>
using namespace std;
int DP[1000001];
int F(int N){
if(N==1)
return 0;
int &ret = DP[N];
if(ret!=-1)
return ret;
ret = 987654321;
if(N%3==0)
ret = min(ret,F(N/3)+1);
if(N%2==0)
ret = min(ret,F(N/2)+1);
ret = min(ret,F(N-1)+1);
return ret;
}
int main(void) {
int N;
cin>>N;
memset(DP,-1,sizeof(DP));
cout<<F(N)<<endl;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/1463/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.