思路显然是暴力枚举.

但是两个问题:

1.当1的位数非常大时,模运算很费时间,会超时.

其实每次不用完全用'11111...'来%K,上一次的余数*10+1后再%K就行.

证明:

令f(n)=111111...(n个1);  

 g(n)=f(n)%K

 因为f(n)=f(n-1)*10+1

 所以f(n)%K=(f(n-1)*10+1)%K

 即g(n)=g(n-1)*10+1

2.枚举何时停止?

一种方法是可以设置一个大数,比如10的6次方,可以Accepted.

更精确的方法是:从1个1到K个1,如果这里都没有答案,后面也没了.

因为K的余数不包括0的话有K-1个,我们算了K个,K个里面没有0的话,里面必然至少有两个相等的(抽屉原理),而根据第一个问题所示,相邻的余数有关系,所以一相等之后就是重复循环这些数了,前面找不到后面也肯定没有了.例如K=6:

  • 1 % 6 = 1
  • 11 % 6 = 5
  • 111 % 6 = 3
  • 1111 % 6 = 1
  • 11111 % 6 = 5
  • 111111 % 6 = 3
class Solution:
def smallestRepunitDivByK(self, K: int) -> int:
if K % 2 == 0 or K % 5 == 0:
return -1
g = 0
for i in range(1, K+1):
g = (g * 10 + 1) % K
if g == 0:
return i
return -1

Leetcode 1015. Smallest Integer Divisible by K的更多相关文章

  1. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【leetcode】1022. Smallest Integer Divisible by K

    题目如下: Given a positive integer K, you need find the smallest positive integer N such that N is divis ...

  3. [Swift]LeetCode1015. 可被 K 整除的最小整数 | Smallest Integer Divisible by K

    Given a positive integer K, you need find the smallest positive integer N such that N is divisible b ...

  4. [LeetCode] K-th Smallest Prime Fraction 第K小的质分数

    A sorted list A contains 1, plus some number of primes.  Then, for every p < q in the list, we co ...

  5. Leetcode 974. Subarray Sums Divisible by K

    前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...

  6. [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字

    Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...

  7. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...

  9. 119th LeetCode Weekly Contest Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

随机推荐

  1. ipv6被拒的解决方法

    A,检测服务器是否支持ipv6连接 用mac 搭建IPv6环境测试,只能测试客户端到mac这段网络正常,但是mac到服务器还是IPv4的,所以没有办法测试服务器的IPv6是否正常.可以用手机打开2)步 ...

  2. Java并发(3):volatile及Java内存模型

    Java 语言中的 volatile 变量可以被看作是一种“程度较轻的 synchronized“:与 synchronized 块相比,volatile 变量所需的编码较少,并且运行时开销也较少,但 ...

  3. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  4. [CTSC2011]幸福路径

    题目描述 有向图 G有n个顶点 1, 2, …, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它的体力都会下降 ...

  5. 轮询、长轮询、长连接、socket连接、WebSocket

    轮询:客户端定时向服务器发送Ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 优点:后端程序编写比较容易. 缺点:请求中有大半是无用,浪费带宽和服务器资源.(而每一次的 HTTP 请求和应答 ...

  6. NOIP Mayan游戏

    描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定的步数内消 ...

  7. window下安裝redis服務

    一.下载windows版本的Redis github下载地址:https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100   ...

  8. linux 压缩以及解压命令

    转载:http://blog.csdn.net/mmllkkjj/article/details/6768294/ tar-c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件- ...

  9. Execute Disable Bit

    “Execute Disable Bit”是Intel在新一代处理器中引入的一项功能,开启该功能后,可以防止病毒.蠕虫.木马等程序利用溢出.无限扩大等手法去破坏系统内存并取得系统的控制权.其工作原理是 ...

  10. segment fault本质

    要谈segment fault,必须要谈指针. 指针的本质是什么?只不过是一种带*的数据类型,其特色有: 1.宽度 2.声明 3.赋值 4.++与-- 5.+与- 6.求差值 7.比较 当声明int ...