Created
October 3, 2009 09:27
-
-
Save jeek/200537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $|=1; | |
| for ($n=1;$n<=7;$n++) { | |
| print "-\n"; $answer=""; | |
| @set=(); | |
| %bad=(); %good=(); | |
| $max=10**10; | |
| for ($i=1;$i<=$n;$i++) {push @set,$i;} | |
| while ($count<$max) { | |
| increment(); | |
| if (check()) { | |
| print "N=$n : "; | |
| $sum=0; | |
| foreach (@set) { | |
| print "$_ "; | |
| $sum+=$_; | |
| } | |
| print "\n"; | |
| if ($sum<$max) {$max=$sum;} | |
| } | |
| $count=$set[-1]; | |
| for ($i=0;$i+1<scalar @set;$i++) { | |
| $count+=int(@set[-1]/2)+$i; | |
| # print "$count $max\n"; | |
| } | |
| } | |
| # print "N=$n : $answer\n"; | |
| } | |
| sub increment() { | |
| @set=sort {$a<=>$b} @set; | |
| @set[0]++; | |
| for (my $i=0;$i<scalar @set;$i++) { | |
| for (my $j=1+$i;$j<scalar @set;$j++) { | |
| if ($set[$i]==$set[$j]) { | |
| $set[$j]++; | |
| if ($j==$n-1) {print @set[-1]."\n";} | |
| for (my $k=0;$k<$j;$k++) { | |
| $set[$k]=$k+1; | |
| } | |
| } | |
| } | |
| } | |
| @set=sort {$a<=>$b} @set; | |
| } | |
| sub check() { | |
| $depth++; | |
| if (1==scalar @set) { | |
| $depth--; | |
| return 1; | |
| } | |
| $string=join " ",sort {$a<=>$b} @set; | |
| if (2<scalar @set) { | |
| @taco=sort {$a<=>$b} @set; | |
| if (@taco[0]+@taco[1]<@taco[-1]) { | |
| $depth--; | |
| $bad{$string}=1; | |
| return 0; | |
| } | |
| if (4<scalar @set) { | |
| if (@taco[0]+@taco[1]+@taco[2]<@taco[-2]+@taco[-1]) { | |
| $depth--; | |
| $bad{$string}=1; | |
| return 0; | |
| } | |
| } | |
| } | |
| if (4<=scalar @set) { | |
| for ($i=0;$i+3<scalar @set;$i++) { | |
| if ($set[$i]+$set[$i+3]==$set[$i+1]+$set[$i+2]) { | |
| $depth--; | |
| $bad{$string}=1; | |
| return 0; | |
| } | |
| } | |
| } | |
| if ($good{$string}) { | |
| $depth--; | |
| return 1; | |
| } | |
| if ($bad{$string}) { | |
| $depth--; | |
| return 0; | |
| } | |
| my $sum=0; foreach (@set) { $sum+=$_;} | |
| if ($sum>=$max) { | |
| $depth--; | |
| $string=join " ",sort {$a<=>$b} @set;$bad{$string}=1;return 0; | |
| } | |
| if ($n==scalar @set) { | |
| @taco=sort {$a<=>$b} @set; | |
| for (my $i=1;$i<scalar @taco;$i++) { | |
| if ($taco[$i-1] *2 < $taco[$i]) { | |
| $depth--; | |
| $string=join " ",sort {$a<=>$b} @set;$bad{$string}=1;return 0; | |
| return 0; | |
| } | |
| } | |
| } | |
| # print "$answer"; | |
| # foreach (1..$depth) {print " ";} | |
| # foreach (sort {$a<=>$b} @set) {print "$_ ";} | |
| # print "\n"; | |
| for (my $i=1;$i<2**(scalar @set)-1;$i++) { | |
| # print "$i\n"; | |
| my @left=(); my @right=(); | |
| my $sumleft=0; my $sumright=0; | |
| for (my $j=0;$j<(scalar @set);$j++) { | |
| # print "$j "; | |
| if ($i&(2**$j)) { | |
| push @left,$set[$j]; | |
| # print "<- ".$set[$j]."\n"; | |
| $sumleft+=$set[$j]; | |
| } else { | |
| push @right,$set[$j]; | |
| # print "-> ".$set[$j]."\n"; | |
| $sumright+=$set[$j]; | |
| } | |
| } | |
| if ($sumright==$sumleft) { | |
| $depth--; | |
| $string=join " ",sort {$a<=>$b} @set;$bad{$string}=1;return 0; | |
| } | |
| if ((scalar @left)>(scalar @right) & ($sumright>$sumleft)) { | |
| $depth--; | |
| $string=join " ",sort {$a<=>$b} @set;$bad{$string}=1;return 0; | |
| } | |
| if ((scalar @left)<(scalar @right) & ($sumright<$sumleft)) { | |
| $depth--; | |
| $string=join " ",sort {$a<=>$b} @set;$bad{$string}=1;return 0; | |
| } | |
| for (my $i=0;$i<scalar @set;$i++) { | |
| my $temp=shift @set; | |
| if (!check()) { | |
| unshift @set,$temp; | |
| @set=sort {$a<=>$b} @set; | |
| $depth--; | |
| $string=join " ",sort {$a<=>$b} @set;$bad{$string}=1;return 0; | |
| } | |
| push @set,$temp; | |
| } | |
| } | |
| $string=join " ",sort {$a<=>$b} @set; | |
| if ($n==scalar @set) { | |
| my $sum=0; | |
| foreach (@set) { | |
| $sum+=$_; | |
| } | |
| if ($sum<$max) {$max=$sum;$answer=$string;} | |
| } else { | |
| $good{$string}=1; | |
| } | |
| $depth--; | |
| return 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment