Count the coins: Difference between revisions

Content added Content deleted
Line 1,202: Line 1,202:
len cache[] 100000 * 7 + 6
len cache[] 100000 * 7 + 6
val[] = [ 1 5 10 25 50 100 ]
val[] = [ 1 5 10 25 50 100 ]
proc count sum kind . r .
func count sum kind .
if sum = 0
if sum = 0
r = 1
return 1
break 1
.
.
if sum < 0 or kind = 0
if sum < 0 or kind = 0
r = 0
return 0
break 1
.
.
chind = sum * 7 + kind
chind = sum * 7 + kind
if cache[chind] > 0
if cache[chind] > 0
r = cache[chind]
return cache[chind]
break 1
.
.
count sum - val[kind] kind r2
r2 = count (sum - val[kind]) kind
count sum kind - 1 r1
r1 = count sum (kind - 1)
r = r1 + r2
r = r1 + r2
cache[chind] = r
cache[chind] = r
return r
.
.
count 100 4 r
print count 100 4
print r
print count 10000 6
count 10000 6 r
print count 100000 6
print r
count 100000 6 r
# this is not exact, since numbers
# this is not exact, since numbers
# are doubles and r > 2^53
# are doubles and r > 2^53
print r
</syntaxhighlight>
</syntaxhighlight>