• Source
    1. #include<bits/stdc++.h>
    2.  
    3. using namespace std;
    4.  
    5. bool co_prime[103][103];
    6.  
    7. int arr[103],S,t;
    8.  
    9. void init()
    10. {
    11. int i,j;
    12.  
    13. for(i=1; i<=100; i++)
    14. {
    15. for(j=1; j<=100; j++)
    16. {
    17. if(__gcd(i,j)==1)
    18. {
    19. co_prime[i][j] = true;
    20. }
    21. }
    22. }
    23. }
    24.  
    25. bool check(int pos,int test)
    26. {
    27. for(int i=0; i<pos; i++)
    28. {
    29. if(!co_prime[arr[i]][test])
    30. {
    31. return false;
    32. }
    33. }
    34.  
    35. return true;
    36. }
    37.  
    38. void dfs(int pos,int start,int sum)
    39. {
    40. if(pos==t-1)
    41. {
    42. int j = S-sum;
    43.  
    44. if(check(pos,j))
    45. {
    46. for(int i=0; i<pos; i++)
    47. {
    48. printf("%d ",arr[i]);
    49. }
    50.  
    51. printf("%d\n",j);
    52.  
    53. return;
    54. }
    55. }
    56.  
    57. for(int i=start; i<=S; i++)
    58. {
    59. if(check(pos,i))
    60. {
    61. arr[pos] = i;
    62.  
    63. if(S-sum-i>=i)
    64. {
    65. dfs(pos+1,i,sum+i);
    66. }
    67. }
    68. }
    69. }
    70.  
    71. int main()
    72. {
    73. init();
    74.  
    75. int test,tc;
    76.  
    77. scanf("%d",&test);
    78.  
    79. for(tc=1; tc<=test; tc++)
    80. {
    81. scanf("%d%d",&S,&t);
    82.  
    83. printf("Case %d:\n",tc);
    84.  
    85. dfs(0,1,0);
    86. }
    87.  
    88. return 0;
    89. }