【HPUoj】Divide the pears(组合数学)

该博客介绍了HPUoj中关于如何将梨子公平地分配到多个盘子里的问题,属于组合数学的应用。题目要求在允许盘子为空的情况下,计算将M个梨分配到N个盘子的方案数。博客提供了样例输入和输出,并提到简单的递归解决方案或通过预计算数据来避免超时。
摘要由CSDN通过智能技术生成

题目链接:点击打开链接

问题 D: Divide the pears

时间限制: 1 Sec  内存限制: 128 MB

提交: 3   解决: 3   状态

题目描述

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

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

输入

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

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

输出

输出有多少种方法

样例输入

1
7 3

样例输出

8



参考:点击打开链接


数据应该不大,递归就可以,如果卡时间的话,可以考虑打表。


代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define LL long long
#define CLR(a,b) memset(a,b,sizeof(a))
int f(int m,int n)
{
	if (n == 1)
		return 1;
	if (m == 0)
		return 1;
	if (m < n)
		return f(m,m);
	return f(m-n,n) + f(m,n-1);		//盘子不空 + 至少空一个 
}
int main()
{
	int u;
	scanf ("%d",&u);
	while (u--)
	{
		int m,n;		//m为梨数,n为盘子数 
		scanf ("%d %d",&m,&n);
		printf ("%d\n",f(m,n));
	}
	return 0;
}


The Cortex-M0 processor does not have a hardware divider, which means that division calculations are performed using software routines. There are various algorithms for performing software division, but one commonly used method is called "long division". In long division, the divisor is repeatedly subtracted from the dividend until the remainder is less than the divisor. The number of times the divisor is subtracted is the quotient, and the remainder is the final result. This process is repeated until all digits of the dividend have been processed. Here is a sample code for performing integer division on Cortex-M0 using long division: ``` int divide(int dividend, int divisor) { int quotient = 0, remainder = 0; int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1; // convert both operands to positive if (dividend < 0) dividend = -dividend; if (divisor < 0) divisor = -divisor; // perform long division for (int i = 31; i >= 0; i--) { remainder <<= 1; // left shift remainder remainder |= (dividend >> i) & 1; // add next bit from dividend to remainder if (remainder >= divisor) { remainder -= divisor; quotient |= (1 << i); // set corresponding bit in quotient } } // apply sign quotient = sign * quotient; return quotient; } ``` Note that this code assumes that both the dividend and divisor are 32-bit integers. It also handles negative operands correctly and applies the correct sign to the result. However, it may not be the most efficient implementation and may need to be optimized for specific use cases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值