fork download
  1. import std.algorithm;
  2. import std.array;
  3. import std.conv;
  4. import std.random;
  5. import std.range;
  6. import std.stdio;
  7.  
  8. auto gen (int n, int steps)
  9. {
  10. auto p = n.iota.array;
  11. foreach (step; 0..steps)
  12. {
  13. int x, y;
  14. do
  15. {
  16. x = uniform (0, n);
  17. y = uniform (0, n);
  18. }
  19. while (x == y);
  20. swap (p[x], p[y]);
  21. }
  22. return p;
  23. }
  24.  
  25. auto value (int [] a)
  26. {
  27. int n = a.length.to !(int);
  28. int [] ans;
  29. foreach (i; 0..n)
  30. {
  31. if (a[i] != -1)
  32. {
  33. int c = 0;
  34. for (int j = i; a[j] != -1; )
  35. {
  36. c += 1;
  37. int k = a[j];
  38. a[j] = -1;
  39. j = k;
  40. }
  41. ans ~= c;
  42. }
  43. }
  44. sort (ans);
  45. return ans;
  46. }
  47.  
  48. void main ()
  49. {
  50. rndGen.seed (1234);
  51.  
  52. int n = 1000;
  53. writeln ("Petr:");
  54. foreach (i; 0..10)
  55. {
  56. writeln ("#", i, ": ", value (gen (n, 3 * n)));
  57. }
  58. writeln ("Um_nik:");
  59. foreach (i; 0..10)
  60. {
  61. writeln ("#", i, ": ", value (gen (n, 7 * n + 1)));
  62. }
  63. }
  64.  
Success #stdin #stdout 0.01s 4528KB
stdin
Standard input is empty
stdout
Petr:
#0: [1, 1, 1, 1, 2, 3, 5, 7, 12, 14, 131, 822]
#1: [1, 2, 4, 32, 38, 123, 202, 598]
#2: [1, 1, 1, 1, 1, 30, 59, 142, 178, 586]
#3: [1, 1, 2, 2, 18, 24, 409, 543]
#4: [1, 1, 1, 1, 1, 2, 2, 3, 4, 16, 71, 76, 330, 491]
#5: [1, 1, 1, 2, 13, 17, 30, 30, 421, 484]
#6: [1, 1, 1, 3, 3, 37, 103, 119, 249, 483]
#7: [1, 1, 2, 6, 22, 38, 122, 128, 155, 525]
#8: [1, 1, 1, 2, 2, 6, 10, 127, 351, 499]
#9: [2, 14, 345, 639]
Um_nik:
#0: [2, 2, 6, 9, 30, 40, 75, 95, 741]
#1: [1, 190, 809]
#2: [2, 6, 15, 43, 48, 107, 180, 283, 316]
#3: [1, 1, 1, 4, 27, 44, 107, 227, 588]
#4: [1, 7, 10, 60, 76, 266, 580]
#5: [2, 5, 6, 9, 29, 35, 914]
#6: [1, 1, 2, 2, 3, 9, 32, 55, 83, 91, 210, 247, 264]
#7: [1, 1, 3, 12, 22, 27, 79, 372, 483]
#8: [1, 5, 7, 9, 10, 14, 17, 19, 25, 152, 741]
#9: [2, 5, 6, 10, 14, 21, 60, 147, 175, 275, 285]