Square coins (Dp-primary function)

Source: Internet
Author: User
Square coins Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 8307 accepted submission (s): 5648


Problem descriptionpeople in silverland use square coins. not only they have square shapes but also their values are 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, are available in silverland.
There are four combinations of coins to pay ten credits:

Ten 1-credit coins,
One 4-credit coin and six 1-credit coins,
Two 4-credit coins and two 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 be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.
 
Outputfor each of the given amount, one line containing a single integer representing the number of combinations of coins shocould be output. No other characters shoshould appear in the output.
 
Sample Input
210300
 
Sample output
1427
 
Sourceasia 1999, Kyoto (Japan)

Question: The number of N yuan is given in the question. How many ways can I find it;

Solution: You can use DP or the primary function to solve this problem. It is a zero-second complexity. I personally think DP is faster. Read the code for details!

Dp ac code:

# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; typedef long ll; ll DP [330]; /*************** // ** simple DP problem * DP [I] indicates the number of methods that can constitute I; * state equation DP [I] + = DP [I-j * j]; * indicates that the number of methods with money I equals to the sum of the number of methods in all its subintervals; */void unit () {DP [0] = 1; // The number of methods with the number of money being 0 is 1; for (INT I = 1; I <= 17; I ++) {for (Int J = 1; j <= 300; j ++) {If (J-I * I <0) continue; DP [J] + = DP [J-I * I] ;}} int main () {int N; Uni T (); While (true) {scanf ("% d", & N); If (! N) break; printf ("% i64d \ n", DP [N]);} return 0 ;}

Master function AC code:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long ll;const int M  = 1000;ll a[M],b[M];int main(){    int n;    while(true)    {        scanf("%d",&n);        if(n == 0) break;        for(int i = 0; i <= n; i++)        {            a[i] = 1;            b[i] = 0;        }        for(int i = 2; i * i <= n; i++)        {            int t = i * i;            for(int j = 0; j <= n; j++)            {                for(int k = 0; k + j <= n; k += t)                {                    b[k + j] += a[j];                }            }            for(int j = 0; j <= n; j++)            {                a[j] = b[j];                b[j] = 0;            }        }        printf("%I64d\n",a[n]);    }    return 0;}



Square coins (Dp-primary function)

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.