From 44c80466fa750355b52898ff15d281829f8494f4 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Wed, 10 May 2023 23:19:17 +0800 Subject: [PATCH] add: 230510-CN --- cpp/2305/LC230510CN.cpp | 30 ++++++++++++++++++++++++++++++ cpp/2305/defs.h | 5 +++++ cpp/2305/main.cpp | 30 ++++++++++++++++++++++++------ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 cpp/2305/LC230510CN.cpp diff --git a/cpp/2305/LC230510CN.cpp b/cpp/2305/LC230510CN.cpp new file mode 100644 index 0000000..4072be9 --- /dev/null +++ b/cpp/2305/LC230510CN.cpp @@ -0,0 +1,30 @@ +#include + +/** + * 1015. Smallest Integer Divisible by K + * + * Given a positive integer k, you need to find the length of the smallest positive integer n such that n is divisible by k, and n only contains the digit 1. + * Return the length of n. If there is no such n, return -1. + * Note: n may not fit in a 64-bit signed integer. + */ + +class LC230510CN { +public: + static int smallestRepunitDivByK(int k) noexcept; +}; + +int LC230510CN::smallestRepunitDivByK(int k) noexcept { + using ULL = unsigned long long; + int sum = 0; + std::unordered_set vis; + ULL key; + for (int j = 1, c = 1;; (j = (j * 10) % k), ++c) { + sum = (sum + j) % k; + if (sum == 0) + return c; + key = (ULL(sum) << 32) + j; + if (vis.contains(key)) + return -1; + vis.insert(key); + } +} diff --git a/cpp/2305/defs.h b/cpp/2305/defs.h index efbebe4..4c8f95b 100644 --- a/cpp/2305/defs.h +++ b/cpp/2305/defs.h @@ -108,4 +108,9 @@ public: static std::vector spiralOrder(const std::vector>&) noexcept; }; +class LC230510CN { +public: + static constexpr inline int smallestRepunitDivByK(int k) noexcept; +}; + #endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2305/main.cpp b/cpp/2305/main.cpp index a041976..756d7b0 100644 --- a/cpp/2305/main.cpp +++ b/cpp/2305/main.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include "defs.h" @@ -12,11 +14,27 @@ std::ostream& operator<<(std::ostream& os, const std::vector& x) noexcept { } int main() { - std::cout << LC230509::spiralOrder({ - {2,3,4}, - {5,6,7}, - {8,9,10}, - {11,12,13} - }); + using ULL = unsigned long long; + int ok = 0, not_ok = 0; + for (int i : {99, 101, 9999}) { + int sum = 0; + std::unordered_set vis; + for (int j = 1, c = 1;; (j = (j * 10) % i), ++c) { + sum = (sum + j) % i; + if (sum == 0) { + std::printf("%6d -> %d\n", i, c); + ++ok; + break; + } + auto key = (ULL(sum) << 32) + j; + if (vis.contains(key)) { + std::printf("%6d -> CANNOT\n", i); + ++not_ok; + break; + } + vis.insert(key); + } + } + std::printf("[OK] %d | [FAIL] %d\n", ok, not_ok); return 0; }