动态规划,先找出可以分解为1个数的平方的数,这些相加的数若不是1,就是2;同理,值为1和2相加的数,不是1、2就是3,余下全部是4.
Run Time: 0.27sec
Run Memory: 420KB
Code length: 708Bytes
Submit Time: 2012-01-12 12:25:48
// Problem#: 2499
// Submission#: 1190049
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
using namespace std;
int main()
{
int T, N;
int i, j;
int data[ 60001 ];
for ( i = 0; i <= 60000; i++ )
data[ i ] = 4;
for ( i = 0; i * i <= 60000; i++ )
data[ i * i ] = 1;
for ( i = 1; i < 60000; i++ ) {
if ( data[ i ] == 1 ) {
for ( j = i; i + j <= 60000; j++ ) {
if ( data[ j ] == 1 && data[ i + j ] > 2 )
data[ i + j ] = 2;
else if ( data[ j ] == 2 && data[ i + j ] > 3 )
data[ i + j ] = 3;
}
}
}
scanf( "%d", &T );
while ( T-- ) {
scanf( "%d", &N );
printf( "%d\n", data[ N ] );
}
return 0;
}