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 ...
随机推荐
- Android安卓开发中图片缩放讲解
安卓开发中应用到图片的处理时候,我们通常会怎么缩放操作呢,来看下面的两种做法: 方法1:按固定比例进行缩放 在开发一些软件,如新闻客户端,很多时候要显示图片的缩略图,由于手机屏幕限制,一般情况下,我们 ...
- public,protected,friendly,private的访问权限
请说出作用域public,private,protected,以及不写时的区别 这四个作用域的可见范围如下表所示. 说明:如果在修饰的元素上面没有写任何访问修饰符,则表示friendly. 作用域 ...
- ti processor sdk linux am335x evm /bin/setup-targetfs-nfs.sh hacking
#!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-targetfs-nfs.sh hacking # 说明: # 本文主要对TI的s ...
- 我的c语言经历
作为一名计算机专业的学生,c语言是我的启蒙编程语言.当时,是刘慧老师带的课.很庆幸,是刘老师带的课.因为,后来当我这个人有了一些经历就会知道.对于像一张 白纸一样的大一新生.老师,如果能给学生很好的启 ...
- (转)HTTP协议详解
引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...
- Remove Duplicates from Sorted List I & II
Title: Given a sorted linked list, delete all duplicates such that each element appear only once. Fo ...
- UVa 11292 The Dragon of Loowater 勇者斗恶龙
你的王国里有一条n个头的恶龙,你希望雇佣一些骑士把它杀死(也就是砍掉所有的头).村里有m个骑士可以雇佣,一个能力值为 x 的骑士可以砍掉恶龙一个直径不超过 x 的头,且需要支付 x 个金币.如何雇佣骑 ...
- Android进度加载的Loading效果
网上看到的一个开源项目的loading效果,效果很赞,记录一下: 开源项目地址如下:https://github.com/RomainPiel/Titanic
- Android中FragmentPagerAdapter对Fragment的缓存(二)
上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...
- Mysql 的存储过程和存储函数
优点: v 提高安全性 v 简化mysql查询 v 减轻带宽负担 缺点: v 增加服务器负担 v 对我们开发者来说,难度大一点 PHP中的函数 Function funname(参数){ //函数体 ...