POJ1664 放苹果 (母函数)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 37515 | Accepted: 23090 |
Description
Input
Output
Sample Input
1
7 3
Sample Output
8
思路:多校见过类似的题目,简单母函数题目,不过比赛的时候,直接给出了解题分析:用的递归写的,感觉不是太理解。
递归:(见注释)
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head int fun(int m, int n) {
if(n == || m == )//如果剩1个盘子 || 没有苹果可以放
return ;
if(n > m)//如果盘子多于苹果,相当于去除多余盘子
return fun(m, m);
else //前者:所有盘子都有苹果,相当于每一个盘子都拿掉一个。后者:至少有一个空盘子
return fun(m-n, n) + fun(m, n-);
} int _, n, m;
int main() {
for(scanf("%d", &_);_;_--) {
scanf("%d%d", &m, &n);
printf("%d\n", fun(m, n));//m个苹果,n个盘子
}
}
母函数:
理解母函数就是一道裸题,写出各个式子,然后计算,系数即为答案。
有一点比较难理解。我其实一直以为是k++,因为每一项都是
,但是题目说了
1,1,5 和 5,1,1是同一种,所以k++会重复,所以用
,这样保证了不会重复。
可以理解为拿几个1,拿几个5,这样就避免了重复。
推荐博客:https://www.cnblogs.com/dolphin0520/archive/2012/11/07/2755080.html
母函数计算理解:http://blog.chinaunix.net/uid-26602509-id-3193699.html
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head
int c1[], c2[];//c1存展开式的系数,c2计算时保存
int _, m, n;
int main() {
for(scanf("%d", &_);_;_--) {
scanf("%d%d", &m, &n);
for(int i = ; i <= m; i++) {//初始化第一个表达式
c1[i] = ;
c2[i] = ;
}
for(int i = ; i <= n; i++) {//从第二个开始
for(int j = ; j <= m; j++) {//已经累乘的式子的第j项
for(int k = ; k+j <= m; k += i) {//k += i !!!
c2[j+k] += c1[j];
}
}
for(int i = ; i <= m; i++) {//更新
c1[i] = c2[i];
c2[i] = ;
}
}
printf("%d\n", c1[m]);//次数为m的系数即为答案
}
}
POJ1664 放苹果 (母函数)的更多相关文章
- poj1664 放苹果(DPorDFS)&&系列突破(整数划分)
poj1664放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33661 Accepted: 20824 Desc ...
- [POJ1664]放苹果(动态规划)
[POJ1664]放苹果 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第 ...
- poj1664放苹果(递归)
题目链接:http://poj.org/problem?id=1664 放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- poj1664 放苹果(递归)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1664 ------ ...
- [POJ1664] 放苹果 (动态规划,组合数学)
题目描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发(5,1,1和1,1,5是同一种方法) 输入输出格式 输入格式: 第一行是测试数据的数目t(0 <= ...
- POJ1664 放苹果
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...
- 放苹果(poj1664递归)
ti放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24392 Accepted: 15513 Descripti ...
- POJ1664:放苹果(线性dp)
题目: http://poj.org/problem?id=1664 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1 ...
- 放苹果(整数划分变形题 水)poj1664
问题:把M个相同的苹果放在N个相同的盘子里.同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 例子 : 1 7 3 ---------------8 ...
随机推荐
- 2014.10.1 Word技巧
设置每页都出现的表头 wordDoc.Tables[tab].Rows[1].HeadingFormat = (int)Word.WdConstants.wdToggle; //合并单元格 wordD ...
- Microsoft Office Visio 2010如何创建UML 用例图
转自:https://blog.csdn.net/mmoooodd/article/details/10513059 1..在Microsoft Office2010中打开Microsoft Visi ...
- mysql使用存储过程插入数据后,参数为中文的为?或乱码
最近了解了一下mysql存储过程,之前版本的mysql不支持存储过程,5.0版本后就可以支持存储过程的使用:恰好笔者下载使用版本为5.6.20: 做了一个给表插入数据的简单存储过程,发现打开表后汉字全 ...
- ListView显示Sqlite的数据
在安卓中,ListView和Sqlite都是十分常用的.这次我们来结合这个两个知识点写一个Demo. 功能:吧SQLite中的数据用ListView显示出来. 先看截图吧 首先是数据库 然后是运行截图 ...
- ssh框架搭建实例代码教程步骤
http://blog.csdn.net/u010539352/article/details/49255729
- sqlserver 查询int类型 in (字符串) 报转换int类型出错的问题
, , '') ) AS c_departNames FROM t_user AS A LEFT JOIN t_role AS B ON A.c_roleId=B.c_roleId 用 CHARIND ...
- nginx 启动、重启、关闭命令
一.启动 cd /usr/local/nginx/sbin ./nginx 二.重启 更改配置重启nginx kill -HUP 主进程号或进程号文件路径 或者cd /usr/local/ngi ...
- day36-hibernate检索和优化
连接查询是多表查询.
- set源码之心得
C++的STL很强大,强大到我只愿慵懒地去使用而不知其所以然.直到李师问我,我的回答被李师否定,我方才意识到自己是多么地浅陋.希望自己有空抽时间把STL源码给研究一下,化为自己真正可以掌控的力量. s ...
- Swing的MVC结构
--------------siwuxie095 工程名:TestMVC 包名:com.siwuxie095.mvc 类名:Test.java ...