CodeForces Round #91

Problem A – Lucky Division – 2nd Top Submission  x1448

n=gets.to_i
puts [4,7,44,47,74,77,444,447,474,477,744,747,774,777].all?{|i|n%i>0}?:NO:"YES"

Problem B – Lucky Substring – 2nd Top Submission  x1273

s = gets
v,f,a=s.count("7"),s.count("4"),-1
a=4 if f>0
a=7 if v>f
puts a

Problem C – Lucky Sum – 1st Top Submission  x701

def f(v, a, b)
  v<<b if a>0
  f v,a+1,b*10+4 if a<10
  f v,a+1,b*10+7 if a<10
end
k,r = [],0
f(k,0,0)
k.sort!
a,b = gets.split.collect{|x| x.to_i }
0.upto(k.length) do |i|
    if a<=b && a<=k[i]
        r+=([b,k[i]].min-a+1)*k[i]
        a=k[i]+1
    end
end
puts r

Problem D – Lucky Transformation – 1st Top Submission  x389

l,n = gets.split.collect{|x| x.to_i}
s=gets
0.upto(l-1) do |i|
    if n!=0 && s[i,1]=='4' && s[i+1,1]=='7'
        n-=1
        if(i%2==0)
            s[i+1,1] = '4'
        else
            s[i,1] = '7'
            if i>0 && s[i-1,1]=='4'
                n = n&1
                n, s[i,1] = 0,'4' if(n==1)
            end
        end
    end
end
puts s
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: