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