Last active
July 6, 2019 11:13
-
-
Save zzandland/ba8b89544d1d6282d9f9921bda393431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <set> | |
| #include <vector> | |
| #include <math.h> | |
| using namespace std; | |
| int sum_sq(int n) { | |
| int n_sqrt = sqrt(n); | |
| set<int> sqrs; | |
| for (int i = 1; i <= n_sqrt; ++i) | |
| sqrs.insert(i * i); | |
| vector<int> dp(n + 1); | |
| dp[0] = 0; | |
| int curr = 1; | |
| for (int i = 1; i <= n; ++i) { | |
| if (sqrs.count(i)) curr = i; | |
| dp[i] = 1 + dp[i - curr]; | |
| } | |
| return dp[n]; | |
| } | |
| int main() { | |
| int i; | |
| cin >> i; | |
| cout << sum_sq(i) << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment