#include #include #include #include // KuuHaKu's solution size_t get_price_kuu(size_t has, size_t wants) { size_t i{has / 10 + 1 | 0}; size_t cost{0}; size_t step{10 - has % 10}; while (wants >= 10) { cost += step * i++; wants -= step; step = 10; } const size_t cost_mult{has / i | 0}; return cost + cost_mult - (has % 10) + wants * ++i; } // Tim's solution size_t get_price_tim(size_t has, size_t wants) { const short base_price{1}; const short step{10}; const size_t first{std::min(step - has % step, wants)}; const size_t second{wants - first - (wants - first) % step}; const size_t third{wants - first - second}; size_t out{static_cast( first * (base_price * std::ceil(has / static_cast(step))))}; if (second != 0) { const size_t iterations{second / step}; const size_t multiplier{base_price * ((has + first) / step) + 1}; for (size_t i{0}; i < iterations; i++) { const size_t current{multiplier + i}; out += current * step; } } if (third != 0) { const size_t current{base_price * ((has + first + second) / step) + 1}; out += third * current; } return out; } int main() { std::srand(std::time(nullptr)); auto start = std::chrono::high_resolution_clock::now(); for (unsigned int i{0}; i < 999'999; i++) { [[maybe_unused]] volatile auto foo = get_price_kuu(rand(), rand()); } auto end = std::chrono::high_resolution_clock::now(); std::cout << "KuuHaKu's solution took: " << std::chrono::duration_cast(end - start) .count() << "ms\n"; start = std::chrono::high_resolution_clock::now(); for (unsigned int i{0}; i < 999'999; i++) { [[maybe_unused]] volatile auto foo = get_price_tim(rand(), rand()); } end = std::chrono::high_resolution_clock::now(); std::cout << "Tim's solution took: " << std::chrono::duration_cast(end - start) .count() << "ms\n"; }