[C++] Linear scan, modulo, trailing zero counting, log10, etc...
\nclass Solution {\npublic:\n    string abbreviateProduct(int left, int right) {\n        // Find trailing zeros.\n        int five_cnts = 0, two_cnts = 0;\n   
```\nclass Solution {\npublic:\n    string abbreviateProduct(int left, int right) {\n        // Find trailing zeros.\n        int five_cnts = 0, two_cnts = 0;\n        for (int d = left; d <= right; ++d) {\n            int val = d;            \n            while ((val % 5) == 0) {\n                val /= 5;\n                ++five_cnts;\n            }\n            two_cnts += __builtin_popcount((d - 1) & (d ^ (d - 1)));\n        }\n        const int zero_cnts = std::min(five_cnts, two_cnts);\n         \n        // Compute trailing digits and the full version when we have no more than 10 digits.\n        int64_t trailing = 1, full = 1;\n        constexpr int64_t kModTrailing = 1E5;\n        constexpr int64_t kModFull = 1E10;\n        five_cnts = two_cnts = zero_cnts;\n        for (int64_t d = left; d <= right; ++d) {\n            auto val = d;\n            while ((five_cnts > 0) && (val % 5 == 0)) {\n                val /= 5;\n                --five_cnts;\n            }\n            while ((two_cnts > 0) && (val % 2 == 0)) {\n                val /= 2;\n                --two_cnts;\n            }\n            trailing = (trailing * val) % kModTrailing;\n            full = (full * val) % kModFull;\n        }\n        \n        // Find how many digits the result has (after removing trailing zeros).\n        double log_prod = 0;\n        for (double d = left; d <= right; ++d) {\n            log_prod += std::log10(d);\n        }        \n        constexpr double kPrec = 1E-9;\n        const int num_digits = 1 + std::floor(log_prod + kPrec) - zero_cnts;        \n\n        std::ostringstream sout;\n        if (num_digits > 10) {\n            const auto frac = log_prod - std::floor(log_prod + kPrec);\n            auto base = std::pow(10, frac);\n            for (int k = 1; k < 5; ++k) {\n                base *= 10;\n            }\n            const int64_t leading = std::floor(base + kPrec);\n            sout << leading << "..." << setw(5) << setfill(\'0\') << trailing;\n        } else {\n            sout << full;\n        }\n        sout << "e" << zero_cnts;        \n        return sout.str();\n    }\n};\n```
[]