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

Автор YouKnowCipher, история, 4 часа назад, По-английски

Problem Statement: Given an integer $$$N > 1$$$, determine the expected number of divisions needed to reduce $$$N$$$ to 1 by repeatedly dividing it by one of its divisors (including $$$N$$$). Output the expected number of steps to reduce it to 1, accurate to at least 5 decimal places. Constraints: There will be $$$T$$$ test cases, where $$$1 < T < 10^4$$$ and $$$1 < N < 10^5$$$.

My Approach: Defining an array $$$\text{dp}$$$ such that $$$\text{dp}[N]$$$ represents the expected number of steps needed to reduce $$$N$$$ to 1. The base case is:

$$$ \text{dp}[1] = 0 $$$

For each integer $$$N > 1$$$, we calculate $$$\text{dp}[N]$$$ using this:

$$$ \text{dp}[N] = \frac{1}{k} \sum_{d \mid N, d > 1} \left( \text{dp}\left[\frac{N}{d}\right] + 1 \right) $$$

where $$$k$$$ is the number of divisors of $$$N$$$ (excluding 1). The term $$$\text{dp}\left[\frac{N}{d}\right] + 1$$$ accounts for the expected steps needed after dividing $$$N$$$ by its divisor $$$d$$$ and includes the division step itself. Then, we iterate through all integers from $$$2$$$ to the maximum $$$N$$$ required, precomputing the values of $$$\text{dp}[N]$$$. This ensures that each test case can be answered in constant time $$$O(1)$$$ by simply returning $$$\text{dp}[N]$$$.

But implementing this approach is not giving AC & I can't find any drawbacks. Can someone help to identify any issue or any new approach? Thanks in advance for you time and effort.

Теги math, dp
  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится

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

you can use prime factorization

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

    Could you provide a bit more detail?

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

      use the sieve to prime factorize the number in O(logn) and the sum of the prime powers will be the ans

      20 = 2^2 *5^1

      2+1=3

      1.20/2=10

      2.10/2=5

      3.5/5= 1

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

        Got it. Thanks a bunch for your help.

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

Can I see your submission please?

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

    Yeah, sure. The main part:

    const int N = 100000;
    vector<double> precompute() {
        vector<double> dp(N + 1, 0.0);
        for (int n = 2; n <= N; n++) {
            int cnt = 0;
            double sum = 0.0;
            for (int d = 2; d * d <= n; d++) {
                if (n % d == 0) {
                    sum += dp[n / d] + 1;
                    cnt++;
                    if (d != n / d) {
                        sum += dp[d] + 1;
                        cnt++;
                    }
                }
            }
            if (cnt == 0) {
                dp[n] = 1;
            } else {
                dp[n] = sum / cnt;
            }
        }
        return dp;
    }