//! (c) WhiZTiM __ionogu(<_at_)>acm.org
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <chrono>
#include <tuple>
using IndexProd = std::tuple<std::size_t, long>;
//! Invariant: str.size() >= length
IndexProd index_of_largest(const std::string& str, std::size_t length){
long largest_product = 0;
std::size_t largest_index = 0;
for(std::size_t i = 0; i < str.size() - length; i++){
auto start = str.begin() + i;
auto stop = start + length;
using Char = std::string::value_type;
auto product = std::accumulate(start, stop, long(1),
[](std::size_t sum, Char c){ return sum * (c - 48); });
if(product > largest_product){
largest_index = i;
largest_product = product;
}
}
return std::make_tuple(largest_index, largest_product);
}
void print(const std::string& str, std::size_t index, std::size_t length){
auto start = str.begin() + index;
auto stop = start + length - 1;
std::copy(start, stop, std::ostream_iterator<char>(std::cout, " x "));
std::cout << *stop;
}
std::string str_input();
void run(){
std::ios::sync_with_stdio(false);
const auto len = 13;
std::string val;
//std::cin >> val;
val = str_input();
auto index_prd = index_of_largest(val, len);
std::cout << "Largest value at index: " << std::get<0>(index_prd) << '\n';
std::cout << "Sequence is "; print(val, std::get<0>(index_prd), len);
std::cout << " = " << std::get<1>(index_prd) << std::endl;
}
std::string str_input(){
return "731671765313306249192251196744265747423553491949349698352031277450632623"
"9578318016984801869478851843858615607891129494954595017379583319528532088055111"
"2540698747158523863050715693290963295227443043557668966489504452445231617318564"
"0309871112172238311362229893423380308135336276614282806444486645238749303589072"
"9629049156044077239071381051585930796086670172427121883998797908792274921901699"
"7208880937766572733300105336788122023542180975125454059475224352584907711670556"
"0136048395864467063244157221553975369781797784617406495514929086256932197846862"
"2482839722413756570560574902614079729686524145351004748216637048440319989000889"
"5243450658541227588666881164271714799244429282308634656748139191231628245861786"
"6458359124566529476545682848912883142607690042242190226710556263211111093705442"
"1750694165896040807198403850962455444362981230987879927244284909188845801561660"
"9791913387549920052406368991256071760605886116467109405077541002256983155200055"
"93572972571636269561882670428252483600823257530420752963450";
}
////////////////////////////////////////////////// My timer function //////
template<typename Func, typename... Args>
void timer(Func func, Args&&... args){
auto start = std::chrono::high_resolution_clock::now();
func(std::forward<Args&&>(args)...);
auto stop = std::chrono::high_resolution_clock::now();
std::cout.precision(8);
std::cout <<
"Took: " << std::chrono::duration<double, std::ratio<1,1>>(stop - start).count()
<< " seconds\n";
}
int main(){
timer(run);
}