Блог пользователя manishbisht

Автор manishbisht, история, 9 лет назад, По-английски

Let's discuss the solutions of all the problems. I would like to know other peoples approaches for the problems. This time I am only able to solve Problem A. What are your views about the problems ?

Here are the problem statements.

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem A:

len is length of S.

let count(k) be the number of blue bulbs in range [1, k].

then the answer is count(J) — count(I — 1).

count(k) = count(k % len) + (k / len) * count(len)

for k <= len, we can store the answer in a table.

time complexity O(len)

»
9 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится -7 Проголосовать: не нравится

EDIT1: Here is the AC code. I have completely avoided overflows as I find f(b) ≥ 0 or not cleverly.(Not using log but finding the GP sum in O(log(VAL)) and returning when sum reaches  ≥ 1018

For Problem B, for given number N, you need to find the smallest number b such that there exists some x so that . Iterate x from 64 to 2 inclusive, and binary search for value of b.
Consider . This will be an non-decreasing function. So binary search for the value of b that makes it 0.
*Warning: Beware of overflows. See implementation above.

»
9 лет назад, скрыть # |
Rev. 8  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem B:

for B in range(1, N):

     binary search for nr_digit in range(2, 64), s.t. N has nr_digits "1" in base B

just iterator over B and nr_digit is also fine...

trick : use a multiprecision library to deal with large integer, like boost::multiprecision::mpz_int. It's allowed in codejam.

UPD: It's wrong. I didn't remember it clearly until I looked at my code... see comments below.

»
9 лет назад, скрыть # |
Rev. 6  
Проголосовать: нравится +3 Проголосовать: не нравится

Problem C: count the number of tuple (a, b, c, x) s.t.

(1) D divides x

(2) a >= 1, b >= 0, c >= 0

(3) a * x + b * (x + 1) + c * (x + 2) == N

for x = D; x <= N; x += D do

    for a = 1; a * x <= N; ++a do

         count number of (b, c) by solving indefinite equation.

time complexity O(N / D * N), but you have 4min to solve the problem. it's enough...

»
9 лет назад, скрыть # |
Rev. 5  
Проголосовать: нравится -8 Проголосовать: не нравится

I couldn't solve Problem D, but was trying in the following way.
When P = 2,

Observation: We are given a permutation of 1....N. We need to make it sorted.

If array is sorted answer would be N.
Otherwise if answer exists, then there would be 2 sub-arrays [L1, R1] and [L2, R2] such that

SubArray[1, L1 - 1] will contain all the numbers from 1 upto L1 - 1.

SubArray[L1, R1] would contain R1 - L1 + 1 consecutive elements with range [L2, L2 + R1 - L1].

SubArray[R1 + 1, L2 - 1] would contain consecutive elements with range [R2 - L2 + L1 + 1, L2 - 1].

SubArray[L2, R2] would contain consecutive elements with range [L1, R2 - L2 + L1].

SubArray[R2 + 1, N] would contain consecutive elements with range [L2 + R1 - L1 + 1, N].

I have not considered edge cases where L1 = 1 or R2 = N etc. Once I get the corresponding L2, R2 for given L1, R1 answer would be max(answer, 2 + dp[1][L1 - 1] + dp[R1 + 1][L2 - 1] + dp[R2 + 1][N]) where dp[i][j] is the maximum number of partitions of SubArray [i, j] so that when you sort those partitions , SubArray [i, j] also gets sorted.

Would be glad if someone could help what to do next.
UPD 1: Here is the code. To check for subarray [i, j] to have j - i + 1 consecutive elements, you can simply check max[i][j] - min[i][j] = j - i.

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For the large version of problem B just check for all divisors of (n-1). It worked well for me.