思路显然是暴力枚举.

但是两个问题:

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. Visio Yoeman

    Visio需要确定文件位置才能运行的 Yo主要是用来生成框架的,相当于是一个框架生成器 new install -g generator-django 然后在Visio按F5,选择要创建的环境,就可以 ...

  2. 大家一起来学 NHibernate+NUnit (VS2012+SQL Server2008)

    大家一起来学 NHibernate+NUnit (VS2012+SQL Server2008) 分类: C#2013-08-10 18:47 1589人阅读 评论(5) 收藏 举报 NHibernat ...

  3. sql developer 如何格式化sql

    1.首先  Ctrl+A  全选需要格式的sql 2.然后  Ctrl+F7 即可格式化

  4. MapReduce:将下面的两排数字先按第一排排序,然后再按第二排排序,要求顺序排序

    MapReduce:将下面的两排数字先按第一排排序,然后再按第二排排序,要求顺序排序 文件如下: 这个案例主要考察我们对排序的理解,我们可以这样做: 代码如下(由于水平有限,不保证完全正确,如果发现错 ...

  5. iOS日常学习 - iOS10上关于NSPhotoLibraryUsageDescription等问题

    最近升级了Xcode8.0,真是很多坑啊,填完一个来另外一个,今天又遇到了一个,用Xcode8.0上传项目时被驳回说是info.plist里面没有设置NSPhotoLibraryUsageDescri ...

  6. PHP练习题三

    1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21. echo date(‘Y-m-d H:i:s’, strtotime(‘-1 days’)); 2.不使用第三个变量交换两个 ...

  7. 编写第一个Shell脚本【TLCL】

    怎样编写一个 Shell 脚本 编写一个脚本 使脚本文件可执行 把脚本放到Shell能够找到的地方 脚本文件格式 #!/bin/bash # This is our first script. ech ...

  8. windchill系统——导航器v1.0:思维导图

    总图 思维导图图片链接 http://www.edrawsoft.cn/viewer/public/s/7b3fc783493788

  9. C++中map的用法

    map的特性是,所有元素都会根据元素的减值自动被排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素会被视为键值,第二个元素会被视为实值.map不允许两 ...

  10. nginx限制ip并发数

    nginx限制ip并发数,也是说限制同一个ip同时连接服务器的数量 1.添加limit_zone 这个变量只能在http使用 vi /usr/local/nginx/conf/nginx.conf l ...