fork download
  1. import std.algorithm;
  2. import std.array;
  3. import std.range;
  4. import std.stdio;
  5.  
  6. immutable int NA = -1;
  7.  
  8. auto mult (int [] a, int [] b)
  9. {
  10. auto n = a.length;
  11. auto res = new int [n];
  12. foreach (i; 0..n)
  13. {
  14. if (b[i] != NA)
  15. {
  16. res[i] = a[b[i]];
  17. }
  18. }
  19. return res;
  20. }
  21.  
  22. void main ()
  23. {
  24. immutable int n = 14;
  25. auto b = n.iota.array;
  26. foreach (j; 0..2)
  27. {
  28. bringToFront (b[j * 7 + 0..j * 7 + 1],
  29. b[j * 7 + 1..j * 7 + 7]);
  30. }
  31. debug {writeln (b);}
  32. auto a = new int [n];
  33. a[] = NA;
  34. auto u = new bool [n];
  35.  
  36. bool check ()
  37. {
  38. int num1 = 0;
  39. int num3 = 0;
  40. foreach (i; 0..n)
  41. {
  42. if (a[i] != NA)
  43. {
  44. if (a[i] == i)
  45. {
  46. num1 += 1;
  47. if (num1 > 2)
  48. {
  49. return false;
  50. }
  51. }
  52. else
  53. {
  54. if (a[a[i]] != NA)
  55. {
  56. if (a[a[i]] == i)
  57. {
  58. return false;
  59. }
  60. else if (a[a[a[i]]] != NA)
  61. {
  62. if (a[a[a[i]]] == i)
  63. {
  64. num3 += 1;
  65. if (num3 > 4)
  66. {
  67. return false;
  68. }
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79.  
  80. auto ab = mult (a, b);
  81. foreach (i; 0..n)
  82. {
  83. if (ab[i] != NA)
  84. {
  85. if (ab[i] == i)
  86. {
  87. return false;
  88. }
  89. if (ab[ab[i]] != NA)
  90. {
  91. if (ab[ab[i]] != i)
  92. {
  93. return false;
  94. }
  95. }
  96. }
  97. }
  98. return true;
  99. }
  100.  
  101. void recur (int k)
  102. {
  103. if (k == n)
  104. {
  105. writeln (a);
  106. return;
  107. }
  108. debug {writeln (a);}
  109. if (!check ())
  110. {
  111. return;
  112. }
  113. foreach (i; 0..14)
  114. {
  115. if (!u[i])
  116. {
  117. u[i] = true;
  118. a[k] = i;
  119. recur (k + 1);
  120. u[i] = false;
  121. }
  122. }
  123. a[k] = NA;
  124. }
  125.  
  126. recur (0);
  127. }
  128.  
Success #stdin #stdout 0.16s 2704KB
stdin
Standard input is empty
stdout
Standard output is empty