Guest User

Untitled

a guest
Jan 17th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. ## ソースコード
  2.  
  3. ```cpp
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. const int MAX_N=100;
  7. int f;
  8. int v[MAX_N];
  9. int N,R;
  10.  
  11. void combination(int now,int cnt,int goal){
  12. if(cnt==goal){
  13. for(int i=0;i<N;i++)if(v[i])cout<<i;
  14. cout<<endl;
  15. return;
  16. }
  17. for(int i=now;i<N;i++){
  18. if(!(f&(1<<i))){
  19. v[i]=1;
  20. f|=(1<<i);
  21. combination(i+1,cnt+1,goal);
  22. v[i]=0;
  23. f&=~(1<<i);
  24. }
  25. }
  26. }
  27.  
  28. void permutation(int now,int cnt,int goal){
  29. if(cnt==goal){
  30. for(int i=0;i<goal;i++)cout<<v[i];
  31. cout<<endl;
  32. return;
  33. }
  34. for(int i=0;i<N;i++){
  35. if(!(f&(1<<i))){
  36. v[now]=i;
  37. f|=(1<<i);
  38. permutation(now+1,cnt+1,goal);
  39. v[now]=-1;
  40. f&=~(1<<i);
  41. }
  42. }
  43. }
  44. int main() {
  45. cin>>N>>R;
  46. cout<<"permutation"<<endl;
  47. for(int i=0;i<N;i++)v[i]=-1;
  48. permutation(0,0,R);
  49. cout<<endl;
  50. cout<<"combination"<<endl;
  51. for(int i=0;i<N;i++)v[i]=0;
  52. combination(0,0,R);
  53. cout<<endl;
  54. return 0;
  55. }
  56. ```
  57.  
  58. ## 実行例
  59.  
  60. ```
  61. 4 2
  62. permutation
  63. 01
  64. 02
  65. 03
  66. 10
  67. 12
  68. 13
  69. 20
  70. 21
  71. 23
  72. 30
  73. 31
  74. 32
  75.  
  76. combination
  77. 01
  78. 02
  79. 03
  80. 12
  81. 13
  82. 23
  83. ```
Add Comment
Please, Sign In to add comment