Skip to content

Commit fc09392

Browse files
committed
[Silver III] Title: Four Squares, Time: 12 ms, Memory: 3984 KB -BaekjoonHub
1 parent 2d31075 commit fc09392

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int dp[500002];
6+
int N;
7+
8+
int main() {
9+
cin.tie(0)->sync_with_stdio(0);
10+
11+
int n, n_2, below_n;
12+
int imsi_N, cnt, min_cnt;
13+
14+
cin >> N;
15+
16+
dp[0] = 0; //1,4,9,16,25, ... 제곱수를 위함
17+
dp[1] = 1;
18+
dp[2] = 2;
19+
dp[3] = 3;
20+
dp[4] = 1;
21+
22+
for (int i = 5; i <= N; i++) {
23+
n = sqrt(i);
24+
n_2 = n * n;
25+
below_n = i - n_2;
26+
min_cnt = 4;
27+
28+
for (int j = 1; j <= n; j++) {
29+
imsi_N = i - j * j;
30+
cnt = dp[imsi_N] + 1;
31+
if (cnt < min_cnt) {
32+
min_cnt = cnt;
33+
}
34+
}
35+
dp[i] = min_cnt;
36+
}
37+
38+
cout << dp[N];
39+
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Silver III] Four Squares - 17626
2+
3+
[문제 링크](https://www.acmicpc.net/problem/17626)
4+
5+
### 성능 요약
6+
7+
메모리: 3984 KB, 시간: 12 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2024년 11월 28일 13:13:11
16+
17+
### 문제 설명
18+
19+
<p>라그랑주는 1770년에 모든 자연수는 넷 혹은 그 이하의 제곱수의 합으로 표현할 수 있다고 증명하였다. 어떤 자연수는 복수의 방법으로 표현된다. 예를 들면, 26은 5<sup>2</sup>과 1<sup>2</sup>의 합이다; 또한 4<sup>2</sup> + 3<sup>2</sup> + 1<sup>2</sup>으로 표현할 수도 있다. 역사적으로 암산의 명수들에게 공통적으로 주어지는 문제가 바로 자연수를 넷 혹은 그 이하의 제곱수 합으로 나타내라는 것이었다. 1900년대 초반에 한 암산가가 15663 = 125<sup>2</sup> + 6<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup>라는 해를 구하는데 8초가 걸렸다는 보고가 있다. 좀 더 어려운 문제에 대해서는 56초가 걸렸다: 11339 = 105<sup>2</sup> + 15<sup>2</sup> + 8<sup>2</sup> + 5<sup>2</sup>.</p>
20+
21+
<p>자연수 <em>n</em>이 주어질 때, <em>n</em>을 최소 개수의 제곱수 합으로 표현하는 컴퓨터 프로그램을 작성하시오.</p>
22+
23+
### 입력
24+
25+
<p>입력은 표준입력을 사용한다. 입력은 자연수 <em>n</em>을 포함하는 한 줄로 구성된다. 여기서, 1 ≤ <em>n</em> ≤ 50,000이다.</p>
26+
27+
### 출력
28+
29+
<p>출력은 표준출력을 사용한다. 합이 <em>n</em>과 같게 되는 제곱수들의 최소 개수를 한 줄에 출력한다.</p>
30+

0 commit comments

Comments
 (0)