示例#1
0
#Let S(A) represent the sum of elements in set A of size n. We shall call it a special sum set if for any two non-empty disjoint subsets, B and C, the following properties are true:

   #1. S(B) ≠ S(C); that is, sums of subsets cannot be equal.
   #2. If B contains more elements than C then S(B) > S(C).

#For this problem we shall assume that a given set contains n strictly increasing elements and it already satisfies the second rule.

#Surprisingly, out of the 25 possible subset pairs that can be obtained from a set for which n = 4, only 1 of these pairs need to be tested for equality (first rule). Similarly, when n = 7, only 70 out of the 966 subset pairs need to be tested.

#For n = 12, how many of the 261625 subset pairs that can be obtained need to be tested for equality?

#NOTE: This problem is related to problems 103 and 105.

#Answer:
	#21384

from time import time; t=time()
from mathplus import combinations

M = 12

s = 0
n = M
for k in range(2, n//2+1):
    for p in combinations(range(n), k):
        qq = [i for i in range(n) if i not in p and i > p[0]]
        for q in combinations(qq, k):
            if not (all(q[i] > p[i] for i in range(k)) or all(q[i] < p[i] for i in range(k))): s += 1

print(s)#, time()-t
示例#2
0
from time import time; t=time()
from mathplus import Cnr, combinations

NN = N = 7

l, h = 17, 50
#l, h = 11, 26

min_s = h * N
min_k = []
cache = {}
for i in range(l, h):
    cache[(i,)] = True
for N in range(2, NN+1):
    for k in combinations(range(l, h - NN + N), N):
        kk = sorted(k)
        if tuple(kk) in cache: continue
        if tuple(kk[:N-1]) not in cache: continue
        if N == NN:
            s = sum(kk)
            if s >= min_s: continue
        for i in range(1, (N+1)//2):
            if sum(kk[-i:]) >= sum(kk[:i+1]): break
        else:
            for r in range(2, N-1):
                ss = set()
                for j in combinations(kk, r):
                    ss.add(sum(j))
                if len(ss) != Cnr(N, r): break
            else:
示例#3
0
#But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2-digit numbers.

#How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?

#Answer:
#1217

from time import time
t = time()
from mathplus import combinations


def test(p, a, b):
    if p[0] in a and p[1] in b: return True
    if p[0] in b and p[1] in a: return True
    return False


s = 0
for b in combinations(range(0, 10), 6):
    if 2 not in b and 5 not in b: continue
    if 6 in b or 9 in b: b += (6, 9)
    for a in combinations(range(0, 10), 6):
        if 6 in a or 9 in a: a += (6, 9)
        for p in [(0, 1), (0, 4), (0, 9), (1, 6), (2, 5), (3, 6), (4, 9),
                  (6, 4), (8, 1)]:
            if not test(p, a, b): break
        else: s += 1

print(s // 2)  #, time()-t
示例#4
0
def get_ret2(a, b):
    r = [a+b, a-b, a*b, b-a]
    if a != 0: r.append(b/a)
    if b != 0: r.append(a/b)
    return r

@memorize
def get_ret3(abc):
    ss = set()
    for i in permutations(abc):
        a, b, c = i[0], i[1], i[2]
        for r in get_ret2(a, b):
            ss.add(tuple(sorted((r, c))))
    return set(r for ab in ss for r in get_ret2(*ab))

for k in combinations(range(1, 10), 4):
    ss = set()
    for i in permutations(k):
        a, b, c, d = i[0], i[1], i[2], i[3]
        for r in get_ret2(a, b):
            ss.add(tuple(sorted((r, c, d))))
    ret = set(r for abc in ss for r in get_ret3(abc))
    i = 1
    while True:
        if i not in ret: break
        i += 1
    i -= 1
    if i > s:
        s = i
        abcd = sorted(k)
示例#5
0
    if len_active_k > 10: continue
    full_f = [0] * 26

    def test(s):
        if full_f[s[0]] == 0: return 0
        c = full_f[s[-1]]
        if c in (0, 2, 3, 7, 8): return 0
        d = full_f[s[-2]]
        if c == 5 and d != 2: return 0
        if c == 6 and d % 2 == 0: return 0
        if d % 2 == 1: return 0
        n = reduce(lambda x, y: x * 10 + y, (full_f[c] for c in s))
        return n if isqrt(n)**2 == n else 0

    def test_xy(x, y):
        global max_value
        for f in permutations(range(10), len_active_k):
            for j in range(len_active_k):
                full_f[active_k[j]] = f[j]
            if test(x) > 0 and test(y) > 0:
                max_value = max(max_value, test(x), test(y))
                #print(v, max_value)

    if len(v) == 2:
        test_xy(v[0], v[1])
    else:
        for x, y in combinations(v, 2):
            test_xy(x, y)

print(max_value)  #, time()-t)
示例#6
0
#{1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5}
#{1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9}

#But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2-digit numbers.

#How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?

#Answer:
	#1217

from time import time; t=time()
from mathplus import combinations

def test(p, a, b):
    if p[0] in a and p[1] in b: return True
    if p[0] in b and p[1] in a: return True
    return False

s = 0
for b in combinations(range(0, 10), 6):
    if 2 not in b and 5 not in b: continue
    if 6 in b or 9 in b: b += (6, 9)
    for a in combinations(range(0, 10), 6):
        if 6 in a or 9 in a: a += (6, 9)
        for p in [(0,1),(0,4),(0,9),(1,6),(2,5),(3,6),(4,9),(6,4),(8,1)]:
            if not test(p, a, b): break
        else: s += 1

print(s//2)#, time()-t
示例#7
0
#For this problem we shall assume that a given set contains n strictly increasing elements and it already satisfies the second rule.

#Surprisingly, out of the 25 possible subset pairs that can be obtained from a set for which n = 4, only 1 of these pairs need to be tested for equality (first rule). Similarly, when n = 7, only 70 out of the 966 subset pairs need to be tested.

#For n = 12, how many of the 261625 subset pairs that can be obtained need to be tested for equality?

#NOTE: This problem is related to problems 103 and 105.

#Answer:
#21384

from time import time
t = time()
from mathplus import combinations

M = 12

s = 0
n = M
for k in range(2, n // 2 + 1):
    for p in combinations(range(n), k):
        qq = [i for i in range(n) if i not in p and i > p[0]]
        for q in combinations(qq, k):
            if not (all(q[i] > p[i]
                        for i in range(k)) or all(q[i] < p[i]
                                                  for i in range(k))):
                s += 1

print(s)  #, time()-t
示例#8
0
    #print(cnt)
    active_k = [i for i in range(26) if k[i] > 0]
    len_active_k = len(active_k)
    if len_active_k > 10: continue
    full_f = [0]*26
    def test(s):
        if full_f[s[0]] == 0: return 0
        c = full_f[s[-1]]
        if c in (0, 2, 3, 7, 8): return 0
        d = full_f[s[-2]]
        if c == 5 and d != 2: return 0
        if c == 6 and d % 2 == 0: return 0
        if d % 2 == 1: return 0
        n = reduce(lambda x,y: x*10+y, (full_f[c] for c in s))
        return n if isqrt(n)**2 == n else 0
    def test_xy(x, y):
        global max_value
        for f in permutations(range(10), len_active_k):
            for j in range(len_active_k):
                full_f[active_k[j]] = f[j]
            if test(x) > 0 and test(y) > 0:
                max_value = max(max_value, test(x), test(y))
                #print(v, max_value)
    if len(v) == 2:
        test_xy(v[0], v[1])
    else:
        for x, y in combinations(v, 2):
            test_xy(x, y)

print(max_value)#, time()-t)