Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Saturday, August 31, 2013
Sunday, May 19, 2013
10515 - Powers Et Al.
(1) Divide n by 4
(2) If the remainder is r, then the last digit is ((m%10)^r)%10.
(3) if the remainder is zero, set r=4 and do (2).
Friday, May 17, 2013
Saturday, May 11, 2013
Friday, May 10, 2013
10015 - Joseph's Cousin
I used the hints from the Algorithmist. (http://www.algorithmist.com/index.php/UVa_10015)
I still have a high run time ~ 9sec but it was accepted since the limit is 10sec.
I still have a high run time ~ 9sec but it was accepted since the limit is 10sec.
(1) Prime Number: Represents the number of steps you will take
(2) If Prime Number > n, you will naturally do prime%n to
get the actual number of steps since it’s a circle
(3) You will perform/walk the steps from where you were
before, so you need to add the previous position (i + prime%n)
(4) take 1 point off since we are already standing on
position i and we don’t want to count it twice. (i + prime%n) – 1
(4) Again you need to mod since it’s a circle ((i + prime%n)
- 1) %n
(5) Kill the person standing at i+1 since we were counting
from i=0.
Person:
|
1
|
2
|
3
|
4
|
5
|
6
|
Prime = 2
Total = n = 6
Position = i = 0 (Start value)
Person to kill is at position: i + 1 where:
i = ( i + prime % n ) – 1 ) % n
i = (0 + 2 % 6) – 1) % 6 = 1
Kill person at position i+1 = 2
Person:
|
1
|
3
|
4
|
5
|
6
|
Prime = 3
n = 5
Position = i = 1
i = (1 + 3 % 5) – 1) % 5 = 3
Kill person at position = i + 1 = 4
Person:
|
1
|
3
|
4
|
6
|
Prime = 5
n = 4
Position = i = 3
i = (3 + 5 % 4) – 1) % 5 = 3
Kill person at position = i + 1 = 4
Person:
|
1
|
3
|
4
|
Prime = 7
n = 3
Position = i = 3
i = (3 + 7 % 3) – 1) % 3 = 1
Kill person at position = i + 1 = 1
Person:
|
3
|
4
|
Prime = 11
n = 2
Position = i = 1
i = (1 + 11 % 2) – 1) % 2 = 1
Kill person at position = i + 1 = 1
4 survived!
Source Code:
Thursday, May 9, 2013
10200 - Prime Time
I received many TLEs for this problem:
- Make sure you pre-compute enough primes ahead using Sieve of Eratosthenes
- Pay attention to the way you compute the primes (my original implementation was slow).
I also received two WAs because of how I was rounding the result:
- For some reason my rounding idea (commented in the code) didn't work, I googled the problem and I corrected my output.
- Make sure you pre-compute enough primes ahead using Sieve of Eratosthenes
- Pay attention to the way you compute the primes (my original implementation was slow).
I also received two WAs because of how I was rounding the result:
- For some reason my rounding idea (commented in the code) didn't work, I googled the problem and I corrected my output.
Sunday, May 5, 2013
Saturday, May 4, 2013
Friday, May 3, 2013
Subscribe to:
Posts (Atom)