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的函数就是母函数,可以用来解决组合问题(详细的可以参阅网上资料,也可以看下面两个简单应用)

  1. #include<stdio.h>
  2.  
  3. int c1[],c2[];
  4.  
  5. int main()
  6. {
  7. int n;
  8. while(~scanf("%d", &n))
  9. {
  10. int i;
  11. for(i = ;i <= n; i++)
  12. {
  13. c1[i] = ;
  14. c2[i] = ;
  15. }
  16. for(i =;i<=n;i++)//操作第i个括号
  17. {
  18. for(int j = ; j<= n;j++)//对于指数为j的进行操作
  19. {
  20. for(int k = ;k+j<=n;k+=i)//吧第i个的每一个数与之前的结果相乘
  21. {
  22. c2[j+k]+=c1[j];//j+k指数相加,他的值就是这个指数的系数
  23. }
  24. }
  25. for(int j = ;j<=n;j++)//系数保存在前面一个数组中
  26. {
  27. c1[j] = c2[j];
  28. c2[j] = ;
  29. }
  30. }
  31. printf("%d\n", c1[n]);
  32. }
  33. return ;
  34. }

另外,我还写了一个记忆化搜索的方法,虽然耗时耗空间,但是过了,挂在这里瞧瞧(15Ms,上面那个0Ms)

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<map>
  5. #include<vector>
  6. #include<set>
  7. #include<stack>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<stdlib.h>
  11. using namespace std;
  12. #define MAX(a,b) (a > b ? a : b)
  13. #define MIN(a,b) (a < b ? a : b)
  14. #define MAXN 400005
  15. #define INF 2000000007
  16. #define mem(a) memset(a,0,sizeof(a))
  17.  
  18. int ans[];
  19. int vis[][],d[][];
  20.  
  21. int dfs(int a, int b)
  22. {
  23. if(vis[a][b])return d[a][b];
  24. vis[a][b] = ;
  25. d[a][b] = ;
  26. for(int i = (a+)/; i <= a-b; i++)
  27. {
  28. d[a][b]+=dfs(i, a-i);
  29. }
  30. return d[a][b];
  31. }
  32.  
  33. void f()
  34. {
  35. ans[] = ;
  36. ans[] = ;
  37. memset(vis,,sizeof(vis));
  38. for(int i = ; i<= ; i++)
  39. {
  40. ans[i] = ;
  41. for(int j = ; i-j >= j; j++)
  42. {
  43. ans[i]++;
  44. if(i-j >= *j)
  45. {
  46. ans[i] += dfs(i-j, j);
  47. ans[i] --;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. f();
  56. int n;
  57. while(~scanf("%d",&n))
  58. {
  59. printf("%d\n",ans[n]);
  60. }
  61. return ;
  62. }

HDU1028Ignatius and the Princess III(母函数)的更多相关文章

  1. HDU1028Ignatius and the Princess III母函数入门

    这个题也能够用递归加记忆化搜索来A,只是因为这题比較简单,所以用来做母函数的入门题比較合适 以展开后的x4为例,其系数为4,即4拆分成1.2.3之和的拆分数为4: 即 :4=1+1+1+1=1+1+2 ...

  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 ...

  3. hdu--1028--Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  4. Ignatius and the Princess III(母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. HDU 1028Ignatius and the Princess III(母函数简单题)

     Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. 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 ...

  8. 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 ...

  9. Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数

    Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...

随机推荐

  1. 1156. Two Rounds(dfs+背包)

    1156 求出每个联通块的黑白块数 然后再背包 二维的背包 要保证每个块都得取一个 写的有些乱.. #include <iostream> #include<cstdio> # ...

  2. hdu 4941 Magical Forest ( 双重map )

    题目链接 题意: 有一个n*m的田地,里边有k棵树,每棵树的位置为(xi,yi),含有能量值ci.之后又q个询问,分三种; 1)1 a b,将a行和b行交换 2)2 a b,将a列和b列交换 3)3 ...

  3. hdu 4901 The Romantic Hero (dp)

    题目链接 题意:给一个数组a,从中选择一些元素,构成两个数组s, t,使s数组里的所有元素异或 等于 t数组里的所有元素 位于,求有多少种构成方式.要求s数组里 的所有的元素的下标 小于 t数组里的所 ...

  4. POJ 2112 - Optimal Milking

    原题地址:http://poj.org/problem?id=2112 题目大意:有K个挤奶机(标号为1 ~ K)和C头奶牛(编号为K + 1 ~ K + C),以邻接矩阵的方式给出它们两两之间的距离 ...

  5. Qt 多线程学习

    最近的项目上用到了关于多线程的知识,自己也比较感兴趣,所以就拿了那本<C++ GUI Qt4 编程>来学习. 这本书的第14章是关于多线程的知识,使用的Qt版本是Qt4.x.在下用的是最新 ...

  6. UVALive 2238 Fixed Partition Memory Management(二分完美匹配)

    题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ...

  7. macro names must be identifiers

    1.错把 #include 写成了 #define 会报这个错 2.定义一个不存在的宏业会报这个错,如加了-DANDRO 而ANDRO不存在

  8. CURL使用

    最近开发的游戏之中需要用到大量的客户端与服务端交互的 东西,开始参考大量的技术文章,感觉是五花八门,眼花缭乱.到后面,真正感受到,学习一门技术,还是需要从它最开始的东西开始学起,要不就是一头雾水,这种 ...

  9. Nginx & AWStats 安装、配置、使用

    —— 参考IBM文章:THIS , 不一样的指导顺序 —— 1. awstats分析nginx - access.log,网上资料大部分都是下载,然后配置.官网下载地址: http://awstats ...

  10. 【Unity3D】枪战游戏—发射子弹、射线检测

    一.子弹的碰撞检测: 因为子弹的移动速度非常的快,那么如果为子弹添加一个collider,就有可能检测不到了. 因为collider是每一帧在执行,第一帧子弹可能在100米处,那么下一帧就在900米处 ...