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 ...
随机推荐
- 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...
- fil_space_create
/*******************************************************************//** Creates a space memory obje ...
- ADO和DAO的区别
ADO(ACTIVEX DATA OBJECTS)应用层的数据访问接口ODBC 数据库驱动接口OLE DB 系统级数据访问接口DAO (DATA ACCESS OBJECTS) 对象的数据访问接口AD ...
- [转]Jquery Ajax用法
原文地址:http://www.php100.com/html/program/jquery/2013/0905/6004.html jQuery学习之jQuery Ajax用法详解 来源: 时间 ...
- Azure SQL 数据库与新的数据库吞吐量单位
在这一期中,Scott 与 Azure SQL 数据库性能首席项目经理主管 Tobias Ternstrom 一起详细阐释了新的数据库吞吐量单位 (Database Throughput Unit, ...
- apache开源项目 -- tajo
一.体系架构 Tajo采用了Master-Worker架构(下图虚线框目前还在计划中),Master-Worker-Client之间的RPC通信是使用Protocol buffer + Netty来实 ...
- VS2010下编译安装DarwinStreamingServer5.5.5
源码下载链接:http://dss.macosforge.org/源码版本: 5.5.5版本电脑环境:visual studio2010,window 7 x64系统.用VS2010打开WinNTSu ...
- TransparentBlt、StretchBlt与BitBlt三个函数的区别
jackyhwei 发布于 2011-10-13 10:55 点击:1080次 来自:blog.csdn.net/weiqubo BOOL StretchBlt( int x, //指定目的矩形区域 ...
- 【转】Linux时间函数之gettimeofday()函数之使用方法
原文网址:http://blog.csdn.net/tigerjibo/article/details/7039434 一.gettimeofday()函数的使用方法: 1.简介: 在C语言中可以使用 ...
- Linux下通过ioctl系统调用来获取和设置网络信息
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...