Advertisement
Saleh127

UVA 10637

Nov 5th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll int
  4. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  5. vector<ll>x;
  6. ll n,t;
  7.  
  8. bool valid(ll a)
  9. {
  10. for(auto i: x)
  11. {
  12. if(__gcd(i,a)!=1)
  13. {
  14. return 0;
  15. }
  16. }
  17. return 1;
  18. }
  19.  
  20. void backtrack(ll num,ll take,ll last)
  21. {
  22. if(num==0)
  23. {
  24. if(take==t)
  25. {
  26. for(ll i=0; i<t; i++)
  27. {
  28. if(i==0) cout<<x[i];
  29. else cout<<" "<<x[i];
  30. }
  31. cout<<endl;
  32. }
  33. return;
  34. }
  35.  
  36. if(take==t)
  37. {
  38. return;
  39. }
  40.  
  41. for(ll i=last; i<=num; i++)
  42. {
  43. if(valid(i)==1)
  44. {
  45. x.push_back(i);
  46. backtrack(num-i,take+1,i);
  47. x.pop_back();
  48. }
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. ios_base::sync_with_stdio(0);
  55. cin.tie(0);
  56. cout.tie(0);
  57.  
  58.  
  59.  
  60. test
  61. {
  62. cin>>n>>t;
  63. cout<<"Case "<<cs<<":"<<endl;
  64. backtrack(n,0,1);
  65. }
  66.  
  67.  
  68. return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement