Official

E - Addition and Multiplication 2 Editorial by en_translator


Considering the final number of digits of \(x\), we can see that the more \(x\) has digits, the larger \(x\) is, so we need to maximize the number of digits.

The number of digits can be found as \(\lfloor \frac{N}{\mathrm{min}(C)}\rfloor\).

Once the number of digits is fixed, we only care about the lexicographical order, so all we need is to determine from the first digit to the last in a greedy way. For more details, please refer to the sample code.

Sample code (C++):

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

using ll = long long;

int main() {
    int n;
    cin >> n;
    vector<int> c(10);
    for(int i = 1; i <= 9; i++) cin >> c[i];

    int mn = *min_element(c.begin() + 1, c.end());
    int length = n / mn;

    string ans = "";

    for(int i = 0; i < length; i++) {
        for(int j = 9; j >= 1; j--) {
            if((ll)mn * (length - 1 - i) + c[j] <= n) {
                n -= c[j];
                ans.push_back((char)('0' + j));
                break;
            }
        }
    }

    cout << ans << endl;

    return 0;
}

posted:
last update: