From: Calamitas Date: 2014-06-11T16:28:37+02:00 Subject: [ruby-core:63108] Re: [ruby-trunk - Bug #9932] [Open] Permutation - Segment fault --e89a8fb1f15e90c5ba04fb90421e Content-Type: text/plain; charset=UTF-8 On Wed, Jun 11, 2014 at 1:12 PM, wrote: > Execute the following program with the inputs > > > ###################################################################################### > # hash.rb > > t = gets.chomp.to_i > > def hash str > res = str.count "A" > if str.size > 1 > s1 = str[0..(str.size/2 - 1)] > s2 = str[(str.size/2)..-1] > #p "#{s1}, #{str}" > res += [hash(s1), hash(s2)].max > end > > res > end > #puts hash("AAEAAE") > > t.times do |x| > a = gets.chomp.to_i > e = gets.chomp.to_i > v = gets.chomp.to_i > > pos = ("A"*a + "E"*e).split('').permutation.map(&:join).uniq > > r = 0 > > pos.each do |y| > r += 1 if(hash(y)==v) > end > > puts (r % 1000000007) > end > > > ###################################################################################### > > $ ruby hash.rb > $ 1 > $ 234234 > $ 224 > $ 754674567 > > Then the follwing error is produced > > This looks a lot like a solution to a programming contest problem. Can you provide a link to the problem statement? The number of permutations you are asking for is humongous: (234234 + 224)! which, if my calculations are right, is between 10 ** 1157233 and 10 ** 1157234. There won't be a computer that can calculate and store that many permutations for a long time (if ever). Peter --e89a8fb1f15e90c5ba04fb90421e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Wed, Jun 11, 2014 at 1:12 PM, <tak2siva@gmail.com> wrote:
Execute the following program with the inputs

###########################################################################= ###########
# hash.rb

t =3D gets.chomp.to_i

def hash str
=C2=A0 =C2=A0 =C2=A0 =C2=A0 res =3D str.count "A"
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if str.size > 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 s1 =3D str[0..(str.= size/2 - 1)]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 s2 =3D str[(str.siz= e/2)..-1]
#p "#{s1}, #{str}"
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 res +=3D [hash(s1),= hash(s2)].max
=C2=A0 =C2=A0 =C2=A0 =C2=A0 end

=C2=A0 =C2=A0 =C2=A0 =C2=A0 res
end
#puts hash("AAEAAE")

t.times do |x|
=C2=A0 =C2=A0 =C2=A0 =C2=A0 a =3D gets.chomp.to_i
=C2=A0 =C2=A0 =C2=A0 =C2=A0 e =3D gets.chomp.to_i
=C2=A0 =C2=A0 =C2=A0 =C2=A0 v =3D gets.chomp.to_i

=C2=A0 =C2=A0 =C2=A0 =C2=A0 pos =3D ("A"*a + "E"*e).spl= it('').permutation.map(&:join).uniq

=C2=A0 =C2=A0 =C2=A0 =C2=A0 r =3D 0

=C2=A0 =C2=A0 =C2=A0 =C2=A0 pos.each do |y|
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 r +=3D 1 if(hash(y)= =3D=3Dv)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 end

=C2=A0 =C2=A0 =C2=A0 =C2=A0 puts (r % 1000000007)
end

###########################################################################= ###########

$ ruby hash.rb
$ 1
$ 234234
$ 224
$ 754674567

Then the follwing error is produced


This looks a lot like a solution to a = programming contest problem. Can you provide a link to the problem statemen= t?

The number of permutations you are asking for i= s humongous: (234234 + 224)! which, if my calculations are right, is betwee= n 10 ** 1157233 and 10 ** 1157234. There won't be a computer that can c= alculate and store that many permutations for a long time (if ever).

Peter
--e89a8fb1f15e90c5ba04fb90421e--