首页 > 其他分享 >Square Coins(母函数)

Square Coins(母函数)

时间:2023-01-19 10:44:06浏览次数:49  
标签:Coins 函数 int coins 289 credit Square square

Problem Description
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.

Input
The 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.

Output
For 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.


输入样例

2
10
30
0

输出样例

1
4
27
用数组保存可选种类的母函数
附ac代码
#include<bits/stdc++.h>
using namespace std;
int val[17]={1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289};
int c1[310],c2[310];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    while(cin>>n&&n)
    {
        for(int i=0;i<=n;++i)
        {
            c1[i]=1;c2[i]=0;
        }
        for(int i=2;i<=17;++i)//枚举需要合并的次数
        {
        for(int j=0;j<=n;++j)//枚举当前括号前的总式各幂次
           for(int k=0;k+j<=n;k+=val[i-1])//当前括号的次数
           {
            c2[j+k]+=c1[j];
           }
        for(int j=0;j<=n;++j)
            c1[j]=c2[j],c2[j]=0;
        }
        cout<<c1[n]<<'\n';
    }
    return 0;
}
 

标签:Coins,函数,int,coins,289,credit,Square,square
From: https://www.cnblogs.com/ruoye123456/p/17061162.html

相关文章

  • 5week-4函数作用域
    一.作用域:可见范围,是表示符的可见范围,常量,变量函数天然就是作用域1.块作用域if,for,switch语句中:=定义的变量,都是只能在当前ifforswitch中使用,离开这个范围......
  • 类内函数的override问题-方法
    Question:haveabaseclasswithavirtualfunction:classBase{public:virtualvoidFunction();};voidBase::Function(){cout<<"defaultversion"<<......
  • 解决:无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
    如果我们已经安装了PHP或者其他集成环境,但是在命令行执行php命令时还是报这个错误  那是因为没有配置环境变量在此电脑上右键,然后看下面这张图  然后在Path变......
  • m基于效用函数的联合资源分配matlab仿真,对比PF,CUBP以及DUBP三种方法
    1.算法描述 表示基站n到用户m是否连接。 1.1C-CUBP   主要涉及到的公式有: 1.2C-DUBP 主要涉及到的公式有: 2.仿真效果预览matlab2022a仿真......
  • 函数的基本使用
    函数简介函数的语法结构函数的定义与调用函数简介name_list=['jason','kevin','oscar','jerry']print(len(name_list))'''突然无法使用len'''count=0fori......
  • 函数的参数
    函数的参数形式参数 在函数定义阶段括号内填写的参数简称'形参'实际参数 在函数调用阶段括号内填写的参数简称'实参'++++++++++++++++++++++++++++++++++++++++++++......
  • C语言基础--函数
    目录一、什么是函数二、函数的创建三、函数的使用四、返回值的使用五、什么是形参和实参六、默认值形参七、函数的递归一、什么是函数编程中的函数是将一些需要复用的代......
  • 常量、函数、三大语句--(基本)
    常量的知识点1.字面常量比如:2、3、3.14就是字面上的不变的量 2.const修饰的常变量const修饰一个变量的时候,变量具有了常属性,也就是不能通过赋值去改变变量了,但它实际上......
  • 进程监视子进程之wait函数
    父进程监视子进程,需要知道子进程状态什么时候改变状态改变有哪些:子进程终止子进程收到停止信号而停止运行,收到恢复信号而运行wait函数作用监视进程什么时候终止回......
  • 35_输出素数(函数)
    一、python收获:1、python自己可能用到的快捷注释:选中ctrl+/、三个引号‘’‘2、根号方式:二次的话sqrt(但要importmath,math.sqrt)、或者使用内置函数pow(i,次数(比如0.5))......