Nasser's Five#12478

Nasser's Five (Editorial)

Problem Author:

Mohammad Nasser (MOAGEZA)

Prepared by:

Mohammad Nasser (MOAGEZA)

Edited by:

Hasan Saleh (MathModel)

Solution

General Idea: We need to append the digit to until x > y.

Each time we append to a number with digits, the new number becomes:

\text{new\_x} = \text{old\_x} \times 10 + 5

We simply simulate this process: keep appending and count the operations until x > y.

Since each operation multiplies by and adds , the number grows exponentially. After operations:

x_k = x_0 \times 10^k + 5 \times \frac{10^k - 1}{9}

However, for the given constraints, simulation is sufficient because the answer is always small (at most \log_{10}(y) operations).

The solution runs in time per test case, where d = \lfloor \log_{10} y \rfloor + 1 is the number of digits in .

#include <bits/stdc++.h>
using namespace std;

void solve() {
    long long x, y;
    cin >> x >> y;
    
    int cnt = 0;
    while (x <= y) {
        x = x * 10 + 5;
        cnt++;
    }
    
    cout << cnt;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}