Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

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

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

Hello all! I found this problem on CodeChef: http://www.codechef.com/problems/PRYS09

I honestly don't know where to start. The brute force approach obviously won't work. Also, there are no solutions and editorials available probably because this is an old contest problem and remained unnoticed by the users. Please help!

Thanks!

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

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

I think it can be done with dynamic programming (to be able to count f (x) = the number of numbers up to x with the desired property, so the answer is f (R) — f (L-1)). The state is how many digits you've placed, whether or not you have already gone below x on some digit, and the current sum of cubes of digits mod m. Then you try each possible digit and get a new state for each valid digit.

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

    Thanks for the reply mkirsche! I am not very good at DP problems so I am not sure how to start writing the code. If you don't mind, can you please write a pseudocode? Thanks a lot! :)

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

    I tried . Got tle'd CODE

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

Calculate F(l, m, r) as number of numbers with length not more than l, such that sum of the cubes of their digits equals to r by modulo m. Then each query can be processed by O(sum_of_digits).

Lets di be the i-th digit of query number n and l its length. Iterate by prefixes, for each d < di adding to answer F(l - i - 1, m,  - (sum_of_cubes_on_prefixi - 1 + di3)%m).