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 ...
随机推荐
- python提示AttributeError: 'NoneType' object has no attribute 'append'
在写python脚本时遇到AttributeError: 'NoneType' object has no attribute 'append' a=[] b=[1,2,3,4] a = a.appe ...
- Linux Shell编程第4章——sed和awk
目录 sed命令基本用法 sed命令实例 命令选项 文本定位 编辑命令 awk编程模型 awk编程实例 1.awk模式匹配 2.记录和域 3.关系和布尔运算符 4.表达式 5.系统变量 6.格式化输出 ...
- WCF RIA SERVICE相关技术
WCF RIA SERVICE实体属性拷贝 private void DoSubmit() { ((IEditableObject)this.RepairContract).EndEdit(); va ...
- JAVA寄存器
所有进程都使用寄存器,Java虚拟机使用下列寄存器管理系统堆栈: 程序记数寄存器:跟踪程序执行的准确位置 堆栈指针寄存器:指示操作栈项 框架寄存器:指向当前执行的环境 变量寄存 ...
- PHP preg_replace
preg_replace (PHP 3 >= 3.0.9, PHP 4, PHP 5) preg_replace -- 执行正则表达式的搜索和替换 说明 mixed preg_replace ( ...
- Java面向对象—抽象类和接口
本文引用海子的一篇文章:http://www.cnblogs.com/dolphin0520/p/3811437.html 一.抽象类 在了解抽象类之前,先来了解一下抽象方法.抽象方法是一种特殊的方法 ...
- PAT 天梯赛 L1-012. 计算指数 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-012 AC代码 #include <iostream> #include <cstdio&g ...
- Django 中间件简介
Django 中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. ...
- Python学习笔记之Python的enumerate函数
Python 的 enumerate() 函数就像是一个神秘的黑箱,你无法简单地用一句话来概括这个函数的作用与用法. enumerate() 函数属于非常有用的高级用法,而对于这一点,很多初学者甚至中 ...
- Openstack之Nova创建虚机流程分析
前言 Openstack作为一个虚拟机管理平台,核心功能自然是虚拟机的生命周期的管理,而负责虚机管理的模块就是Nova. 本文就是openstack中Nova模块的分析,所以本文重点是以 ...