HDU 1398 Square Coins (DP)

Source: Internet
Author: User

Square Coins

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8800 Accepted Submission (s): 5991


Problem descriptionpeople in Silverland use square coins. Not only they has square shapes but also their values is square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e, 1-credit Coins, 4-credit Coins, 9-credit Coins, ..., and 289-credit coins, is available in Silverland.
There is four combinations of coins to pay ten credits:

Ten 1-credit coins,
One 4-credit coin and six 1-credit coins,
4-credit coins and 1-credit coins, and
One 9-credit coin and one 1-credit coin.

Your mission is to count the number of ways to pay a given amount using coins of Silverland.

Inputthe input consists of lines each containing an integer meaning an amount to being paid, followed by a line containing a Zero. You may assume the amounts is positive and less than 300.

Outputfor each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output.

Sample Input210300

Sample Output1427 Test instructions is the number of squares to form a number, how many different composition schemes. At first, I thought that there was a lot of repetition. For example 10 = 1 + 9 = 9 + 1 is not very good maintenance of the two-dimensional approach is to use dp[i][j]. Represents the number of schemes that make up the number J with the first I square number (0<i<=17). This ensures that the calculations are not repeated.
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<algorithm>using namespacestd;#defineRoot 1,n,1#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1#defineLR rt<<1#defineRR rt<<1|1typedefLong LongLl;typedef pair<int,int>PII;#defineX First#defineY SecondConst intOO = 1e9+7;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6 ;Const intN =305 ;Const intMoD = 1e9+7; LL Dp[n][n];intN;voidinit () {dp[0][0] =1 ;  for(inti =1; I <= -; ++i) {         for(intj =0; J < N; ++j) {             for(intK =0; K <= J; K + = i *i) {Dp[i][j]+ = dp[i-1][j-K]; }        }    }}voidRun () {if(!n)return ; cout<< dp[ -][n] <<Endl;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); #endif //LOCALIos::sync_with_stdio (false); Init ();  while(Cin >>N) Run ();
View Code

HDU 1398 Square Coins (DP)

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.