yukicoder No.1644 Eight Digits
yukicoder.me
解答
from itertools import permutations K = int(input()) N = permutations('12345678',8) cnt = 0 for i in N: if int(''.join(list(i))) % K == 0: cnt += 1 print(cnt)
permutations('****', N)
これは第一引数の文字列をN個並べてできる順列を返してくれる
※ Permutation : 順列
# こんな感じ a = permutations('123',3) for i in a: print(i) => ('1', '2', '3') ('1', '3', '2') ('2', '1', '3') ('2', '3', '1') ('3', '1', '2') ('3', '2', '1')
解き方&個人的苦労
今回の問題では8! = 40320個なので、普通に繰り返しでKの倍数を数えて計算量は問題ない
個人的にはpermutations がtuple で返してくるのでそれをどう処理するかと、
Pythonにpermutationsというものがあるということに気づくまでが時間かかった