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 ...
随机推荐
- ZOJ 3607 Lazier Salesgirl(贪心)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607 题意:一个卖面包的小姑娘,给第i个来买面包的人的价格是pi, ...
- sdut 2351 In Danger (找规律)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2351 题意:xyez, xy表示一个十进 ...
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
abstract的method不可以是static的,因为抽象的方法是要被子类实现的,而static与子类扯不上关系! native方法表示该方法要用另外一种依赖平台的编程语言实现的,不存在着被子类实 ...
- uva12716GCD XOR
筛法,打表. 通过打表可知,但gcd(a,b)==a xor b时,a xor b = a-b. 就是求满足 c = a-b且c = a xor b 的c的个数. #include<cstdio ...
- (转)在mac上配置cocos2d-x开发环境
转自:http://www.cnblogs.com/xiaodao/archive/2013/01/08/2850751.html 一.下载cocos2d-x最新安装包 在终端中cd到本地将要存放目录 ...
- C#进程启动程序,并禁止原窗口操作
Process myProcess = new Process(); myProcess.StartInfo.FileName = exeName; myP ...
- 通过对源代码的反向工程学习CoreData架构
在本文开始,先给出反向工程后的结果: 不过需要注意,三个实例的指针都被同一个实例拥有,比如三个指针都位于appDelegate. 在AppDelegate类中定义了下面三个属性: @property ...
- 移动设备3G网站制作的detail
说明一下,在此所说的移动设备前端开发是指针对高端智能手机(如Iphone.Android),所以需要对webkit内核的浏览器有一定的了解. 1.webkit内核中的一些私有的meta标签 <m ...
- Canvas处理头像上传
未分类 最近社区系统需要支持移动端,其中涉及到用户头像上传,头像有大中小三种尺寸,在PC端,社区用Flash来处理头像编辑和生成,但该Flash控件的界面不友好而且移动端对Flash的支持不好,考虑到 ...
- HDU 5783 Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...