- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近遇到了这个编码挑战 - The Unlucky number 13 . (本节最后一个问题)
问题陈述:
编写一个程序来计算由 组成的字符串总数电话 人物。
任何字符串都不能将“ 13 ”作为子字符串。
字符串可以包含 0-9 之间的任何整数,重复任意次数。
输入:
N 作为输入,其中 0<= N <= 1000000009 .
输出:
输出应该是模 1000000009 的答案。
我的解决方案:
我试着用我想出的一个简单的方程来解决它。
ans = 10^n - ((10^(n-2)) * (n-1))
Subtracting the possibilities with 13 as substring from the total number of possibilities.
let input = Int(readLine()!)
if let n = input as? Int{
var ans = getPowerOfTen(n)
ans = ans - getPowerOfTen(n-2) * (n - 1)
print(ans % 1000000009)
}
// Did not import any library to calculate the power
func getPowerOfTen(_ num: Int)->Int{
var ans = 1
var n = num
while(n > 0){
ans = ans * 10
n = n - 1
}
return ans
}
我希望得到两个问题的帮助。
I found this in Array & Strings problem section. I did not use anyarray though. I think this equation works.
最佳答案
公式似乎是正确的。您的解决方案有两个问题。
1: Time complexity
2: Overflow Issue
10^n mod m
时使用了幼稚的技术.您正在使用来自
1
的简单循环至
n
这导致
O(n)
的复杂性.有一种非常流行的技术,称为
Binary Exponentiation计算
a^b mod m
在
O(logn)
.这种技术在竞争性编程中被广泛用于解决各种问题。
ans
像这样:
ans = ans * 10
当值足够大时,
ans
将变为负数,因为它将无法存储该值。这被称为
Overflow .发现有可能发生溢出的一种好方法是在问题中提到它时:
The output should be the answer modulo 1000000009
这表明程序设置者自己知道计算量会很大,因此他们要求以这种格式输出答案。
关于swift - HackerEarth 中的不幸 13 挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67500729/
我想得到 id a b c -------------------- 1 1 100 90 6 2 50 100 ...来自: id a
让我们看看,我有这段将 NFA 自动转换为 DFA 的代码;这是我编写的;我发现了一个“bug”; printf()指令 这意味着像这样“printf("",X); ”以防止出现错误 没有要在屏幕上打
我有一些文本图像,但它们是弯曲的,呈圆形或波浪形。我需要把它们弄直。我尝试使用OCR提取文本,但是它们效率低下,需要直接的图像。 我附上测试图片: 我需要覆盖这两个最小区域。 请建议一些路径或使用
data1=data.frame("StudentID"=c(1,1,1,2,2,2,2,3,3,3,3), "Class"=c(1,1,1,1,1,1,1,2,2,2,2),
我的问题已在 java draw line as the mouse is moved 中提到过然而,我对这本书的了解还不够深入,无法涵盖 JPanels、JFrames 和 Points,正如提出这
这是我上一个问题 here. 的后续问题那里发布的答案实际上不起作用。所以这就是挑战。您将获得以下代码(假设包含 jQuery): $("input").val(**YOUR PHP /
以下是C语言中链表的语法,部分内容 struct tag-name { type member1; type member2; ....... ....... struc
我面临以下挑战性问题: There are a circle of 100 baskets in a room; the baskets are numbered in sequence from 1
我有一个这样的结构: public struct MyStruct { public string Name; public bool Process; } 我有一个这样的
假设我有: var directions = [ "name", "start_address", "end_address", "order_date" ]; 我正在尝试找到一种巧妙、快速的方法来将
我正在用 Javascript 重做 Project Euler 挑战。任务是获取最大的回文数( https://projecteuler.net/problem=4 )。现在我得到以下代码: var
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
第一问:有没有可能有一个不可见的矩形? 问题 2:是否可以在方法上调用方法?见下文。 var canvas = document.getElementById("canvas"); var ctx =
问题: 给定一串数字,计算是任何回文的字谜的子词(一致的子序列)的数量。 例子: 对于输入字符串“02002”,结果应该是 11,即: “0”、“2”、“0”、“0”、“2”、“00”、“020”、“
用户A-用户B-用户C-用户D-用户F 用'-'连接的用户互相认识。 我需要一个算法来完成这两项任务: 计算从UserX到UserY的路径 对于 UserX,计算距离不超过 3 步的所有用户。 有没有
根据我的教授介绍。对于数据库理论,没有任何例子可以说明这种情况何时会出现,考虑到它是理论的特定部分,这似乎有点奇怪。 我正在寻找的只是一个示例关系,它是第 4 范式并且可以执行第 5 范式分解。或者(
给定任务sameEnds来自 CodingBat: 给定一个字符串,返回出现在字符串开头和结尾且不重叠的最长子字符串。例如,sameEnds("abXab") 是 "ab"。 sameEnds("ab
在我的 welcome#index 页面上,有一个按钮可以远程(或者我应该说异步)为 Article 编写新的 Comment ),使用 AJAX。 它工作得很好,只是当使用rails迭代一篇文章时,
希望每个人都有美好的一天。 这是我在 Stackoverflow 上发表的第一篇文章! 我刚刚完成了 Codeacademy 上的 javascript 类(class),并且也阅读了几本相关书籍。现
挑战是删除数字末尾的零。两个数字内的零是可以的。例如: 14000 == 14 //all end zeros removed 10300 == 103 // all end zeros remove
我是一名优秀的程序员,十分优秀!