Integer Division and Apple placement

Source: Internet
Author: User

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;
}

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.