ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1073. Square Country

Najmaddin Akhundov How to make this code faster?? Please, help [3] // Problem 1073. Square Country 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;

}
use memoization
Najmaddin Akhundov Re: How to make this code faster?? Please, help // Problem 1073. Square Country 15 Oct 2014 15:36
Thank you Noob, I got an AC with your help.
Paras Kumar Meena Re: How to make this code faster?? Please, help // Problem 1073. Square Country 12 Jul 2015 10:39
One optimization No need to start loop from always from 1 , sqrt(N)/2 is enough ;) thing why?. :)