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 ...
随机推荐
- NBU客户端备份失败,报错error 48 client hostname could not be found
今天在做备份时发现了这个报错.经过ping, nslookup, bpclntcmd命令检查没有发现连接或域名解析存在问题. 参考文档http://www.symantec.com/docs/TECH ...
- NSOperation/NSOperationQueue详细使用介绍
一.简介 (1)是使用GCD实现的一套Objective-C的API (2)是面向对象的线程技术 (3)提供了一些在GCD中不容易实现的特性,如:限制最大并发数量.操作之间的依赖关系 NSOp ...
- 链接ssh失败问题
Starting sshd:/var/empty/sshd must be owned by root and not group or world-writable. ...
- js常见的字符串及数组处理
最近工作设计前台比较多,由于好久没动前台,或者使用前台框架习惯了,js有点生,将常见的字符串处理忘了,在这里整理一下常见的,以便于查阅: 1.substr():字符串分割,第一个是开始的下标,第二个是 ...
- C++深度解析教程学习笔记(2)C++中的引用
1.C++中的引用 (1)变量名的回顾 ①变量是一段实际连续存储空间的别名,程序中通过变量来申请并命名存储空间 ②通过变量的名字可以使用存储空间.(变量的名字就是变量的值,&变量名是取地址操作 ...
- 安卓SQLite数据库操作(下)
在安卓开发中,数据库的操作无非就是增删改查.那么,这里我们通过例子来学习这四个操作. 我们先看代码吧.具体讲解后面说. 布局文件 activity_main.xml <LinearLayout ...
- css垂直居中方法(四)
第六种方法,使用css的writing-mode属性,结合margin:auto. 参考文章:改变CSS世界纵横规则的writing-mode属性 传统的web流中,margin设置auto值的时候, ...
- android wifi框架
---恢复内容开始--- frameworks/base/services/java/com/android/server/wifi 中的ReadMe文件 WifiService: Implement ...
- 进程与进程之间通信Manager
#!/usr/bin/env python from multiprocessing import Process,Manager #Manager进程与进程之间通信 def Foo(i,dic): ...
- JAVA input/output 流层次关系图
在java中,input和output流种类繁多,那么它们之间是否有关系呢?答案是肯定的,其中使用到了设计模式,装饰模式 下图来自于HEAD FIRST 设计模式 装饰模式一章 下图来自网络博客:ht ...