zoukankan      html  css  js  c++  java
  • hdu1028 Ignatius and the Princess III

    题目链接

    solution

    此题有两种解法。

    第一种解法就是裸的完全背包。

    就相当于有n种物品,第i种物品的重量是i。每种物品有无限多个,问恰好填满一个容量为n的背包的方案数。

    第二种解法是生成函数。

    用生成函数((1+x+x^1+x^2+...))表示拆分出的(1)的数量。用((1+x^2+x^4+x^6+...))表示拆分出的(2)的数量。剩下的同理。最终(x^n)的系数就是答案。

    code

    解法1

    /*
    * @Author: wxyww
    * @Date:   2020-04-16 14:18:39
    * @Last Modified time: 2020-04-16 14:19:55
    */
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<ctime>
    using namespace std;
    typedef long long ll;
    
    ll read() {
    	ll x = 0,f = 1;char c = getchar();
    	while(c < '0' || c > '9') {
    		if(c == '-') f = -1; c = getchar();
    	}
    	while(c >= '0' && c <= '9') {
    		x = x * 10 + c - '0'; c = getchar();
    	}
    	return x * f;
    }
    int f[150];
    int main() {
    	int n;
    	while(~scanf("%d",&n)) {
    		memset(f,0,sizeof(f));
    		f[0] = 1;
    		for(int i = 1;i <= n;++i) {
    			for(int j = i;j <= n;++j) 
    				f[j] += f[j - i];
    		}
    		cout<<f[n]<<endl;
    	}
    
    	return 0;
    }
    

    解法2

    /*
    * @Author: wxyww
    * @Date:   2020-04-16 14:11:40
    * @Last Modified time: 2020-04-16 14:19:27
    */
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<ctime>
    using namespace std;
    typedef long long ll;
    const int N = 150;
    ll read() {
    	ll x = 0,f = 1;char c = getchar();
    	while(c < '0' || c > '9') {
    		if(c == '-') f = -1; c = getchar();
    	}
    	while(c >= '0' && c <= '9') {
    		x = x * 10 + c - '0'; c = getchar();
    	}
    	return x * f;
    }
    int f[N][N],tmp[N];
    int main() {
    	int n;
    	while(~scanf("%d",&n)) {
    		memset(f,0,sizeof(f));
    		for(int i = 0;i <= n;++i) f[1][i] = 1;
    		for(int t = 2;t <= n;++t) {
    			for(int i = 0;i <= n;++i) {
    				for(int k = 0;k + i <= n;k += t) {
    					f[t][i + k] += f[t - 1][i];
    				}
    			}
    		}
    		cout<<f[n][n]<<endl;
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    HMM 学习一
    matlab buffer的使用
    JDK环境变量配置及Tomcat安装服务
    Js参数RSA加密传输,jsencrypt.js的使用
    Dapper基本增删改查
    Dapper事务操作
    Ubuntu知识记录
    InstallShield 覆盖安装
    Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    Sql Server导出表结构Excel
  • 原文地址:https://www.cnblogs.com/wxyww/p/hdu1028.html
Copyright © 2011-2022 走看看