G - 炎之箭 (URAL - 1073 )
作者:互联网
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 a 2 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.
Example
input | output |
---|---|
344 |
3 |
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int w[61234],dp[61234];
int n,i,j;
for(i=1;i<=245;i++)
{
w[i]=i*i;
}
while(cin >> n)
{
memset(dp,0,sizeof(dp));
int v=sqrt(n);
for(i=w[1];i<=n;i++)
{
dp[i]=i;
}
for(i=2;i<=v;i++)
{
for(j=w[i];j<=n;j++)
{
dp[j]=min(dp[j],dp[j-w[i]]+1);
}
}
cout << dp[n] << endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
int dp[60000+100];
int dfs(int a)
{
if(a==0)
{
return 0;
}
int &ans=dp[a];
if(ans!=-1)
{
return ans;
}
ans=1e9;
for(int i=1; i*i<=a; i++)
{
ans=min(ans,dfs(a-i*i)+1);
}
return ans;
}
int main()
{
int n;
memset(dp,-1,sizeof(dp));
while(cin >> n)
{
cout << dfs(n) << endl;
}
return 0;
}
标签:square,int,炎之箭,1073,land,citizen,URAL,include,dp 来源: https://blog.csdn.net/qq_44134712/article/details/94589495