Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17-miniron-v #64

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions miniron-v/DP/1699-제곱수의 ν•©.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <vector>

int main() {
int n;
std::cin >> n;

std::vector<int> dp(n + 1, 0);
dp[1] = 1;

for (int i = 2; i <= n; ++i) {
int min_index = i - 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min_indexλ₯Ό i-1둜 μ΄ˆκΈ°ν™”ν•˜λŠ” μ΄μœ κ°€ μžˆμ„κΉŒμš”?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

min_indexλŠ” μ΅œμ†Ÿκ°’μ„ κ°–λŠ” 인덱슀λ₯Ό μ €μž₯ν•©λ‹ˆλ‹€.

그런데 이게 μ΅œμ†Ÿκ°’μ΄λ‚˜ μ΅œλŒ“κ°’μ„ μ €μž₯ν•˜λŠ” λ³€μˆ˜λΌλ©΄, λ‹¨μˆœνžˆ λ²”μœ„λ³΄λ‹€ 큰 수λ₯Ό 지정해주면 λΌμš”. (닿을 수 μ—†μ„λ§ŒνΌ 큰 수)

ν•˜μ§€λ§Œ μΈλ±μŠ€λŠ” 0λΆ€ν„° i-1κΉŒμ§€ 있고, κ·Έ 쀑 μ–΄λ–€ κ±Έ κΈ°μ€€μœΌλ‘œ μž‘μ•„μ•Ό 항상 μ΅œμ†Ÿκ°’λ³΄λ‹€ 큰 μˆ˜κ°€ 될지... μ•ŒκΈ° μ–΄λ ΅μ£ ?

κ·Έλž˜μ„œ λ°˜λ“œμ‹œ νƒμƒ‰ν•˜λŠ”, 첫번째 인덱슀λ₯Ό κΈ°μ€€μœΌλ‘œ μž‘μ•˜μŠ΅λ‹ˆλ‹€. 그리고 forλ¬Έ i의 초기 값을 2둜 μ„€μ •ν•΄, i-1번 칸은 μ œμ™Έν•˜κ³  λΉ„κ΅ν•˜κ²Œλ” κ΅¬ν˜„ν–ˆμ–΄μš”. μ΄λ ‡κ²Œ κ΅¬ν˜„ν•˜λ©΄

1. i-1이 μ΅œμ†Ÿκ°’μ„ κ°€μ§ˆ 경우, λͺ¨λ“  쑰건문을 이겨내고 i-1이 min_indexκ°€ 됨
2. i-1보닀 μž‘μ€ 값을 κ°–λŠ” μΈλ±μŠ€κ°€ μžˆμ„ 경우, ν•΄λ‹Ή μΈλ±μŠ€κ°€ min_indexκ°€ 됨

μœ„ 과정을 톡해 항상 μ΅œμ†Ÿκ°’μ˜ 인덱슀λ₯Ό μ €μž₯ν•  수 있게 λ©λ‹ˆλ‹€.


for (int j = 2; j * j <= i; ++j) {
if (dp[i - (j * j)] < dp[min_index]) {
min_index = i - (j * j);
}
}

dp[i] = dp[min_index] + 1;
}

std::cout << dp[n];
}
2 changes: 2 additions & 0 deletions miniron-v/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
| 13μ°¨μ‹œ | 2024.02.06 | DP | [퇴사 2](https://www.acmicpc.net/problem/15486) | [#50](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/50) |
| 14μ°¨μ‹œ | 2024.02.11 | κ·Έλž˜ν”„, DP | [ACM Craft](https://www.acmicpc.net/problem/1005) | [#53](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/53) |
| 15μ°¨μ‹œ | 2024.02.15 | DP, 큰 수 μ—°μ‚° | [타일링](https://www.acmicpc.net/problem/1793) | [#56](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/56) |
| 16μ°¨μ‹œ | 2024.02.18 | μˆ˜ν•™ | [ν•©](https://www.acmicpc.net/problem/1081) | [#61](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/61) |
| 17μ°¨μ‹œ | 2024.02.21 | DP | [제곱수의 ν•©](https://www.acmicpc.net/problem/1699) | [#64](https://github.com/AlgoLeadMe/AlgoLeadMe-5/pull/64) |
---
36 changes: 36 additions & 0 deletions miniron-v/μˆ˜ν•™/1081-ν•©.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>

long long gaussSum(long long k) {
if (k % 2 == 0) {
return (k / 2) * (k + 1);
}

return ((k + 1) / 2) * k;
}

long long getSumOfPlace(long long n) {
if (n < 1) {
return 0;
}

long long answer = gaussSum(n);

long long d = 10;
while ((n / d) > 0) {
long long q = n / d;
long long r = n % d;

answer -= 9 * (gaussSum(q - 1) * d + q * (r + 1));

d *= 10;
}

return answer;
}

int main() {
long long l, u;
std::cin >> l >> u;

std::cout << getSumOfPlace(u) - getSumOfPlace(l - 1);
}