思路显然是暴力枚举.

但是两个问题:

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. HTML 块级元素与行内元素

    1.块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签'P".“form"这个块元素比较特殊,它只能用来容纳其他块元素. 2.如果没有css的作用,块元素 ...

  2. MAC下配置MAVEN环境变量配置

    MAVEN环境变量的配置: 第一步:在MAVEN的官网下载MAVEN.http://maven.apache.org/download.cgi,我这里下载的是apache-maven-3.39-bin ...

  3. Matplot相关(一)

    ——————————缩写定义—————————— import matplotlib.pyplot as plt import matplotlib as mpl ——————————函数解析———— ...

  4. 使用githubs托管代码

    此文章已经发表于本人博客. 最近在学习nodejs,使用它自己都蛮觉得有激情哦,相信自己路学下去.在学习的过程中nodejs很多插件都在github上,于是自己也用了这个东东感觉不错,开始的时候还用命 ...

  5. 2016年国内开源maven镜像站点汇总

    本文系转载,原文链接:https://www.cnblogs.com/xunianchong/p/5684042.html 一.站点版 (一).企业站 1.网易:http://mirrors.163. ...

  6. 大数据生态,哪些框架需要全部启动,哪些只启动master,仅为汇总

    主从,只需要在master节点启动 hadoop hbase 单机启动 hive 其他,需要启动每个节点 zookeeper kafka flume presto

  7. CSS3鼠标悬停8种动画特效

    在线演示 本地下载

  8. 20145229吴姗珊《网络对抗》WEB基础实践

    20145229吴姗珊<网络对抗>WEB基础实践 基础与实践 基础问题 1.什么是表单 表单是可以收集用户的信息和反馈意见,是网站管理者与浏览者之间沟通的桥梁. 一部分是HTML源代码用于 ...

  9. [SCOI2005]扫雷Mine

    1088: [SCOI2005]扫雷Mine Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2028  Solved: 1187[Submit][St ...

  10. Define class with itself as generic implementation. Why/how does this work?

    https://stackoverflow.com/questions/10709061/define-class-with-itself-as-generic-implementation-why- ...