Hint
Explanation
Let's start with an example: and . Observe that we really need to consider three numbers as viable answers: , , and . Try to think about why, and how this generalizes!
We can apply a very similar approach when . In fact, it's almost identical, with one minor difference. Consider the case where and , just as before. The only difference is we can no longer consider as a viable answer, as it is less than . Again, try to think about how to generalize this!
Implementation
Time Complexity: , where is the maximum number of digits (19).
C++
#include <bits/stdc++.h>using ll = long long;using namespace std;/** @return the product of the given string's digits (-1 if empty) */ll prod(string s) {// remove leading zerosint i = 0;while (s[i] == '0') { i++; }s = s.substr(i);
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!