CSES - Datatähti 2023 alku - Results
Submission details
Task:Kertoma
Sender:cppbetter
Submission time:2022-11-06 14:53:15 +0200
Language:C++17
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:70:25: error: no matching function for call to 'find(std::array<unsigned int, 100000>::iterator, std::array<unsigned int, 100000>::iterator, uint32_t&)'
   70 |     auto loc = std::find(std::begin(lengths), std::end(lengths), length);
      |                ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/bits/locale_facets.h:48,
                 from /usr/include/c++/11/bits/basic_ios.h:37,
                 from /usr/include/c++/11/ios:44,
                 from /usr/include/c++/11/ostream:38,
                 from /usr/include/c++/11/iostream:39,
                 from input/code.cpp:1:
/usr/include/c++/11/bits/streambuf_iterator.h:421:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT, std::char_traits<_CharT>...

Code

#include <iostream>
#include <array>
#include <cmath>

#define PI 3.14159265358979323846

// Length of factorial
uint32_t StirlingAprox(uint32_t n)
{
    return (uint32_t) std::floor( ((n+0.5) * std::log(n) - n + 0.5 * std::log(2*PI))/std::log(10) ) + 1;
}

int main()
{
    std::array<uint32_t, 10> frequency = {};
    uint32_t length = 0;

    for(auto& i : frequency)
    {
        std::cin >> i;
        length += i;
    }

    // Check special cases that share the same factorial length
    std::array<uint32_t, 10> factorialSpecial = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
    if(frequency == factorialSpecial)
    {
        std::cout << "2\n";
        return 0;
    }
    factorialSpecial = {0, 0, 0, 0, 0, 0, 1, 0, 0, 0};
    if(frequency == factorialSpecial)
    {
        std::cout << "3\n";
        return 0;
    }
    factorialSpecial = {0, 0, 1, 0, 1, 0, 0, 0, 0, 0};
    if(frequency == factorialSpecial)
    {
        std::cout << "4\n";
        return 0;
    }
    factorialSpecial = {1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
    if(frequency == factorialSpecial)
    {
        std::cout << "5\n";
        return 0;
    }
    factorialSpecial = {1, 0, 0, 0, 0, 1, 0, 7, 0, 0};
    if(frequency == factorialSpecial)
    {
        std::cout << "6\n";
        return 0;
    }
    factorialSpecial = {2, 0, 0, 0, 1, 1, 0, 0, 0, 0};
    if(frequency == factorialSpecial)
    {
        std::cout << "7\n";
        return 0;
    }
    // Use Stirling’s approximation to get a table of values during compile time and check if there is a hit
    // Have to check until 10^5

    std::array<uint32_t, 100000> lengths = {};
    for(int i = 0; i < 100000; i++)
    {
        lengths[i] = StirlingAprox(i);
    }

    auto loc = std::find(std::begin(lengths), std::end(lengths), length);
    if (loc != std::end(lengths))
    {
        std::cout << std::distance(lengths.begin(), loc) << "\n";
    }
    else
    {
        // Stirling’s approximation doesn't work anymore :C
        std::cout << "-1\n";
    }
}