ural 1073. Square Country

1073. Square Country

Time limit: 1.0 second
Memory limit: 64 MB

There live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citizen of the country has a right to buy land. A land is 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 a one pays a2 quadrics (a local currency) and gets a square certificate of a landowner.

One citizen of the country has decided to invest all of his N quadrics into the land. He can, surely, do it, buying square pieces 1 × 1 meters. At the same time the citizen has requested to minimize an amount of pieces he buys: "It will be 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.

Input

The only line contains a positive integer N ≤ 60 000 , that is a number of quadrics that the citizen has invested.

Output

The only line contains a number of certificates that he has gotten.

Sample

input output
344
3

Problem Author: Stanislav Vasilyev
Problem Source: Ural State Univerisity Personal Contest Online February‘2001 Students Session

Tags: dynamic programming  (hide tags for unsolved problems)

Difficulty: 161

题意:给出x,问要多少个完全平方数相加才能得到x。

分析:dp可以解决这个问题。

dp[i]表示i最少要多少个来组成、

dp[i] = min(d[i - j*j] + 1)

但是这题有更简单的做法。

根据定理,仍和正整数可以有四个完全平方数组成。

所以说最多四个数。

这样直接暴力即可。

当然,我是弱智,做的时候没有想到。

 1 /**
 2 Create By yzx - stupidboy
 3 */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <deque>
 9 #include <vector>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 #include <map>
14 #include <set>
15 #include <ctime>
16 #include <iomanip>
17 using namespace std;
18 typedef long long LL;
19 typedef double DB;
20 #define MIT (2147483647)
21 #define INF (1000000001)
22 #define MLL (1000000000000000001LL)
23 #define sz(x) ((int) (x).size())
24 #define clr(x, y) memset(x, y, sizeof(x))
25 #define puf push_front
26 #define pub push_back
27 #define pof pop_front
28 #define pob pop_back
29 #define ft first
30 #define sd second
31 #define mk make_pair
32
33 inline int Getint()
34 {
35     int Ret = 0;
36     char Ch = ‘ ‘;
37     bool Flag = 0;
38     while(!(Ch >= ‘0‘ && Ch <= ‘9‘))
39     {
40         if(Ch == ‘-‘) Flag ^= 1;
41         Ch = getchar();
42     }
43     while(Ch >= ‘0‘ && Ch <= ‘9‘)
44     {
45         Ret = Ret * 10 + Ch - ‘0‘;
46         Ch = getchar();
47     }
48     return Flag ? -Ret : Ret;
49 }
50
51 const int N = 60010;
52 int n;
53 int dp[N];
54
55 inline void Input()
56 {
57     cin >> n;
58 }
59
60 inline void Solve()
61 {
62     dp[0] = 0, dp[1] = 1;
63     for(int i = 2; i <= n; i++)
64     {
65         dp[i] = INF;
66         for(int j = 1; j <= i / j; j++)
67             if(dp[i] > dp[i - j * j] + 1)
68                 dp[i] = dp[i - j * j] + 1;
69     }
70     cout << dp[n] << endl;
71 }
72
73 int main()
74 {
75     freopen("e.in", "r", stdin);
76     Input();
77     Solve();
78     return 0;
79 }

时间: 2024-10-17 03:48:53

ural 1073. Square Country的相关文章

URAL 1073 Square Country(DP)

题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 :其实就是求x12+x22+--+Xn2中的最小的n. 1 //1073 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 6 using namespace std ; 7 8 int a[

URAL 1073 Square Country(DP)

Square Country 大意: 买一块边长为 a 的正方形地需要的钱数是 a^2, 现在输入N为钱的数目,求最少购买地的块数可以凑够N. 思路:DP,由背包思想推出来的dp[i] = min(dp[i], dp[j-i*i]+1);  方块都是由正方形组成的,所以是i*i,循环的时候也是i*i. #include <stdio.h> #define min(a, b) ((a) > (b) ? (b) :(a)) int n; int dp[60005]; int main() {

01背包 URAL 1073 Square Country

题目传送门 1 /* 2 题意:问n最少能是几个数的平方和 3 01背包:j*j的土地买不买的问题 4 详细解释:http://www.cnblogs.com/vongang/archive/2011/10/07/2200721.html 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include <cstring> 10 using namespace std;

ural 1073. Square Country 完全背包

点击打开链接 1073. Square Country Time limit: 1.0 second Memory limit: 64 MB There live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citiz

Ural 1073 Square Country (DP)

题目地址:Ural 1073 DP水题.也可以说是背包. #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include

URAL 1698. Square Country 5(记忆化搜索)

题目链接 题意 : 自守数的定义:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数.例如5*5=25,则5就是自守数.让你求不超过n位的自守数有多少 思路 : 实际上,自守数还有两个性质:以他为后几位的两个数相乘,乘积的后几位仍是这个自守数.刚好n位的自守数一定是两个,当然1位的时候0和1是没有算进去的,但是题目中1是要加上的,所以从第0位深搜枚举到第n位.还有另一种方法,因为一个数是自守数当且仅当这个数是另一个自守数的后缀,2000位的自守数只有两个,所以存下来直接数也行 1 #

ural1097 Square Country 2

Square Country 2 Time limit: 1.0 secondMemory limit: 64 MB The Square Parliament of the Square country (recall the corresponding problem from the USU 2001 personal contest) has decreed that the National Square Park be created. Of course, the Park sho

URAL1073——DP——Square Country

Description There live square people in a square country. Everything in this country is square also. Thus, the Square Parliament has passed a law about a land. According to the law each citizen of the country has a right to buy land. A land is sold i

【算法】将正整数表示为平方数之和

问题来源 Timus Online Judge 网站上有这么一道题目:1073. Square Country.这道题目的输入是一个不大于 60,000 的正整数,要求计算出该正整数最少能够使用多少个正整数的平方和来表示.这道题目的时间限制是 1 秒. 问题解答 <数论导引(第5版)>([英]G.H.Hardy.E.M.Wright 著,人民邮电出版社,2008年10月第1版)第 320 页有以下定理: 定理 369(Lagrange 定理): 每个正整数都是四个平方数之和 在这个定理中,平方