Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
smallest-integer-divisible-by-k.cpp 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-03-24 18:54 +08:00 . Create smallest-integer-divisible-by-k.cpp
// Time: O(k)
// Space: O(1)
class Solution {
public:
int smallestRepunitDivByK(int K) {
// by observation, K % 2 = 0 or K % 5 = 0, it is impossible
if (K % 2 == 0 || K % 5 == 0) {
return -1;
}
// let f(N) is a N-length integer only containing digit 1
// given K % 2 != 0 and K % 5 != 0
// if there is no N in range (1..K) s.t. f(N) % K = 0
// => there must be K remainders of f(N) % K in range (1..K-1) excluding 0
// => due to pigeonhole principle, there must be at least 2 same remainders
// => there must be some x, y in range (1..K) and x > y s.t. f(x) % K = f(y) % K
// => (f(x) - f(y)) % K = 0
// => (f(x-y) * 10^y) % K = 0
// => due to (x-y) in range (1..K)
// => f(x-y) % K != 0
// => 10^y % K = 0
// => K % 2 = 0 or K % 5 = 0
// => -><-
// it proves that there must be some N in range (1..K) s.t. f(N) % K = 0
int result = 0;
for (int N = 1; N <= K; ++N) {
result = (result * 10 + 1) % K;
if (!result) {
return N;
}
}
assert(false);
return -1; // never reach
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助