-
-
Save yaasita/3be9ca5ca85dae2f2b478489044804e4 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use feature qw(:5.10); | |
use utf8; | |
use bigint; | |
my @combination; | |
my ($A, $B, $K); | |
{ | |
my $line = <STDIN>; | |
($A, $B, $K) = split(/\s/, $line); | |
} | |
my $cursor = 0; | |
my $result = ""; | |
while (1) { | |
if ($A == 0 or $B == 0){ | |
$result .= "b" x $B; | |
$result .= "a" x $A; | |
last; | |
} | |
my $n = count_pattern($A-1, $B); | |
if ($n + $cursor < $K){ | |
$cursor += $n; | |
$result .= "b"; | |
$B--; | |
} | |
else { | |
$result .= "a"; | |
$A--; | |
} | |
} | |
say $result; | |
sub count_pattern { | |
my $a_num = shift; | |
my $b_num = shift; | |
return combination($a_num + $b_num, $a_num); | |
} | |
sub combination { | |
my $c1 = shift; | |
my $c2 = shift; | |
if (!defined($combination[$c1][$c2])){ | |
my $numerator = 1; | |
for(my $i=0;$i < $c2;$i++){ | |
$numerator *= $c1 - $i; | |
} | |
my $denominator = 1; | |
for(my $i=0;$i < $c2;$i++){ | |
$denominator *= $c2 - $i; | |
} | |
$combination[$c1][$c2] = $numerator / $denominator; | |
} | |
return $combination[$c1][$c2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment