Cattle off net patchwork coins

Patchwork coins

Subject description:

Q small very rich, with a lot of coins coins, small Q have is a regular, for all non-negative integer K, Q just have two small denominations of coins 2 ^ K, Q has so little coin is 1,1,2,2,4,4,8,8, .... Q small one day go to the store to buy something to pay n dollars, small Q want to know how many kinds of programs selected from the coin he has pieced together exactly some n-ary (if two programs a face value of coins selected number not the same as it is considered to be a different program).

Reference answer solution to a problem is, sigh too strong;
\ (Recur (the n-m-2 ^) \) represents the largest use of \ (2 ^ m \) to build n, where the edge needed to change my thinking, I can not used directly to \ (2 ^ m \) of the number of methods,
but this method is actually the number and build \ ((n-2 ^ m ) \) number is the same;
\ (Recur (2 ** (m +1) -2-n) \) represents not used \ (2 ^ m \) to build n, the need for understanding, without the use of \ (m ^ 2 \) , the two will now be left \ (^ 2 0, \ cdots, 2 ^ (m-. 1) \) , which add up to exactly which \ (2 ^ (m +. 1) -2 \) , using the same idea can represent. I can only say that really is too strong;

import sys
import math
 
n = int(sys.stdin.readline())
mem = {-1:0,0:1,1:1}
def recur(n):
    if n in mem:
        return mem[n]
    else:
        m = int(math.log2(n))
        res = recur(n-2**m)+recur(2**(m+1)-2-n)
        mem[n] = res
        return res
print(recur(n))

Guess you like

Origin www.cnblogs.com/curtisxiao/p/11366164.html