zoukankan      html  css  js  c++  java
  • 母函数基本应用

    People 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 should be output. No other characters should appear in the output. 
    Sample Input

    2
    10
    30
    0

    Sample Output

    1
    4
    27

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<math.h>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<stack>
     8 #include<deque>
     9 #include<iostream>
    10 using namespace std;
    11 typedef long long  LL;
    12 int a[310];     //生成函数
    13 int b[310];     //辅助数组,帮助完成系数的转移
    14 int main()
    15 {
    16     int n;
    17     while(scanf("%d",&n)!=EOF)
    18     {
    19         if(n==0)
    20             break;
    21         int i,p,j;
    22         for(i=0;i<=300;i++) //对两个数组进行初始化,因为一开始什么也没有,所以系数都为1
    23         {
    24             a[i]=0;
    25             b[i]=0;
    26         }
    27         for(i=0;i<=300;i++)     //因为1元的硬币有无限个,所以0到300元都可以构成
    28             a[i]=1;
    29         for(i=2;i<=17;i++)      //一共有17个式子想乘,第一个1元的式子已经处理完了,从2元的开始处理
    30         {
    31             for(j=0;j<=n;j++)       //遍历之前形成的式子的每一项
    32                 for(int k=0;k+j<=n;k+= i*i)     //遍历要乘的式子中系数不为零的项
    33                     b[k+j]+=a[j];
    34             for(int j=0;j<=n;j++)       //将第i个式子处理完之后的系数赋值给初始函数式,同时b数组准备下一轮的转换
    35             {
    36                 a[j]=b[j];
    37                 b[j]=0;
    38             }
    39         }
    40         printf("%d
    ",a[n]);    //有几种组成n元的方式
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    topK问题 前K个高频元素 leetcode692
    反转链表 leetcode206
    关于IO多路复用的简单整理
    两数之和 leetcode1
    使用 jenkins 发布 前端 项目
    CentOS7 部署 nacos 集群
    JWT
    keepalived 的 unicast 单播模式
    使用 keepalived 高可用 nginx
    翻转二叉树 leetcode226
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9434925.html
Copyright © 2011-2022 走看看