Ural 1073 square country (DP)

Source: Internet
Author: User

Square country


The amount of money required for a square area with a side length of A is a ^ 2. Now, enter N as the amount of money. You can make up N for the minimum number of places you want to buy.


Idea: DP, DP [I] = min (DP [I], DP [J-I * I] + 1) introduced by the backpack idea ); square is composed of square, so it is I * I, and it is I * I during the cycle.

#include <stdio.h>#define min(a, b) ((a) > (b) ? (b) :(a))int n;int dp[60005];int main(){    scanf("%d", &n);    for(int i = 1; i <= n; ++i){        dp[i] = i;    }    for(int i = 1; i*i <= n; ++i){        for(int j = i*i; j <= n; ++j){            dp[j] = min(dp[j], dp[j-i*i]+1);        }    }    printf("%d\n", dp[n]);    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.