Showing posts with label SPOJ. Show all posts
Showing posts with label SPOJ. Show all posts

Saturday, July 10, 2010

SPOJ - JEDNAKOST

Getting back to more programming after an inspirational summer camping trip. Saw one of my friends attempting this problem, thought I should give it a shot.

Recently solved problem #3752 - JEDNAKOS.

If you want to solve the problem yourself - stop reading now.

Solution - Recursion + Memoization

Easy problem really

1) Treat numbers starting with zero as special cases.

2) Let minplus[sum][pos] be an array that is the minimum number of pluses required to obtain the value 'sum' using the postfix starting at 'pos', then it's just simple recursion.

3) To avoid initialization, the array stores the number of pluses plus one.

Saturday, May 29, 2010

SPOJ - Flower Growing

Recently solved problem #4477 - FLOWGROW.

If you want to solve the problem yourself - stop reading now.

Solution - Combinatorics

The hard part is to calculate the number of possibilities to fill one row of flowers; to obtain the final answer, raise this number to the number of rows.

Let S(c,n) be the number of ways to fill n columns with exactly c colours, when only c colours are available:

S(1,n) = 1

S(2,n) = 2n-2xS(1,n)

S(3,n) = 3n-3xS(2,n)-3xS(1,n)

S(4,n) = 4n-4xS(3,n)-6xS(2,n)-4xS(1,n)

S(5,n) = 5n-5xS(4,n)-10xS(3,n)-10xS(2,n)-5xS(1,n)

S(6,n) = 6n-6xS(5,n)-15xS(4,n)-20xS(3,n)-15xS(2,n)-6xS(1,n)

S(7,n) = 7n-7xS(6,n)-21xS(5,n)-35xS(4,n)-35xS(3,n)-21xS(2,n)-7xS(1,n)

The factors being numbers from Pascal's triangle.

To calculate T(c,n), the number of ways to fill n columns with at least c colours from all 7 available colours:

T(7,n) = S(7,n)

T(6,n) = T(7,n)+7xS(6,n)

T(5,n) = T(6,n)+21xS(5,n)

T(4,n) = T(5,n)+35xS(4,n)

T(3,n) = T(4,n)+35xS(3,n)

T(2,n) = T(3,n)+21xS(2,n)

T(1,n) = T(2,n)+7xS(1,n)

The factors being numbers from the seventh row of Pascal's triangle.

When there are less than 7 columns, some of the calculations are skipped; the second part is only carried on as far as needed.

Faster optimizations are possible.
Note, "Time limit: 0.100s - 1.5s".

Woohoo! 60 problems!

Saturday, May 22, 2010

SPOJ - Rectangles Counting

Recently solved problem #3890 - MRECTCNT.

If you want to solve the problem yourself - stop reading now.

Solution

Count the number of pairs
(a,b), a ≤ b and a + b - GCD(a,b) = squares.

Note: Monkey counting is fast enough.

My SPOJ account

Saturday, May 15, 2010

SPOJ - Brackets Parade

Recently solved problem #4202 - BRPAR.

If you want to solve the problem yourself - stop reading now.

Solution: Catalan numbers & number of permutations.

The number of ways to order n pairs of matching brackets is

i.e. The Catalan numbers.

The number of ways to order n things of m different types, where there are a1, a2,..., am things of each type is

i.e. The number of permutations.

When multiplying, we can divide out n! and have:
Numerator = (2n)!
Divisor = (n+1)!a1!a2!...am!

We can calculate both quantities using modular multiplication, calculate the modular inverse of the divisor and then the modular product is the required answer.

This modular inversion uses the fact that and applies repeated squaring to calculate the exponentiation. We could have used extended Euclid instead.

Maybe one could have solved it by following the logical steps the problem outlined. Though I doubt it'll pass the time limit required. I, however, find this solution more fun.

My SPOJ account

Thursday, April 22, 2010

CCC 2009 Wireless

The Canadian Computing Competition is a prestigious programming competition for high school students with considerable programming experience willing to take on a challenge.

For over a year now, I have been rather struggling to solve the last problem from the 2009 senior section, Wireless. The reason I have been struggling was not that my solution didn't generate the correct outputs, but that the algorithm was not efficient enough to pass the SPOJ online judging system, with a rather strict time limit.

My original solution which was just straight forward 2D array processing, with an O(N log N) algorithm complexity failed to pass the judge as it exceeded the time limit. Mind you, this was implemented in Java, which I realize now is horrible for online competitions. Perhaps another time I will post my thoughts on Java vs C/C++.

Anyways, after several failed attempts, I decided to change my language to C++, and while talking with one of my friends, I came up with a rather odd sort and count solution that at the time I didn't think was sufficient enough.

Here is an outline of how it works:

On every row, all wirelesses have a (possibly empty) range of columns it covers.
This is translated into a set of commands for that row, consisting of a column number and the change in bitrate, with every wireless leading to two commands:
An increase in rate at the start of the range, and a similar decrease at the end of the range. By combining the commands and considering them in order of column number, the cumulative bitrates can be calculated over the range of the whole row.

NOTE: With N rows and K wirelesses, the complexity of this implementation is O(N*K*InK), the K*InK factor arising from the fact that a simple Quick sort is used to sort the commands, plus the fact that a Binary search is used to find the ranges within each row (Pythagoras). Better algorithms may be possible.

Once I coded this, to my surprise, it finally got accepted!
Time: 11.25 sec
MEM: 2.6 M

Now I have to prepare for the upcoming regional ECOO programming contest held at York University. Usually the problems are very trivial, so i'm not worried at all.

My SPOJ account

2009 CCC Wireless Solution C++