Skip to content

Commit

Permalink
[Silver III] Title: Four Squares, Time: 12 ms, Memory: 3984 KB -Baekj…
Browse files Browse the repository at this point in the history
…oonHub
  • Loading branch information
scottXchoo committed Nov 28, 2024
1 parent 2d31075 commit fc09392
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 백준/Silver/17626. Four Squares/Four Squares.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>

using namespace std;

int dp[500002];
int N;

int main() {
cin.tie(0)->sync_with_stdio(0);

int n, n_2, below_n;
int imsi_N, cnt, min_cnt;

cin >> N;

dp[0] = 0; //1,4,9,16,25, ... 제곱수를 위함
dp[1] = 1;
dp[2] = 2;
dp[3] = 3;
dp[4] = 1;

for (int i = 5; i <= N; i++) {
n = sqrt(i);
n_2 = n * n;
below_n = i - n_2;
min_cnt = 4;

for (int j = 1; j <= n; j++) {
imsi_N = i - j * j;
cnt = dp[imsi_N] + 1;
if (cnt < min_cnt) {
min_cnt = cnt;
}
}
dp[i] = min_cnt;
}

cout << dp[N];

}
30 changes: 30 additions & 0 deletions 백준/Silver/17626. Four Squares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# [Silver III] Four Squares - 17626

[문제 링크](https://www.acmicpc.net/problem/17626)

### 성능 요약

메모리: 3984 KB, 시간: 12 ms

### 분류

브루트포스 알고리즘, 다이나믹 프로그래밍

### 제출 일자

2024년 11월 28일 13:13:11

### 문제 설명

<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>

<p>자연수 <em>n</em>이 주어질 때, <em>n</em>을 최소 개수의 제곱수 합으로 표현하는 컴퓨터 프로그램을 작성하시오.</p>

### 입력

<p>입력은 표준입력을 사용한다. 입력은 자연수 <em>n</em>을 포함하는 한 줄로 구성된다. 여기서, 1 ≤ <em>n</em> ≤ 50,000이다.</p>

### 출력

<p>출력은 표준출력을 사용한다. 합이 <em>n</em>과 같게 되는 제곱수들의 최소 개수를 한 줄에 출력한다.</p>

0 comments on commit fc09392

Please sign in to comment.