Integer sharding problem _ C ++, integer sharding _ c

Source: Internet
Author: User

Integer sharding problem _ C ++, integer sharding _ c

I. Problem background

Integer splitting refers to dividing an integer into the sum of several integers.

For example, 3 = 2 + 1 = 1 + 1 + 1.

We think that 2 + 1 and 1 + 2 are the same split.

Ii. Definition

In the integer n split, the maximum number of splits is m. Let's note that the number of splits is f (n, m)

That is, n = x1 + x2 +... + XK-1 + xk, arbitrary x ≤ m

Here we use the recursive Progressive Method

Iii. Recursive relationship

1. When n = 1 or m = 1

The splitting scheme is only n = 1 or n = 1 + 1 + 1 + ······

F (n, m) = 1

2. n = m

When S1 selects m, f (n, m) = 1, that is, n = m

F (n, m) = f (n, m) = f (n, s-1) = f (n, n-1) when m is not selected for S2. In this case, we will discuss the situation where the maximum number of splits is expressed as S-1.

F (n, m) = f (n, n-1) + 1

3. n <m hours

Because m cannot be selected, m can be considered as n. When n is m, f (n, m) = f (n, n)

4. n> m

When m is selected in S1, f (n, m) = f (n-m, m), the split score is changed to n-m because m is selected, in addition, the maximum number of m values may be selected in n-m.

When m is not selected for S2, f (n, m) = f (m-1). In this case, we will discuss the situation where the maximum number of splits is S-1.

F (n, m) = f (m-1) + f (n-m, m)

The total recurrence formula is

 

 

The Code is as follows:

 1 #include <algorithm> 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 using namespace std; 8  9 int f(int n,int m)10 {11     if ((n!=1)&&(m!=1))12         {13             if (n>m) return f(n-m,m)+f(n,m-1);14             else return 1+f(n,n-1);15         }16     else return 1;17 }18 void work()19 {20     int n,m;21     cin>>n>>m;22     cout<<f(n,m);23 }24 int main()25 {26     freopen("cut.in","r",stdin);27     freopen("cut.out","w",stdout);28     work();29     return 0;30 }

In addition, the master function method is provided. For more information, see

Http://blog.chinaunix.net/uid-26548237-id-3503956.html

 

 

 

All rights reserved. For details, contact the author.

QQ: 740929894

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.