diff --git a/2112/211230.cpp b/2112/211230.cpp new file mode 100644 index 0000000..2a89781 --- /dev/null +++ b/2112/211230.cpp @@ -0,0 +1,35 @@ +#include +#include + +class Solution { +public: + static int smallestRepunitDivByK(int k) { + if (k == 1) return 1; + /* + * Bad code. + * Despite std::unordered_set is O(1), + * it increases space complexity + * and the constant increases. + int pos = 2, rem = 11 % k; + std::unordered_set s; + s.insert(rem); + while (true) { + if (!rem) + return pos; + rem = (10 * rem + 1) % k; + ++pos; + if (s.count(rem)) + return -1; + s.insert(rem); + } + */ + for (int pos = 1, rem = 1; pos < k; ++pos) + if (!(rem = (10 * rem + 1) % k)) + return ++pos; + return -1; + } +}; + +int main() { + std::printf("%d\n", Solution::smallestRepunitDivByK(7)); +} \ No newline at end of file diff --git a/2112/CMakeLists.txt b/2112/CMakeLists.txt index 9ad460a..cfa70b3 100644 --- a/2112/CMakeLists.txt +++ b/2112/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2112) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2112 211229.cpp) \ No newline at end of file +ADD_EXECUTABLE(2112 211230.cpp) \ No newline at end of file