add: 230510-CN

This commit is contained in:
Eatswap 2023-05-10 23:19:17 +08:00
parent 2c85f9fc4b
commit 44c80466fa
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 59 additions and 6 deletions

30
cpp/2305/LC230510CN.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <unordered_set>
/**
* 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<ULL> 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);
}
}

View File

@ -108,4 +108,9 @@ public:
static std::vector<int> spiralOrder(const std::vector<std::vector<int>>&) noexcept;
};
class LC230510CN {
public:
static constexpr inline int smallestRepunitDivByK(int k) noexcept;
};
#endif //LEETCODE_CPP_DEFS_H

View File

@ -1,5 +1,7 @@
#include <cstdio>
#include <iostream>
#include <ostream>
#include <unordered_set>
#include <vector>
#include "defs.h"
@ -12,11 +14,27 @@ std::ostream& operator<<(std::ostream& os, const std::vector<T>& 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<ULL> 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;
}