ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1073. Square Country

How to make this code faster?? Please, help
Posted by Najmaddin Akhundov 13 Oct 2014 09:26
#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
Posted by Noob 13 Oct 2014 12:00
use memoization
Re: How to make this code faster?? Please, help
Posted by Najmaddin Akhundov 15 Oct 2014 15:36
Thank you Noob, I got an AC with your help.
Re: How to make this code faster?? Please, help
Posted by Paras Kumar Meena 12 Jul 2015 10:39
One optimization No need to start loop from always from 1 , sqrt(N)/2 is enough ;) thing why?. :)