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

Here is DP solution. If you know the Lagrange's proof about the theory, please contact me. megatronbiao@gmail.com
Posted by Megatron 8 Feb 2009 14:47

DP is easy. I want to know the easiest solution to the problem. Please contact me. megatronbiao@gmail.com

Thanks.

Sorry for my English.

#include <iostream>
#include <cmath>
using namespace std;
int data[60001];
int main()
{
    int n;
    cin>>n;
    data[1]=1;
    data[2]=2;
    for (int i=3;i<=n;++i) {
        int min=data[i-1]+1;
        for (int j=2;j<=245;++j) {
            if (i>=j*j) {
               if (min>data[i-j*j]+1) min=data[i-j*j]+1;
            }
            else break;
        }
        data[i]=min;
    }
    cout<<data[n];
    return 0;
}