Interesting questions in the project--eating dumplings

Source: Internet
Author: User

Title Description:
Recently, the project encounters an interesting topic, feeling a lot, forget it. Abstract out, roughly: a total of 100 dumplings on the table, of which there are 10 dumplings covered with coins, asked: How many times the expected number of consecutive to eat coins? First, define the continuation here if we abstract the order of eating dumplings into a 100-bit binary number. And eat dumplings expressed as 1, did not eat to 0, then:
    • If eaten once and for a second time, then it can be expressed as: 110 ..., then the number of successive eats is 1.
    • If the number is: ... 1111 ...., so here the Continuous 4 1 is 3 consecutive, that is, as long as continuous, even if 1 times. The expected number of times, that is to say we need to figure out 0 consecutive, 1 consecutive, .... 9 consecutive. These 10 counts.
Note: The continuity here is actually determined by the state of the previous position in the current position. So you can use DP to solve。 This topic is a question of numbers, and it is obvious that you can use DP to solve it. GLOBAL[I][J][K]: said the first I dumplings, has eaten a J coin, and the number of consecutive times is K. LOCAL[I][J][K]: The first I dumplings, eat a J-coin, and the K-time continuous occurrence in the first position. (That is, the first i-1 to eat dumplings). And the number of consecutive times is K.
    • LOCAL[I][J][K] = Local[i-1][j-1][k-1] + global[i-3][j-2][k-1]

    • GLOBAL[I][J][K] = Local[i][j][k] + global[i-2][j][k] + global[i-2][j-1][k] + local[i-3][j-2][k-1] + global[i-3][j-1][k]

Obviously it is unwise to use the last time as a sub-problem, and recursive relationships are too complex. The following is an analysis from another perspective: the last two bits of the state to determine:
    • 11: That is, once in a row,
    • 10, 01, 00, there is no continuous, sub-problem direct recursion can be. Therefore, the following sub-problem is defined: F[I][J][K][0/1]: The first I dumplings, ate j coins, and there are k consecutive. And the last one is 0/1 times. Then we ask for: f[100][10][k][0] + f[100][10][k][1]; K = {x-,..., 9} Note: The last dimension here is actually a 0/1 record, of course you can also use two three-dimensional array to represent. The Python code is implemented as follows:
def Compute (m,n,f,g):if n > m:return-1  For i in range (m+1):f[i][0][0] = 1  for J in Range (n+1):  f[0][j][0] = 0  G[0][j][0] = 0f[0][0][0] = 1  g[0][0][0] = 1 for I in Range (1,m+1):  for J in Range (1,min (i,n) +1):  for K in range (0,j):  F[i][j][k] = f[i-1][j][k] + g[i-1][j][k]  if k! = 0:g[i][j][k] = F[i-1][j-1][k] + g[i-1][j-1][k-1]Else:g[i][j][k] = f[i-1][j-1][k] cnt = [0 for I in range (n)]  For K in range (0,n):Cnt[k] = f[100][10][k] + g[100][10][k]  print cnt[1 :]   allsum = 1.0  For i in range (91,101): allsum = allsum * IFor i in range (1,11): Allsum/= iprint "Allsum =", Allsum Print [ float (x)/allsum for x in CNT]  allSum2 = 0For i in range (1,n): allSum2 + = (i * cnt[i])print allsum2/float (allsum) if __name__ = = "__main__":   m = n = Tenk = n-1f = [[[0 for K in range (k+1)] for J in Range (n+1)] for I in range (m+1)]g = [[[0 for K in range (k+1)] for J in Range (n+1)] for I in range (m+1)]Compute (M,N,F,G)
The result is: 0.9 This function may have multiple invocations in the current project. So the number of calculations should be as low as possible. On the other hand, this memory footprint is very large, and the values of N and M in the project are probably 10^5. How to implement it quickly and efficiently. The current scheme is: Because this is a three-bit array, then each dimension is actually a plane, then I save a fixed interval such a plane, for example, save f[i][j][100], f[i][j][200], ..., so go on, in the calculation, select the closest plane, push down. (because each plane's calculation depends only on the previous plane). The above may be a bit obscure, and a simple example is clear: for example, we are now going to write a function to solve the Fibonacci number, which is very frequent, so we recall the use of memos, but we now have a very limited amount of memory, it is not possible to save all the previously computed values. So what values do we keep, it's simple, because each number depends on its first two numbers, so we keep two numbers at intervals. For example, we save f[1000], f[1001], f[2000], f[2001],f[3000],f[3001], .... We calculate f[3076], directly from the f[3000],f[3001] start counting, do not need to start from 0, which reduces the consumption of space while minimizing the time consumption. In fact, this is a one-dimensional situation, each save is two number. The three-dimensional matrix above, each save is a two-dimensional array. Final words:
  • In this case, the dynamic language Python is a step further, the following code is correct? Try it yourself:
    tmp = [0 for I in range (k+1)]  TMP2 = [tmp[:] for I in range (n+1)]f = [tmp2[:] for I in range (m+1)]g = [tmp2[:] for I in range (m+1)]
    So define the F and g two arrays, right?
  • The sub-problem definition for DP is technique. The definition of sub-problem directly affects the complexity of recursion.

Interesting questions in the project--eating dumplings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.