- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试计算范围 A 到 B 之间具有数字总和 S(假设 S=60)的整数。
A 和 B 的范围从 1 到 10^18。
设 X 为数字,直到 Y 我们必须计算整数。
X = x1 x2 ... xn - 1 xn 和 Y = y1 y2 ... yn - 1 yn,其中 xi 和 yi 是 X 和 Y 的十进制数。
leftmost_lo 作为 xi < yi 的最小 i。如果没有这样的 i,我们将 leftmost_lo 定义为 n + 1。类似地,我们将 leftmost_hi 定义为 xi > yi 的最小 i,否则为 n + 1。
函数 count 返回具有属性 X ≤ Y 且 X 的数字和为 60 的整数 X 的数量 f(Y)。
根据上面的定义,令 n 为 Y 的位数,y[i] 为 Y 的第 i 个十进制数字。下面的递归算法解决了这个问题:
count(i, sum_so_far, leftmost_lo, leftmost_hi):
if i == n + 1:
# base case of the recursion, we have recursed beyond the last digit
# now we check whether the number X we built is a valid solution
if sum_so_far == 60 and leftmost_lo <= leftmost_hi:
return 1
else:
return 0
result = 0
# we need to decide which digit to use for x[i]
for d := 0 to 9
leftmost_lo' = leftmost_lo
leftmost_hi' = leftmost_hi
if d < y[i] and i < leftmost_lo': leftmost_lo' = i
if d > y[i] and i < leftmost_hi': leftmost_hi' = i
result += count(i + 1, sum_so_far + d, leftmost_lo', leftmost_hi')
return result
Compute the number f(Y) of integers X with the property X ≤ Y and X has the digit sum 60
现在我们有 f(Y) = count(1, 0, n + 1, n + 1) 并且我们已经解决了问题。运行时
对于这个特定的实现是 O(n^4)。
我从这个链接理解了这个概念。 How to count integers between large A and B with a certain property?
但无法理解如何优化它。
现在如何针对这个特定问题将其优化为 O(n) 解决方案。
谁能帮帮我。
最佳答案
首先,您可以注意到,如果您有一个函数 F,它返回整数 <= A 且数字总和为 S,则 A 和 B 之间具有数字总和 S 的整数个数为 F(B)-F( A-1).
然后定义一些符号:
然后你有这些关系,一次做一个数字,并注意到可能是你匹配 A 的第一个数字,或者你在那里放一个较低的数字(然后对于递归情况,你可以用 A 替换全9)。
F(S, A) = 1 if S = 0
F(S, A) = 0 if S < 0 or A = 0
otherwise F(S, A) =
F(S-A[0], A[1:])
+ F(S-0, n(A[1:])) + F(S-1, n(A[1:])) + ... + F(S-A[0]-1, n(A[1:]))
这为您提供了这段代码(带有缓存以避免多次计算同一事物):
def count1(S, digits, nines, k, cache):
if S <= 0 or k == len(digits): return S==0
key = (S, nines, k)
if key not in cache:
dk = 9 if nines else digits[k]
cache[key] = sum(count1(S-i, digits, nines or i<dk, k+1, cache)
for i in xrange(dk+1))
return cache[key]
def count(S, A):
return count1(S, map(int, str(A)), False, 0, {})
def count_between(S, A, B):
return count(S, B) - count(S, A-1)
print count_between(88, 1, 10**10)
缓存最终的最大大小为 S * 2 * len(str(A)),每件事都计算一次,这给了你复杂性:O(S * log_10(A))。
关于algorithm - 计算具有数字 S 的大 A 和 B 之间的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25579027/
我有两种结构,Header 和Session,它们都符合协议(protocol)TimelineItem。 我有一个 Array 由 TimelineItem 组成,如下所示: [Header1, S
这个问题在这里已经有了答案: Multiple assignment and evaluation order in Python (11 个答案) 关闭 6 年前。 我刚接触python所以想问你
我试图找到一种方法来在 R 中获取 A、A、A、A、B、B、B、B、B 的所有可能的唯一排列的列表。 组合最初被认为是获得解决方案的方法,因此组合的答案。 最佳答案 我认为这就是你所追求的。 @bil
我怎样才能将两个给定的向量混合成一个新的向量,它以交替的顺序保存它们的值。 (f [a a] [b b]) ; > [a b a b] 这是我想到的: (flatten (map vector [:a
这是我的第一个问题,我开始学习Python。之间有区别吗: a, b = b, a + b 和 a = b b = a + b 当您在下面的示例中编写它时,它会显示不同的结果。 def fib(n):
这个问题在这里已经有了答案: Why is there an injected class name? (1 个回答) 12 个月前关闭。 我不知道如何解释: namespace A { struct
我尝试了一些代码来交换 Java 中的两个整数,而不使用第三个变量,使用 XOR。 这是我尝试过的两个交换函数: package lang.numeric; public class SwapVars
假设类 B 扩展类 A,并且我想为 B 声明一个变量。什么更有效?为什么? B b或 A b . 最佳答案 您混淆了两个不同的概念。 class B extends A { } 意味着B 是 A .
我不确定这个问题的标题是什么,这也可能是一个重复的问题。所以请相应地指导。 我是 python 编程的新手。我有这个简单的代码来生成斐波那契数列。 1: def fibo(n): 2: a =
我在谷歌上搜索了有关 dynamic_cast 的内容,我发现显式地将基类对象转换为派生类指针可能是不安全的。但是当我运行一些示例代码来检查它时,我没有收到任何错误。请在下面找到我的代码: class
这个问题在这里已经有了答案: What is this weird colon-member (" : ") syntax in the constructor? (14 个答案) 关闭 8 年前。
在不重现产生非整数值的表达式的情况下实现以下目标的惯用方法是什么(在我的真实情况下,该值是在我不想重现的冗长查询之后计算为百分比的): SELECT * FROM SomeTable WHERE 1/
在析构中,这两个代码的结果确实不同。我不确定为什么。 提示说 const [b,a] = [a,b] 将导致 a,b 的值为 undefined (从左到右的简单分配规则)。我不明白为什么会这样。 l
C++ Templates - The Complete Guide, 2nd Edition介绍max模板: template T max (T a, T b) { // if b < a th
我最近开始学习代码(Java),并根据第 15.17.3 节在 Oracle 网站上查找了模运算符。以下链接: http://docs.oracle.com/javase/specs/jls/se8/
无法理解以下行为。 d1 := &data{1}; 的区别d1 和 d2 := 数据{1}; &d1。两者都是指针,对吧?但他们的行为不同。这里发生了什么 package main import "f
这个问题在这里已经有了答案: How to make loop infinite with "x = y && x != y"? (4 个回答) How can i define variables
在我的程序中,当我调试我的代码时,它似乎在我生成的代码中的某处 X1=['[a,a,a]','[b,b,b]'] 还有我生成的其他地方 X2=[[a,a,a],[b,b,b]] 当我想添加这两个列表然
我试图使用递归将两个整数相乘,并意外编写了这段代码: //the original version int multiply(int a, int b) { if ( !b ) retu
我有一个列表中数字之间所有可能的操作组合: list = ['2','7','8'] 7+8*2 8+7*2 2*8+7 2+8*7 2-8*7 8-2/7 etc 我想知道是否可以说像 ('7*2+
我是一名优秀的程序员,十分优秀!