-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1699.cpp
More file actions
47 lines (42 loc) · 964 Bytes
/
1699.cpp
File metadata and controls
47 lines (42 loc) · 964 Bytes
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
int D[100001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
D[i] = i;
for (int j = 1; j * j <= i; j++)
D[i] = min(D[i], D[i - j * j] + 1);
}
cout << D[N];
}
// 더 빠른 답안
#include <stdio.h>
int isSquare[100001];
int isTwoSquare[100001];
int main(void) {
for (int i = 1; i * i < 100001; i++)
isSquare[i * i] = 1;
for (int i = 1; i * i < 100001; i++) {
for (int j = i; j * j + i * i < 100001; j++)
isTwoSquare[i * i + j * j] = 1;
}
int N;
scanf("%d", &N);
if (isSquare[N])
printf("1");
else if (isTwoSquare[N])
printf("2");
else {
for (int i = 1; i * i <= N; i++) {
if (isTwoSquare[N - i * i]) {
printf("3");
return 0;
}
}
printf("4");
}
}