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 ...
随机推荐
- adb基本命令总结(Android Debug Bridge)
adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作 adb devices 列出你的devices adb kill-server 杀掉ad ...
- 【SymmetricDS】SymmetricDS是如何工作的
2018-04-20 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/8890785.html 本文翻译自SymmetricDS官方文档 ...
- 第三章 深入分析Java Web的中文乱码问题(待续)
几种常见的编码格式 在Java中需要编码的场景 在Java中如何编解码 在Java Web中涉及的编解码 在JS中的编码问题 常见问题分析 一种繁简转换的实现方式
- lucene、solr中的日期衰减方法-------function query --尚未测试在solr4.8
经常有一种情景是这样的:我们索引了N年的文章,而查询时候无论直接用相关度.或者用时间排序,都是比较鲁莽的:我们想要一种既要相关度比较高,又要时间上比较新的文章. 这时候的解决办法就是,自定义日期衰减的 ...
- js面试题知识点全解(一作用域)
问题: 1.说一下对变量提升的理解 2.说明this几种不同的使用场景 3.如何理解作用域 4.实际开发中闭包的应用 知识点: js没有块级作用域只有函数和全局作用域,如下代码: if(true){ ...
- Spring第五天
1. [简答题]:简述一下hibernate和spring框架的整合步骤: 答: 1.加入hibernate jar包 2.编写持久化类 3.添加Hibernate的配置文件:hibernate.cf ...
- 算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器
1. package algorithms.util; import algorithms.ADT.Stack; /****************************************** ...
- JavaPersistenceWithHibernate第二版笔记-第七章-001Mapping a set(@ElementCollection、@CollectionTable、@JoinColumn、)
一.结构 二.代码 1. package org.jpwh.model.collections.setofstrings; import org.jpwh.model.Constants; impor ...
- 之前在不网站看到过关于css的一些例子 今天自己也写了一个css特效
下面是代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- java全栈day33--html
本天要完成6个任务,并且布局静态页面(首页)详细分为六个部分 如下 网站信息页面案例(字体标签.排版标签) 网站图片信息页面案例(图片标签) 网站友情链接页面案例(列表标签) 网站首页案例(表格标签 ...