题意:中文题目,不解释。。。

题解:

第一种方法是暴力深搜:枚举盘子1~n放苹果数量的所有情况,不需要剪枝;将每次枚举的情况,即每个盘的苹果数量,以字典序排序,然后存进set里 以此去重像"5 1 1"和"1 5 1"这种相同情况。

 /**
* @author Wixson
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map>
#include <set>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int n,m;
set<string> s;
set<string>::iterator ite;
int ans;
char str[];
void dfs(int pos,int left)
{
if(pos==n-)
{
str[pos]=''+left,str[n]='\0';
string temp=str;
sort(temp.begin(),temp.end());
if((ite=s.find(temp))==s.end())
{
s.insert(temp);
ans++;
}
return;
}
//
for(int i=;i<=left;i++)
{
str[pos]=''+i;
dfs(pos+,left-i);
}
}
int main()
{
//freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
ans=;
s.clear();
dfs(,m);
printf("%d\n",ans);
}
return ;
}

第二种方法: 递推。利用dp的思想,来看下样例:将7个苹果放进3个盘子里,可以分2种情况考虑:1.空着一个盘子不放,即将7个苹果放进2个盘子里;2.先每个盘子均放进一个苹果,然后将剩下的4个苹果放进3个盘子里。即dp[m][n]=dp[m-1][n]+dp[m-n][n](m>=n),  另外,显而易见 dp[m][n]=dp[m][m](m<n),dp[m][n]=1(m或n中有为1或0时);

 /**
* @author Wixson
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <map>
#include <set>
const int inf=0x3f3f3f3f;
const double PI=acos(-1.0);
const double EPS=1e-;
using namespace std;
typedef long long ll;
typedef pair<int,int> P; int n,m;
int dp(int m,int n)
{
if(!n||!m||m==||n==) return ;
if(m>=n) return dp(m,n-)+dp(m-n,n);
else return dp(m,m);
}
int main()
{
//freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
printf("%d\n",dp(m,n));
}
return ;
}

poj 1664 放苹果 (划分数)的更多相关文章

  1. poj 1664 放苹果(递推)

    题目链接:http://poj.org/problem? id=1664 放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  2. poj 1664 放苹果 递归

    题目链接: http://poj.org/problem?id=1664 题目描述: 有n个苹果,m个盒子,盒子和苹果都没有顺序,盒子可以为空,问:有多少种放置方式? 解题思路: 当前有n个苹果,m个 ...

  3. POJ 1664 放苹果 (递推思想)

    原题链接:http://poj.org/problem?id=1664 思路:苹果m个,盘子n个.假设 f ( m , n ) 代表 m 个苹果,n个盘子有 f ( m , n ) 种放法. 根据 n ...

  4. POJ 1664 放苹果

    放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24985   Accepted: 15908 Description ...

  5. poj 1664放苹果(递归)

    放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37377   Accepted: 23016 Description ...

  6. poj 1664 放苹果(dfs)

    放苹果 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30284   Accepted: 19098 Description ...

  7. POJ 1664 放苹果 (递推)

    题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...

  8. OpenJudge/Poj 1664 放苹果

    1.链接地址: http://bailian.openjudge.cn/practice/1664 http://poj.org/problem?id=1664 2.题目: 总时间限制: 1000ms ...

  9. POJ 1664 放苹果(递归或DP)

    一.Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t ...

随机推荐

  1. 题解报告:hdu 1575 Tr A

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  2. JSP所需要掌握的部分

    JSP基本语法 指令 <%@ 指令%> JSP指令是JSP的引擎 主要的两种指令是page和include(taglib) <%@ page import="java.ut ...

  3. the interview questions of sql server

    1.一道SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-0 ...

  4. overflow:解决 div的高度塌陷问题

    高度塌陷是如何引起的? 解析: 当一个 div中所有的子  div都进行了浮动后,那么会出现该问题,那么解决方就是在父 div中 设置其  overflow:hidden;即可解决高度塌陷问题. 方式 ...

  5. 【转】utf-8的中文是一个汉字占三个字节长度

    因为看到百度里面这个人回答比较生动,印象比较深刻,所以转过来做个笔记 原文链接 https://zhidao.baidu.com/question/1047887004693001899.html 知 ...

  6. jquery ajax在IE9以下进行跨域请求时无效的问题

    第一步:设置浏览器安全属性,启用[通过域访问数据源]选项: 1.选择Internet选项 2.选择安全---自定义级别 3.找到其他---通过域访问数据源,选择启用,然后确定就可以了. 第二步:调用a ...

  7. 仿iphone动态萤火虫锁屏应用安卓源码

    该源码是仿iphone动态萤火虫锁屏应用源码,源码SkyLock,这也是最近弄了一款锁屏,苦于市场百般阻拦与锁屏应用数量实在太多,于是将它拿出来开源:废话不多说,希望大家能够希望,更多说明请看下面的吧 ...

  8. jstree CHECKBOX PLUGIN

    The checkbox plugin makes multiselection possible using three-state checkboxes. Configuration overri ...

  9. django-Celery分布式队列简单使用

    介绍: Celery 是一个简单.灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具. 它是一个专注于实时处理的任务队列,同时也支持任务调度. worker:是一个独立的进程, ...

  10. AcGePoint3d ads_point 转换

    AcGePoint3d (AcGePoint2d )转换 ads_point 用:asDblArray函数. ads_point 转换AcGePoint2d  用asPnt2d(const doubl ...