D: Divide the pears

提交: 20 解决: 18

题目描述

Macro非常喜欢吃梨,有一天他得到了ACMICPC组委会送给他的一筐梨子。他比较心疼学生,就打算把梨子分给学生吃。现在他要把M个梨子放到N个盘子里面 (我们允许有的盘子为空) ,你能告诉Macro有多少种分法吗?

(请注意,如果有三个盘子,我们将5,1,1和1,1,5,视为同一种分法)

输入

第一行是一个整数t,代表有t组样例。

第二行有两个整数M 和 N 代表有M个梨和N个盘子。

输出

输出有多少种方法

样例输入

1
7 3

样例输出

8
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int dp(int m,int n)
 5 {
 6     if(n==1)
 7         return 1;
 8     if(m==0)
 9         return 1;
10     if(m<n)
11         return dp(m,m);
12     return dp(m-n,n)+dp(m,n-1);
13 }
14 15 int main()
16 {
17     int t;
18     scanf("%d",&t);
19     while(t--)
20     {
21         int m,n;
22         scanf("%d%d",&m,&n);
23         int ans=dp(m,n);
24         printf("%d\n",ans);
25     }
26     return 0;
27 } 
时间: 2024-08-13 08:08:27

D: Divide the pears的相关文章

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

29. Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. (1) log int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return INT_MAX; double t1 = log(fabs(dividend

codeforces 792C. Divide by Three

题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧.删除一个数字模3为M的或删除两个模3为3-M的(还有一些要删零),具体看代码. #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<strin

Divide Groups 二分图的判定

Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1835    Accepted Submission(s): 657 Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration mo

lintcode 中等题:Divide Two Integers 两个数的除法

题目 两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11 解题  15%的通过率,减法,位运算?表示不知道如何下手. 法一:利用减法,超时,人工直接去除的一些情况太流氓. public class Solution { /** * @param dividend the dividend * @param divisor the divisor * @return the re

Divide Two Integers

不能使用乘法,除法和mod operator,实现除法功能. Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 用减法可以实现,但是太慢了.算法里所做的优化都是为了节省时间. 不能忽视溢出的问题.例如,被除数是Integer.MIN_VALUE,除以-1,得到的结果对于Int型来说就溢出了,因此返回Integer.MAX_V

分治算法(Divide and Conquer)

分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并. 分治法所能解决的问题一般具有以下几个特征: 问题的规模缩小到一定的程度就可以容易地解决 问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质 利用该问题分解出的子问题的解可以合并为该问题的解 该问题所分解出的各个子问题是相互独立的,即子问题之间不包含公共的子问

[LeetCode] Divide Two Integers

In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can be use? Well, bit manipulations. Let's look at an example and see how bit manipulation might help.

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢