把問題轉換成一個只有0和1的二維陣列,
例如題意轉換為
[0,0,0,0,1,0,0,0,0,0]
[0,0,0,0,0,0,0,1,0,0]
[0,0,1,0,0,0,0,0,0,0]
[0,0,0,0,0,0,0,0,0,1]
[0,0,0,0,0,0,1,0,0,0]
[1,0,0,0,0,0,0,0,0,0]
[0,0,0,0,0,0,0,0,1,0]
[0,1,0,0,0,0,0,0,0,0]
[0,0,0,0,0,1,0,0,0,0]
[0,0,0,1,0,0,0,0,0,0]
其實就是在算有幾個cycle(自己和自己是朋友也是一個cycle)
for all i,i=0~N-1先算A[i][i]有幾個1
再來就是你想的那樣子做,當N非常大的時候會快很多