|
|
вернуться в форумHow to make this code faster?? Please, help #include <iostream> #include <cmath> #include <algorithm> using namespace std; int function(int n) { if (n == 1) return 1; else if (n == 0) return 0; else{
int k = 999999999; for (int i = 1; i <= sqrt(n); i++) { int a = function(n - i*i); if (a < k ) k = min(k, a); } return function(k) + 1;
}
} int main() { int n; cin >> n; cout << function(n) << endl; } Re: How to make this code faster?? Please, help Послано Noob 13 окт 2014 12:00 use memoization Re: How to make this code faster?? Please, help Thank you Noob, I got an AC with your help. Re: How to make this code faster?? Please, help One optimization No need to start loop from always from 1 , sqrt(N)/2 is enough ;) thing why?. :) |
|
|