天天看點

Project Euler 90:Cube digit pairs 立方體數字對

Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers.

For example, the square number 64 could be formed:

Project Euler 90:Cube digit pairs 立方體數字對

In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81.

For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other cube.

However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09.

In determining a distinct arrangement we are interested in the digits on each cube, not the order.

{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?

立方體數字對

在一個立方體的六個面上分别标上不同的數字(從0到9),對另一個立方體也如法炮制。将這兩個立方體按不同的方向并排擺放,我們可以得到各種各樣的兩位數。

例如,平方數64可以通過這樣擺放獲得:

Project Euler 90:Cube digit pairs 立方體數字對

事實上,通過仔細地選擇兩個立方體上的數字,我們可以擺放出所有小于100的平方數:01、04、09、16、25、36、49、64和81。

例如,其中一種方式就是在一個立方體上标上{0, 5, 6, 7, 8, 9},在另一個立方體上标上{1, 2, 3, 4, 8, 9}。

在這個問題中,我們允許将标有6或9的面颠倒過來互相表示,隻有這樣,如{0, 5, 6, 7, 8, 9}和{1, 2, 3, 4, 6, 7}這樣本來無法表示09的标法,才能夠擺放出全部九個平方數。

在考慮什麼是不同的标法時,我們關注的是立方體上有哪些數字,而不關心它們的順序。

{1, 2, 3, 4, 5, 6}等價于{3, 6, 4, 1, 2, 5}

{1, 2, 3, 4, 5, 6}不同于{1, 2, 3, 4, 5, 9}

但因為我們允許在擺放兩位數時将6和9颠倒過來互相表示,這個例子中的兩個不同的集合都可以代表拓展集{1, 2, 3, 4, 5, 6, 9}。

對這兩個立方體有多少中不同的标法可以擺放出所有的平方數?

解題

 我發現這個翻譯我了解不透

在兩個六面體上面塗:0-9的數字,主要這裡有10個數字,隻用其中6個圖,兩個六面體塗的數字可以不一樣的,6可以當9用,9可以當6用。

兩個六面體上面的數字能組合成:1-9的平方:01 04 09 16 25 36 49 64 81  ,求這樣的塗法有多少種?

骰子說成六面體還吊的。

0-9 十個數 取出6個 就是一個骰子的塗法。

先組合出塗法的種類。

再判斷是否能組成1-9的平方

參考程式 

# coding=gbk

import time as time 
from itertools import combinations  
def run():
    dice=list(combinations([0,1,2,3,4,5,6,7,8,6],6))
    ans = 0 
    for i1,d1 in enumerate(dice):
        for d2 in dice[i1:]:
            if valid(d1,d2) == True:
                ans +=1
    print ans 

def valid(c1,c2):
    squares=[(0,1),(0,4),(0,6),(1,6),(2,5),(3,6),(4,6),(8,1)]
    return all(x in c1 and y in c2 or x in c2 and y in c1 for x,y in squares)

t0 = time.time()
run() 
t1 = time.time()
print "running time=",(t1-t0),"s"      

# 1217

# running time= 0.0620000362396 s