The problem of integer division and continuous division of positive integers: http://www.cnblogs.com/nokiaguy/archive/2008/05/11/1192308.htmlis very detailed.
Put the apple question: Put M apple in N plate, allow free, how many ways to put http://acm.tzc.edu.cn/acmhome/problemdetail.do? & Method = showdetail & id = 2679
Recursive equation: F (m, n) = f (M, n-1) + f (m-N, N) // There are two methods for each score
F (M, n-1): Put M apples on n-1 plates, that is, at least one empty
F (m-N, N): First put an apple on each plate, and then put the remaining Apple (m-N) into N plates, that is to say, each plate contains at least one
1. n = 1 | M = 1 that is, there is only one plate or only one apple. Obviously, there is only one method;
2. m <n indicates that the number of apples is smaller than the number of plates. In this case, F (m, n) ---> F (M, m)
3. M = N at this time, each plate has at least one case, so it is 1 + f (M, n-1)
4. m> n f (M, n-1) + f (m-N, N) (as mentioned in the beginning)
#include <stdio.h>
int f(int m , int n)
{
if(m == 1 || n== 1 || m == 0) return 1;
if(m < n) return f(m, m);
return f(m - n, n) + f(m, n - 1);
}
int main(void)
{
int n , m , z;
scanf("%d", &z);
while(z-- > 0)
{
scanf("%d%d", &m ,&n);
printf("%d\n",f(m, n));
}
return 0;
}
For the continuous division of integers, I also wrote:
I * x + I * (I-1)/2 = N (I indicates the number of partitions, and X indicates the start number );
I * (I + 1)/2 <= N (upper limit of I );
#include <stdio.h>
void split(int n)
{
int i , t , x , k;
for(i = 1 ; (t = i * (i - 1) / 2) < n ; i++)
{
if(n - t < 0 || (n - t) % i) continue;
x = (n - t) / i;
for(k = 0 ; k < i ; k++)
{
printf(k == i - 1 ? "%d\n" : "%d + ", x + k);
}
}
}
int main(void)
{
int n;
scanf("%d", &n);
split(n);
return 0;
}