- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我昨晚想到的一个谜题。我想出了一个解决方案,但效率不高,所以我想看看是否有更好的主意。
谜题是这样的:
给定正整数 N 和 T,你需要:
for i in [1, T], A[i] from { -1, 0, 1 }, such that SUM(A) == N
additionally, the prefix sum of A shall be [0, N], while when the prefix sum PSUM[A, t] == N, it's necessary to have for i in [t + 1, T], A[i] == 0
here prefix sum PSUM is defined to be: PSUM[A, t] = SUM(A[i] for i in [1, t])
the puzzle asks how many such A's exist given fixed N and T
例如,当N = 2
,T = 4
时,A
的工作如下:
1 1 0 0
1 -1 1 1
0 1 1 0
但以下不要:
-1 1 1 1 # prefix sum -1
1 1 -1 1 # non-0 following a prefix sum == N
1 1 1 -1 # prefix sum > N
下面的 python 代码可以验证这样的规则,当给定 N 作为 expect
和 A 的实例作为 seq
(有些人可能觉得阅读代码比阅读文字描述更容易) :
def verify(expect, seq):
s = 0
for j, i in enumerate(seq):
s += i
if s < 0:
return False
if s == expect:
break
else:
return s == expect
for k in range(j + 1, len(seq)):
if seq[k] != 0:
return False
return True
我已经编写了我的解决方案,但它太慢了。以下是我的:
我将问题分解为两部分,一部分没有-1
(只有{0, 1}
),一部分有-1
.
所以如果 SOLVE(N, T)
是正确答案,我定义一个函数 SOLVE'(N, T, B)
,其中正 B 允许我将前缀和扩展到 [-B, N]
的区间而不是 [0, N]
所以实际上 SOLVE(N, T) == SOLVE'(N, T, 0)
。
所以我很快意识到解决方案实际上是:
{0, 1}
组合,具有正长度 l
和 o
1
里面有l + 1
位置,我开始添加 1 个或多个 -1
并使用 B 跟踪数字。最大值将为 B + o
或取决于 A
中剩余的槽数,以较小者为准。SOLVE'(N, T, B)
在前面的 N = 2, T = 4
示例中,在其中一个搜索案例中,我会这样做:
A = [1, -, -, -]
。-1
。在这里我将只添加一个:A = [1, -1, -, -]
。SOLVE'
,这里我将调用SOLVE'(2, 2, 0)
来解决最后两个点。这里它只会返回 [1, 1]
。然后其中一个组合产生 [1, -1, 1, 1]
。但是这个算法太慢了。
我想知道我该如何优化它或以任何不同的方式来看待这个可以提高性能的问题?(我只需要这个想法,而不是实现)
一些示例将是:
T N RESOLVE(N, T)
3 2 3
4 2 7
5 2 15
6 2 31
7 2 63
8 2 127
9 2 255
10 2 511
11 2 1023
12 2 2047
13 2 4095
3 3 1
4 3 4
5 3 12
6 3 32
7 3 81
8 3 200
9 3 488
10 3 1184
11 3 2865
12 3 6924
13 3 16724
4 4 1
5 4 5
6 4 18
通常遵循指数时间解决方案(在 python 中):
import itertools
choices = [-1, 0, 1]
print len([l for l in itertools.product(*([choices] * t)) if verify(n, l)])
最佳答案
观察:假设 n
至少为 1
,您所陈述问题的每个解决方案都以 [1, 0, .. ., 0]
:即单个 1
后跟零个或多个 0
。该点之前的解决方案部分是完全位于 [0, n-1]
中的步行,从 0
开始,在 n-1 结束
,并且少于 t
个步骤。
因此,您可以将原来的问题简化为一个稍微简单的问题,即确定开始时 [0, n]
中有多少 t
步走在 0
并在 n
结束(其中每个步骤可以是 0
、+1
或 -1
,和以前一样)。
下面的代码解决了更简单的问题。它使用 lru_cache
装饰器来缓存中间结果;这是在 Python 3 的标准库中,或者有一个 recipe您可以下载适用于 Python 2 的版本。
from functools import lru_cache
@lru_cache()
def walks(k, n, t):
"""
Return the number of length-t walks in [0, n]
that start at 0 and end at k. Each step
in the walk adds -1, 0 or 1 to the current total.
Inputs should satisfy 0 <= k <= n and 0 <= t.
"""
if t == 0:
# If no steps allowed, we can only get to 0,
# and then only in one way.
return k == 0
else:
# Count the walks ending in 0.
total = walks(k, n, t-1)
if 0 < k:
# ... plus the walks ending in 1.
total += walks(k-1, n, t-1)
if k < n:
# ... plus the walks ending in -1.
total += walks(k+1, n, t-1)
return total
现在我们可以使用这个功能来解决您的问题。
def solve(n, t):
"""
Find number of solutions to the original problem.
"""
# All solutions stick at n once they get there.
# Therefore it's enough to find all walks
# that lie in [0, n-1] and take us to n-1 in
# fewer than t steps.
return sum(walks(n-1, n-1, i) for i in range(t))
solve(10, 100)
在我的机器上的结果和时间:
In [1]: solve(10, 100)
Out[1]: 250639233987229485923025924628548154758061157
In [2]: %timeit solve(10, 100)
1000 loops, best of 3: 964 µs per loop
关于计算数字总和到固定结果的组合数的算法难题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709311/
我正在尝试实现 3 列固定-流体-固定布局。此外,布局的高度必须占据整个屏幕,使其看起来像从上到下的 3 个实心列。 总结: Left-column: fixed-width Center-col
我在网上搜索过,似乎找不到一个干净、简单、所有浏览器都友好的 3 列布局。 我希望有 3 列布局,左列固定为 200px,右列固定为 200px,中间列为剩余宽度,但最小宽度为 600px。所以整体最
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
我正在使用一些音频指纹技术来标记长录音中的歌曲。例如,在广播节目中的记录。指纹机制工作正常,但我在归一化(或下采样)方面遇到问题。 在这里您可以看到两首相同的歌曲,但波形不同。我知道我应该进行一些直流
为什么使用cout调用setprecision和fixed以及其他iomanip函数不像例如name.find('')plz这样的字符串函数 最佳答案 它们允许您以以下方式链接操作: cout <<
我正在尝试创建一个侧边栏,当用户向下滚动页面时,该侧边栏会跟随用户,并且我设法使用以下代码实现了效果: 标记 Javas
我正在尝试设置一些性能分析以及影响缓冲策略的运行时决策。我的应用程序收到一个指向由库(CUDA 或 OpenCL)分配的缓冲区的指针。 如何测试内存区域是否被页面锁定? 据我所知,POSIX 给了我们
我正在用 C# 编写一个 B+ 树实现,我为我的应用程序选择的树实现有一个非常特定的结构,它是缓存敏感的。为了实现这些属性,它对树节点有严格的布局策略。 我想要的只是使用 C# 的 fixed 来表达
我试图通过将 fragment 注入(inject)容器来在每个屏幕的底部放置一个广告 View 。通过使用 LinearLayout,我可以将 adview 置于操作栏下方的顶部。我已经尝试了 Re
我正在尝试创建一个导航稍微复杂的网站。我已经让导航看起来像我想要的初始加载,但现在我试图让它粘性导航到滚动顶部。我已经能够在导航栏到达顶部时创建一个类,但无法使其粘滞。每次我添加一个位置时,它都会跳回
首先,我正在寻找一个纯 CSS 解决方案。我可以使用 JavaScript 非常轻松地做到这一点,所以不要费心给我提示如何在 JS 中做到这一点。 我有一个包含 3 个容器的网页。其中 2 个是固定的
我猜这里有一种 super 特例。 我正在处理许多包含。现在我正在一个包含的内容文件中编码。我需要一个灯箱,它有一个 20% 的黑色背景覆盖整个页面,包括本身固定的标题,并在先前的 PHP 文件中设置
我正在尝试制作一个包含两个“固定”侧边栏图像和一个“固定”标题图像的页面。由于标题是固定的(距顶部 0 到 10%)。我不希望页眉图像与页面上的任何文本重叠。我尝试将段落标记定位为“相对”,并将其设置
我遇到了一个(水平)居中固定位置元素的解决方案,如下所示: element { width: 200px; position: fixed; left: 0; righ
我试图让我的网站主体具有固定的高度(我想!)。 无论如何,站点正文只是白色,边框大小为 1。基本上,正文的大小取决于其中的内容,例如,随着添加更多内容,它会自动调整大小。 我想要的是垂直滚动条,这样主
是否可以在 css 中创建 master-detail-states 布局? 我需要 3 个占位符: +---------------+-------+ | A | B
我的问题是,我是否必须在每个 Get/Post 请求之前单独请求检查 SSL Pinning OkHttpClient client = new OkHttpClient.Builder().cert
假设我有一个具有 N 个节点的常量(一旦构建就不会改变)平衡树,每个内部节点都有 p 个子节点。显然,访问节点的最坏情况是 logp(N)。但是访问 r 个节点的摊销成本呢?如果我们按升序访问它们(有
我知道会话固定是用php破解网站的方法。 会话固定是一种允许攻击者劫持有效用户会话的攻击 session fixation 但我不知道这可以防止我的项目中出现此问题。 是描述解决方案的方式或样本。 最
我在 jquery 砖石布局内有一个导航元素,我想将其修复到特定位置,比如右上角。是否可以这样做,以便所有其他元素都位于它周围? 诸如前置导航之类的东西.. 这是我的代码:http://jsfiddl
我是一名优秀的程序员,十分优秀!