HDU1028Ignatius and the Princess III(母函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1028
母函数:
例1:若有1克、2克、3克、4克的砝码各一 枚,能称出哪几种重量?各有几种可能方案?
如何解决这个问题呢?考虑构造母函数。
如果用x的指数表示称出的重量,则:
1个1克的砝码可以用函数1+x表示,
1个2克的砝码可以用函数1+x2表示,
1个3克的砝码可以用函数1+x3表示,
1个4克的砝码可以用函数1+x4表示,
(1+x)(1+x2)(1+x3)(1+x4)
=(1+x+x2+x3)(1+x3+x4+x7)
=1+x+x2+2x3+2x4+2x5+2x6+2x7+x8+x9+x10
从上面的函数知道:可称出从1克到10克,系数便是方案数。
例如右端有2x5 项,即称出5克的方案有2:5=3+2=4+1;同样,6=1+2+3=4+2;10=1+2+3+4。
故称出6克的方案有2,称出10克的方案有1
这样一来,一个括号内有多少个x,那么就表示有多少个砝码,如果有3个值为1的砝码,那么就是(1+x+x2+x3),其中,xk中的k就表示用k个值为1的组成,他的系数为1,也就是说用只用值为1的要配出3出来只有一种方法。
按照上面的方法,3个值为2的砝码那就是(1 + x2 + x4 + x6),x6相当于(x2)3,就是说3 个值为2的构成6。
那么,上面的x的函数就是母函数,可以用来解决组合问题(详细的可以参阅网上资料,也可以看下面两个简单应用)
#include<stdio.h> int c1[],c2[]; int main()
{
int n;
while(~scanf("%d", &n))
{
int i;
for(i = ;i <= n; i++)
{
c1[i] = ;
c2[i] = ;
}
for(i =;i<=n;i++)//操作第i个括号
{
for(int j = ; j<= n;j++)//对于指数为j的进行操作
{
for(int k = ;k+j<=n;k+=i)//吧第i个的每一个数与之前的结果相乘
{
c2[j+k]+=c1[j];//j+k指数相加,他的值就是这个指数的系数
}
}
for(int j = ;j<=n;j++)//系数保存在前面一个数组中
{
c1[j] = c2[j];
c2[j] = ;
}
}
printf("%d\n", c1[n]);
}
return ;
}
另外,我还写了一个记忆化搜索的方法,虽然耗时耗空间,但是过了,挂在这里瞧瞧(15Ms,上面那个0Ms)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<stdlib.h>
using namespace std;
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define MAXN 400005
#define INF 2000000007
#define mem(a) memset(a,0,sizeof(a)) int ans[];
int vis[][],d[][]; int dfs(int a, int b)
{
if(vis[a][b])return d[a][b];
vis[a][b] = ;
d[a][b] = ;
for(int i = (a+)/; i <= a-b; i++)
{
d[a][b]+=dfs(i, a-i);
}
return d[a][b];
} void f()
{
ans[] = ;
ans[] = ;
memset(vis,,sizeof(vis));
for(int i = ; i<= ; i++)
{
ans[i] = ;
for(int j = ; i-j >= j; j++)
{
ans[i]++;
if(i-j >= *j)
{
ans[i] += dfs(i-j, j);
ans[i] --;
}
}
}
} int main()
{
f();
int n;
while(~scanf("%d",&n))
{
printf("%d\n",ans[n]);
}
return ;
}
HDU1028Ignatius and the Princess III(母函数)的更多相关文章
- HDU1028Ignatius and the Princess III母函数入门
这个题也能够用递归加记忆化搜索来A,只是因为这题比較简单,所以用来做母函数的入门题比較合适 以展开后的x4为例,其系数为4,即4拆分成1.2.3之和的拆分数为4: 即 :4=1+1+1+1=1+1+2 ...
- HDU-1028-Ignatius and the Princess III(母函数)
链接: https://vjudge.net/problem/HDU-1028 题意: "Well, it seems the first problem is too easy. I wi ...
- hdu--1028--Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Ignatius and the Princess III(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028Ignatius and the Princess III(母函数简单题)
Ignatius and the Princess III Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 1028 Sample Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
随机推荐
- Effective C++学习笔记 条款05:了解C++默默编写并调用的哪些函数
一.如果用户没有提供构造函数.copy构造函数.copy assignment操作符和析构函数,当且仅当这些函数被需要的时候,编译器才会帮你创建出来.编译器生成的这些函数都是public且inline ...
- tornado中使用torndb,连接数过高的问题
问题背景 最近新的产品开发中,使用了到了Tornado和mysql数据库.但在基本框架完成之后,我在开发时候发现了一个很奇怪的现象,我在测试时,发现数据库返回不了结果,于是我在mysql中输入show ...
- "xxxx".zip:这个压缩文件格式未知或者数据已经被损坏,打不开压缩文件,总出现这个提示的解决方法
从网上下载了一些压缩文件,有时解压时会出现“这个压缩文件格式未知或者数据已经被损坏”或“未找到压缩文件”的提示. 造成的原因有两种: 一.网站上的压缩文件本来就是坏的. 1.你可以尝试可以使用WINR ...
- 封装Log工具类
public class LogUtil { public static final int VERBOSE = 1; public static final int DEBUG = 2; publi ...
- Share SDK 第三方登录
import java.util.HashMap; import org.apache.http.Header; import android.app.Activity; import android ...
- HDU 5311 Hidden String (暴力)
题意:今天是BestCoder一周年纪念日. 比赛管理员Soda有一个长度为n的字符串s. 他想要知道能否找到s的三个互不相交的子串s[l1..r1], s[l2..r2], s[l3..r3]满足下 ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- PHP单元测试工具PHPUnit初体验
今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查. 看了PHPUnit的文档之后 ...
- H264 帧结构分析、帧判断
http://blog.csdn.net/dxpqxb/article/details/7631304 H264以NALU(NAL unit)为单位来支持编码数据在基于分组交换技术网络中传输. NAL ...
- POJ 1833 排列
题意: 给你某个排列 求从下一个排列开始的第k个排列如果是最后一个排列 则下一个排列为1 2 3 ... n// 1 用stl 里面的 next_permutation// 2 用生成下一个排列算法/ ...