Sort by

recency

|

235 Discussions

|

  • + 0 comments
    #include <stdio.h>
    #include <math.h>
    #include <limits.h>
    
    #define MAX_N 1000001
    
    int dp[MAX_N];
    
    void precompute_min_moves() {
        dp[0] = 0;
        dp[1] = 1;
    
        for (int i = 2; i < MAX_N; i++) {
            // Option 1: Decrement
            dp[i] = 1 + dp[i - 1];
    
            // Option 2: Factorization
            // We only need to find factors up to sqrt(i)
            for (int j = 2; j * j <= i; j++) {
                if (i % j == 0) {
                    int largest_factor = i / j;
                    if (dp[i] > 1 + dp[largest_factor]) {
                        dp[i] = 1 + dp[largest_factor];
                    }
                }
            }
        }
    }
    
    int main() {
        precompute_min_moves();
    
        int Q;
        scanf("%d", &Q);
    
        while (Q--) {
            int N;
            scanf("%d", &N);
            printf("%d\n", dp[N]);
        }
    
        return 0;
    
  • + 0 comments

    include

    include

    include

    define MAX_N 1000001

    int dp[MAX_N];

    void precompute_min_moves() { dp[0] = 0; dp[1] = 1;

    for (int i = 2; i < MAX_N; i++) {
        // Option 1: Decrement
        dp[i] = 1 + dp[i - 1];
    
        // Option 2: Factorization
        // We only need to find factors up to sqrt(i)
        for (int j = 2; j * j <= i; j++) {
            if (i % j == 0) {
                int largest_factor = i / j;
                if (dp[i] > 1 + dp[largest_factor]) {
                    dp[i] = 1 + dp[largest_factor];
                }
            }
        }
    }
    

    }

    int main() { precompute_min_moves();

    int Q; scanf("%d", &Q);

    while (Q--) { int N; scanf("%d", &N); printf("%d\n", dp[N]); }

    return 0;[](https://)

  • + 1 comment

    def downToZero(n): if n == 0: return 0 queue = deque([(n, 0)]) visited = set([n])

    while queue:
        current, moves = queue.popleft()        
    
        if current == 0:
            return moves
    
        # Operation 1
        if current - 1 not in visited:            
            queue.append((current - 1, moves + 1))
            visited.add(current - 1)
    
        # Operation 2
        for i in range(2, int(current**0.5) + 1):
            if current % i == 0:
                factor = max(i, current // i)
                if factor not in visited:
                    queue.append((factor, moves + 1))
                    visited.add(factor)
    
    return -1
    
  • + 0 comments
    
    

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    int testCase(int n) { if (n == 0) { return 0; }

    priority_queue<int, vector<int>, greater<int>> pq;
    unordered_map<int, int> moves;
    
    pq.push(n);
    moves.insert(make_pair(n, 0));
    
    int minMoves = 1e9;
    while (!pq.empty()) {
        int x = pq.top();
        int m = moves.find(x)->second;
        pq.pop();
    
        if (x > 0 && m + 1 < minMoves) {
            auto it = moves.insert(make_pair(x - 1, 1e9)).first;
            if (it->second > m + 1) {
                it->second = m + 1;
                pq.push(x - 1);
                if (x - 1 == 0) {
                    minMoves = it->second;
                }
            }
            for (int k = (int)floor(sqrt(x)); k > 1; k--) {
                if (x % k == 0) {
                    int factor = x / k;
                    auto it2 = moves.insert(make_pair(factor, 1e9)).first;
                    if (it2->second > m + 1) {
                        it2->second = m + 1;
                        pq.push(factor);
                    }
                }
            }
        }
    }
    
    return minMoves;
    

    }

    int main() { int q; cin >> q;

    for (int i = 0; i < q; i++) {
        int n;
        cin >> n;
        cout << testCase(n) << endl;
    }
    
    return 0;
    

    }

    
    
  • + 0 comments

    how is editorial code working q =10^5 n =10^6

    for each query we are doing memset so q*n then how is it working or is there anything that i am missing here