given numbers 1..100, find the missing number(s)

1-N数字,1个丢失,用O(1)找到丢失数字。

k个丢失,还是用O(1).


Here's a summary of Dimitris Andreou's link.

Remember sum of i-th powers, where i=1,2,..,k. This reduces the problem to solving the system of equations

a1 + a2 + ... + ak = b1

a12 + a22 + ... + ak2 = b2

...

a1k + a2k + ... + akk = bk

Using Newton's identities, knowing bi allows to compute

原来牛顿等式还可以计算出这样的变化,没有仔细看就会直接以为bi可以计算出ci

后来想想为什么呢。

c1 = a1 + a2 + ... ak

c2 = a1a2 + a1a3 + ... + ak-1ak

...

ck = a1a2 ... ak

Formulation in terms of symmetric polynomials[edit source | editbeta]

Let x1,…, xn be variables, denote for k ≥ 1 by pk(x1,…,xn) the k-th power sum:

p_k(x_1,\ldots,x_n)=\sum\nolimits_{i=1}^nx_i^k = x_1^k+\cdots+x_n^k,

and for k ≥ 0 denote by ek(x1,…,xn) the elementary symmetric polynomial that is the sum of all distinct products of k distinct variables, so in particular

\begin{align}e_0(x_1,\ldots,x_n) &= 1,\\e_1(x_1,\ldots,x_n) &= x_1+x_2+\cdots+x_n,\\e_2(x_1,\ldots,x_n) &= \textstyle\sum_{1 \leq i<j\leq n}x_ix_j,\\e_n(x_1,\ldots,x_n) &= x_1x_2\cdots x_n,\\e_k(x_1,\ldots,x_n) &= 0, \quad\text{for}\ k>n.\\\end{align}

Then Newton's identities can be stated as

 ke_k(x_1,\ldots,x_n) = \sum_{i=1}^k(-1)^{i-1} e_{k-i} (x_1,\ldots,x_n) p_i(x_1,\ldots,x_n),

valid for all k ≥ 1. Concretely, one gets for the first few values of k:

\begin{align} e_1(x_1,\ldots,x_n) &= p_1(x_1,\ldots,x_n),\\ 2e_2(x_1,\ldots,x_n) &= e_1(x_1,\ldots,x_n)p_1(x_1,\ldots,x_n)-p_2(x_1,\ldots,x_n),\\ 3e_3(x_1,\ldots,x_n) &= e_2(x_1,\ldots,x_n)p_1(x_1,\ldots,x_n) - e_1(x_1,\ldots,x_n)p_2(x_1,\ldots,x_n) + p_3(x_1,\ldots,x_n).\\ \end{align}

The form and validity of these equations do not depend on the number n of variables (although the point where the left-hand side becomes 0 does, namely after the n-th identity), which makes it possible to state them as identities in the ring of symmetric functions. In that ring one has

\begin{align} e_1 &= p_1,\\ 2e_2 &= e_1p_1-p_2,\\ 3e_3 &= e_2p_1 - e_1p_2 + p_3,\\ 4e_4 &= e_3p_1 - e_2p_2 + e_1p_3 - p_4,\\\end{align}

and so on; here the left-hand sides never become zero. These equations allow to recursively express the ei in terms of the pk; to be able to do the inverse, one may rewrite them as

\begin{align} p_1 &= e_1,\\ p_2 &= e_1p_1-2e_2,\\ p_3 &= e_1p_2 - e_2p_1 + 3e_3 ,\\ p_4 &= e_1p_3 - e_2p_2 + e_3p_1 - 4e_4, \\     & {}\ \ \vdots\end{align}

牛顿还是相当牛逼啊!


If you expand the polynomial (x-a1)...(x-ak) the coefficients will be exactly c1, ..., ck - see Viète's formulas. Since every polynomial factors uniquely (ring of polynomials is an Euclidean domain), this means ai are uniquely determined, up to permutation.

This ends a proof that remembering powers is enough to recover the numbers. For constant k, this is a good approach.

However, when k is varying, the direct approach of computing c1,...,ck is prohibitely expensive, since e.g. ck is the product of all missing numbers, magnitude n!/(n-k)!. To overcome this, perform computations in Zqfield, where q is a prime such that n <= q < 2n - it exists by Bertrand's postulate. The proof doesn't need to be changed, since the formulas still hold, and factorization of polynomials is still unique. You also need an algorithm for factorization over finite fields, for example the one by Berlekamp or Cantor-Zassenhaus.

High level pseudocode for constant k:

  • Compute i-th powers of given numbers
  • Subtract to get sums of i-th powers of unknown numbers. Call the sums bi.
  • Use Newton's identities to compute coefficients from bi; call them ci. Basically, c1 = b1; c2 = (c1b1 - b2)/2; see Wikipedia for exact formulas
  • Factor the polynomial xk-c1xk-1 + ... + ck.
  • The roots of the polynomial are the needed numbers a1, ..., ak.

对于变化k部分,懒得看了=-=
以后学习吧。

For varying k, find a prime n <= q < 2n using e.g. Miller-Rabin, and perform the steps with all numbers reduced modulo q.

As Heinrich Apfelmus commented, instead of a prime q you can use q=2⌈log n⌉ and perform arithmetic in finite field.





参考:

http://stackoverflow.com/questions/3492302/easy-interview-question-got-harder-given-numbers-1-100-find-the-missing-numbe


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值