fork download
  1. use strict;
  2. use warnings;
  3. use feature qw(say);
  4.  
  5. my @pattern = (
  6. [1],
  7. [1, 2],
  8. [1, 3],
  9. [1, 2, 3, 4],
  10. [1, 5],
  11. [1, 2, 3, 4, 5, 6],
  12. [1, 3, 5, 7],
  13. [1, 2, 4, 5, 7, 8],
  14. );
  15.  
  16. my @mask = (0, 1, 2, 4, 8, 16, 32, 64, 128, 256);
  17.  
  18. sub permute {
  19. my ($n, $v, $b, $s) = @_;
  20.  
  21. if ($n < 1){
  22. $s->($v, $b);
  23. }
  24. foreach my $i (1..9){
  25. ($b & $mask[$i]) and next;
  26. permute($n - 1, $v * 10 + $i, $b | $mask[$i], $s);
  27. }
  28. }
  29.  
  30. my $total = 0;
  31.  
  32. foreach my $i (1..8){
  33. my %memo;
  34. permute($i, 0, 0, sub{
  35. my ($d, $db) = @_;
  36.  
  37. foreach my $dp (2..9){
  38. (not ($db & $mask[$dp]) or ($d % $dp)) and next;
  39. foreach my $np (@{$pattern[$dp]}){
  40. my $n = $d / $dp * $np;
  41. my $nb = $memo{$n} or next;
  42. (($nb ^ $mask[$np]) == ($db ^ $mask[$dp])) or next;
  43. # say "$n/$d";
  44. $total++;
  45. }
  46. }
  47.  
  48. $memo{$d} = $db;
  49. });
  50. }
  51.  
  52. say $total;
  53.  
Time limit exceeded #stdin #stdout 5s 19120KB
stdin
Standard input is empty
stdout
Standard output is empty