1073. Square Countrytime limit:1.0 Second
Memory limit:64 mbthere Live square people in a square country. Everything in this country is square also. Thus, the Square Parliament had passed a law on a land. According to the law each citizen of the country have a right to buy land. A land was sold in squares, surely. Moreover, a length of a square side must be a positive integer amount of meters. Buying a square of land with a side
aOne pays
a2 Quadrics (a local currency) and gets a square certificate of a landowner. One citizen of the country have decided to invest all of his
NQuadrics into the land. He can, surely, do it, buying square pieces 1x1 meters. At the same time the citizen have requested to minimize an amount of pieces he buys: "It'll be a easier for me to pay taxes , "-he has said. He has bought the land successfully. Your task is to find out a number of certificates he has gotten. Inputthe only line contains a positive integer
N≤60, that's a number of quadrics that the citizen have invested. Outputthe only line contains a number of certificates so he has gotten. Sample
problem Author:Stanislav Vasilyev
problem Source:Ural State univerisity Personal Contest Online February ' 2001 Students Session
Tags:Dynamic programming()difficulty:161 Test Instructions: Give x, ask how many total square numbers to add to get X. Analysis: DP can solve this problem. Dp[i] means I at least how many to make up, dp[i] = min (d[i-j*j] + 1) But the problem has a simpler approach. According to the theorem, still and positive integers can be composed of four complete squares. So say a maximum of four numbers. Such direct violence can be. Of course, I was mentally retarded and did not think of it at the time.
1 /**2 Create by Yzx-stupidboy3 */4#include <cstdio>5#include <cstring>6#include <cstdlib>7#include <cmath>8#include <deque>9#include <vector>Ten#include <queue> One#include <iostream> A#include <algorithm> -#include <map> -#include <Set> the#include <ctime> -#include <iomanip> - using namespacestd; -typedefLong LongLL; +typedefDoubleDB; - #defineMIT (2147483647) + #defineINF (1000000001) A #defineMLL (1000000000000000001LL) at #defineSZ (x) ((int) (x). Size ()) - #defineCLR (x, y) memset (x, y, sizeof (x)) - #definePUF Push_front - #definePub push_back - #definePOF Pop_front - #definePOB pop_back in #defineFT first - #defineSD Second to #defineMk Make_pair + -InlineintGetint () the { * intRet =0; $ CharCh =' ';Panax Notoginseng BOOLFlag =0; - while(! (Ch >='0'&& Ch <='9')) the { + if(Ch = ='-') Flag ^=1; ACh =GetChar (); the } + while(Ch >='0'&& Ch <='9') - { $RET = RET *Ten+ Ch-'0'; $Ch =GetChar (); - } - returnFlag? -Ret:ret; the } - Wuyi Const intN =60010; the intN; - intDp[n]; Wu -InlinevoidInput () About { $CIN >>N; - } - -InlinevoidSolve () A { +dp[0] =0, dp[1] =1; the for(inti =2; I <= N; i++) - { $Dp[i] =INF; the for(intj =1; J <= I/j; J + +) the if(Dp[i] > Dp[i-j * j] +1) theDp[i] = Dp[i-j * j] +1; the } -cout << Dp[n] <<Endl; in } the the intMain () About { theFreopen ("e.in","R", stdin); the Input (); the Solve (); + return 0; -}View Code
Ural 1073. Square Country