Leetcode 1015. Smallest Integer Divisible by K
思路显然是暴力枚举.
但是两个问题:
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的更多相关文章
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 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 ...
随机推荐
- 微信小程序学习笔记(2)--------框架之目录结构
框架提供了自己的视图层描述语言 wxml 和 WXSS,以及基于 JavaScript 的逻辑层框架,并在视图层与逻辑层间提供了数据传输和事件系统. 一.响应的数据绑定 框架的核心是一个响应的数据绑定 ...
- python之路 面向对象基础 XML
一.面向对象基础 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公用的.类变量定义 ...
- WINDOWS和UNIX换行符的理解
# WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37 其他 美国54.163.255.40 其他 美国 弗吉尼亚州 亚马逊公司 **[ro ...
- 配置OpenVpn使用证书和用户名密码双验证
修改OpenVpn的主配置文件 添加一下内容: [root@check1 openvpn]# tail -3 server.conf auth-user-pass-verify /etc/openvp ...
- CentOS 7安装Xfce和VNC
很多VPS商提供的CentOS 7镜像只有最小化安装,这样就只能在命令行下操作,如果要调试代码的话,还是图形化界面方便,而且CentOS自带的Gnome和KDE都比较占内存.启动桌面后就占用了400M ...
- spark学习12(spark架构原理)
spark采用的是主从式的架构,主节点叫master,从节点是worker Driver 我们编写的spark就在Driver上,由driver进程执行. Driver是spark集群的节点之一,或你 ...
- windows 环境下安装python MySQLdb
使用Python访问MySQL,需要一系列安装 Linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装 http://blog.csdn.NET/wklken/arti ...
- Git和GitHub相关
组员从GitHub上下载项目并上传项目的步骤如下 .组员接收到组长发的项目地址,组员需要从GitHub上把项目克隆下来,首先组员 需要在本地的一个文件夹里打开git,然后运行如下代码:git clon ...
- ceph 测试
FIO用法: 随机读: fio -filename=/dev/sdb1 -direct=1 -iodepth 1 -thread -rw=randread -ioengine=psync -bs=16 ...
- thinkphp接收阿里淘宝客数据
坑在于淘宝客api返回的数据对象是SimpleXMLElement Object类型,不转为php的json array类型数据直接扔到thinkphp循环输出中会达不到要的效果,奇奇怪怪的数组,一度 ...