fork download
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. #define endl "\n"
  4. using ll = long long ;
  5.  
  6. int n , k , a[100] , cnt;
  7. bool final = false;
  8. vector<vector<string>> res ;
  9.  
  10. void khoiTao(){
  11. cnt = 1 ;
  12. a[1] = n ;
  13. }
  14.  
  15.  
  16. void Sinh(){
  17. // bat dau tu so hang cuoi cung va tim so hang dau tien khac 1
  18. int i = cnt ;
  19. while(i >= 1 && a[i] == 1) --i ;
  20.  
  21. if( i == 0){
  22. final = true ;
  23. }
  24.  
  25. else{
  26. int tmp = cnt - i + 1 ;
  27. a[i]-- ; // giam a[i] 1 don vi
  28. cnt = i ; // gan lai cnt
  29. int q = tmp / a[i] ; // xem tmp gap may lan a[i]
  30. int r = tmp % a[i] ; // xem tmp chia du cho a[i] bang 0 hay kh
  31. for(int j = 1 ; j <= q ; j++){
  32. a[i + j] = a[i];
  33. ++cnt ;
  34. }
  35. if(r != 0){
  36. ++cnt ;
  37. a[cnt] = r ; // gan gia tri cuoi cho du
  38. }
  39. }
  40. }
  41.  
  42. int main(){
  43. ios::sync_with_stdio(false);
  44. cin.tie(nullptr);
  45. cout.tie(nullptr);
  46.  
  47. cin >> n ;
  48. khoiTao();
  49.  
  50. while(!final){
  51. vector<string> v ;
  52. for(int i = 1 ; i <= cnt ; i++){
  53. v.push_back(to_string(a[i]));
  54. if(i != cnt) v.push_back("+");
  55. }
  56. res.push_back(v);
  57. cout << endl;
  58. Sinh();
  59. }
  60.  
  61. cout << res.size() << endl ;
  62.  
  63. for(int i = 0 ; i < res.size() ; i++){
  64. for(auto x : res[i]){
  65. cout << x ;
  66. }
  67. cout << endl ;
  68. }
  69. }
  70.  
  71.  
  72.  
Success #stdin #stdout 0s 5300KB
stdin
5
stdout






7
5
4+1
3+2
3+1+1
2+2+1
2+1+1+1
1+1+1+1+1