ABC183 C - Travel から学んだ
4202 ワード
一瞬 グラフかな?っと思った。
だが、出力例1 にヒントがあった。
1 => ********** => 1 に戻ってくる。
間の ********** は 2 ~ 8 の並べ替え。
例えば 1=> 2 => 3 => 4 => 1 を考える。
行列 T から以下のように値を取り出して足し合わせて K と一致する組み合わせをカウントすれば良い。
1行2列 1=> 2 => 3 => 4 => 1
2行3列 1=> 2 => 3 => 4 => 1
3行4列 1=> 2 => 3 => 4 => 1
4行1列 1=> 2 => 3 => 4 => 1
Travel.py
n,k = map(int,input().split())
T = [list(map(int,input().split())) for _ in range(n)]
from itertools import permutations
cnt = 0
for nums in permutations(range(1,n)):#O(ザックリ40000)
#print(nums)
nums = [0]+list(nums)+[0]
score = 0
for i in range(len(nums)-1):#O(9)
score += T[nums[i]][nums[i+1]]
#print(score)
if score == k:
cnt += 1
print(cnt)
#O(360000) .. ザックリだけど間に合うっしょ。
Author And Source
この問題について(ABC183 C - Travel から学んだ), 我々は、より多くの情報をここで見つけました https://qiita.com/AKpirion/items/4f5eb1d1594b29d93a92著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .