Problem:


カード並べ

問題

花子さんは n 枚（4 ≤ n ≤ 10）のカードを並べて遊んでいる．
それぞれのカードには 1 以上 99 以下の整数が 1 つずつ書かれている．
花子さんは，これらのカードの中から k 枚（2 ≤ k ≤ 4）を選び，
横一列に並べて整数を作ることにした．
花子さんは全部で何種類の整数を作ることができるだろうか．

例えば，1, 2, 3, 13, 21 の 5 枚のカードが与えられ，そこから 3 枚を選び整数を作ることを考える．
2, 1, 13 をこの順に並べると，整数 2113 を作ることができる．
また，21, 1, 3 をこの順に並べても，同じ整数 2113 を作ることができる．
このように，異なるカードの組み合わせから同じ整数が作られることもある．

n 枚のカードに書かれた整数が与えられたとき，
その中から k 枚を選び，横一列に並べることで作ることができる整数の個数を求めるプログラムを作成せよ．

入力

入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．

各データセットは 2+n 行からなる．
1 行目にはカードの枚数 n （4 ≤ n ≤ 10）が，
2 行目にはカードを選ぶ枚数 k （2 ≤ k ≤ 4）が書かれている．
2+i 行目（1 ≤ i ≤ n）には i 枚目のカードに書かれている 1 以上 99 以下の整数が書かれている．

n, k がともに 0 のとき入力の終了を示す.データセットの数は 5 を超えない．

出力

データセットごとに，花子さんが作ることができる整数の個数を1 行に出力する．

入出力例

入力例

4
2
1
2
12
1
6
3
72
2
12
7
2
1
0
0

出力例

7
68

1つ目の入力例 において，
1, 2, 12, 1 の 4 枚のカードの中から 2 枚を選び，横一列に並べて作ることができる整数は，
11, 12, 21, 112, 121, 122, 212 の 7 個である．

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
4
2
1
2
12
1
6
3
72
2
12
7
2
1
0
0


Sample output:
7
68

Submission status: Wrong Answer

Solution code:
from itertools import permutations as P
import sys
r=sys.stdin.readline
for e in iter(r,'0\n'):
 n,k=int(e),int(r())
 C=[r()for _ in[0]*n]
 print(len(set(''.join(s)for s in P(C,k))))

Submission status: Accepted

Solution code:
from itertools import permutations
while True:
    n, k = int(input()), int(input())
    if not n:
        break
    a = [input() for _ in [0]*n]
    s = set(int("".join(a)) for a in permutations(a, k))
    print(len(s))

Submission status: Accepted

Solution code:
def calc(l,m,k,ans):
    if k == 0:
        s = ""
        for i in m:
            s += str(l[i])
        ans.add(s)
        return
    for i in range(len(l)):
        if not i in m:
            m.append(i)
            calc(l,m,k-1,ans)
            m.remove(i)
while 1:
    n = input() 
    if n == 0: break
    k = input()
    l = [input() for i in range(n)]
    ans = set()
    calc(l,[],k,ans)
    print len(ans)

Submission status: Accepted

Solution code:
import itertools
while True:
    n,k=map(int,[input()]+[input()])
    if not n|k:
        break
    a=list()
    b=set()
    for i in range(n):
        a.append(int(input()))
    c=list(itertools.permutations(a,k))
    for i in c:
        s=str()
        for j in i:
            s+=str(j)
        b.add(s)
    print(len(b))

Submission status: Accepted

Solution code:
import itertools

while True:
    n = int(input())
    k = int(input())
    if n==0: break
    deck = []
    for _ in range(n):
        deck.append(input())
    ans = set()
    for it in itertools.permutations(deck,k):
        ans.add("".join(it))
    print(len(ans))

Submission status: Accepted

Solution code:
from itertools import permutations as P

while True:
    n, k = int(input()), int(input())
    if k == 0:
        break
    card = [int(input()) for _ in range(n)]
    print(len(set([''.join(map(str, s)) for s in set(P(card, k))])))

Submission status: Accepted

Solution code:
from itertools import permutations as P

while True:
    n, k = int(input()), int(input())
    if k == 0:
        break
    card = [input() for _ in range(n)]
    print(len(set([''.join(s) for s in P(card, k)])))

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  k = int(input())
  if not n:
    break
  lst = [input() for i in range(n)]
  dic = dict()
  def func(num,selected):
    if num == 0:
      dic["".join([lst[i] for i in selected])] = 1
      return
    for i in range(n):
      if not i in selected:
        selected.append(i)
        func(num - 1, selected)
        selected.pop()
  func(k,[])

  print(len(dic))

Submission status: Accepted

Solution code:
from itertools import permutations as P
for e in iter(input,'0'):
 n,k=int(e),int(input())
 C=[input()for _ in[0]*n]
 print(len(set(''.join(s)for s in P(C,k))))

Submission status: Accepted

Solution code:
from itertools import permutations as P
import sys
r=sys.stdin.readline
for e in iter(r,'0\n'):
 n,k=int(e),int(r())
 C=[r()[:-1]for _ in[0]*n]
 print(len(set(''.join(s)for s in P(C,k))))

Submission status: Accepted

Solution code:
def s():
 from itertools import permutations as P
 for e in iter(input,'0'):
  n,k=int(e),int(input())
  C=[input()for _ in[0]*n]
  print(len(set(''.join(s)for s in P(C,k))))
if'__main__'==__name__:s()

Submission status: Accepted

Solution code:
# AOJ 0546: Lining up the cards
# Python3 2018.7.1 bal4u

import sys
from sys import stdin
input = stdin.readline

def rec(i, m):
	global n, k, ans
	if i >= k:
		if m not in dic:
			ans += 1
			dic[m] = 1
		return
	for j in range(n):
		if f[j]: continue
		f[j] = 1
		if c[j] < 10: rec(i+1, m*10 + c[j])
		else:         rec(i+1, m*100+ c[j])
		f[j] = 0

while True:
	n, k = int(input()), int(input())
	if n == 0: break
	dic = {}
	f = [0]*12
	c = [int(input()) for i in range(n)]
	ans = 0
	rec(0, 0)
	print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
 
def main():
	while True:
		line = raw_input()
		if line == "0 0": break
		n = int(line)
		k = input()
		nums = [raw_input().strip() for i in xrange(n)]
		result = set(["".join(p) for p in permutations(nums, k)])
		print (len(result))

if __name__ == '__main__':
	sys.exit(main())

Submission status: Accepted

Solution code:
import itertools
while True:
    n=raw_input()
    if n=="0 0":break
    else:
        k=input()
    if k==0:break
    L=[raw_input() for i in range(int(n))]
    S=set()
    for t in itertools.permutations(L,k):
        S.add("".join(t))
    print len(S)

Submission status: Accepted

Solution code:
import itertools
while 1:
	n = input()
	if n == 0: break
	k = input()
	C = [input() for i in range(n)]
	ans = set()
	for c in itertools.permutations(C,k):
		ans.add("".join(map(str,c)))
	print len(ans)

Submission status: Accepted

Solution code:
import itertools
while True:
    n = int(input())
    k = int(input())
    if n == 0 and k == 0:
        break
    li = [int(input()) for i in range(n)]
    p = list(itertools.permutations(li,k))
    s = set()
    for i in p:
        c  = ""
        for j in range(len(i)):
            c += str(i[j])
        if c in s:
            continue
        else:
            s.add(int(c))
    print(len(s))

Submission status: Accepted

Solution code:
import itertools

ans = []

while True:
	n = int(input())
	k = int(input())
	if n == 0 and k == 0:
		break

	cards = []
	ans_set = []

	for i in range(n):
		cards.append(input())

	for i in itertools.permutations(cards, k):
		ans_set.append(int("".join(i)))
	
	ans.append(len(set(ans_set)))

for i in ans:
	print(i)

Submission status: Accepted

Solution code:
from itertools import permutations

while 1:
    N = int(input())
    S = int(input())
    if N == 0:
        break
    l = [int(input()) for _ in range(N)]
    dic = {}
    for i in permutations(l,S):
        i = map(str,i)
        dic[("".join(i))] = 1
    print(len(dic))

Submission status: Accepted

Solution code:
def solve():
    from itertools import permutations
    from sys import stdin
    f_i = stdin
    
    while True:
        n = int(f_i.readline())
        k = int(f_i.readline())
        if n == 0:
            break
        
        cards = []
        for i in range(n):
            cards.append(f_i.readline().rstrip())
        
        nums = set(''.join(t) for t in permutations(cards, k))
        
        print(len(nums))

solve()

Submission status: Accepted

Solution code:
import sys
import itertools
if sys.platform =='win32':
	sys.stdin=open('input.txt')

def MAP(): return [int(x) for x in input().split()]

while True:
	N = int(input())
	K = int(input())

	if N==0 and K==0: break

	a = [int(input()) for _ in range(N)]

	perm = itertools.permutations(a, K)
	nums = []
	for p in perm:
		num = ''
		for s in p:
			num += str(s)
		nums.append(int(num))
	
	print(len(set(nums)))

Submission status: Accepted

Solution code:
import itertools

while 1:
    n = int(input())
    k = int(input())
    if n == k == 0:
        break
    a = []
    for i in range(n):
        a.append(input())
    
    b = []
    p = list(itertools.permutations(a, k))
    for i in p:
        b.append(''.join(i))
    c = list(set(b))
    print(len(c))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Lining up the cards
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0546

"""
import sys


from itertools import permutations

def solve(cards, k):
    return len(set([''.join(c) for c in permutations(cards, k)]))


def main(args):
    while True:
        n = int(input())
        k = int(input())
        if n == 0 and k == 0:
            break
        cards = [input()  for _ in range(n)]
        ans = solve(cards, k)
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import itertools

while(1):
 n = int(input())
 k = int(input())
 if((n == 0)and(k == 0)): break

 num = []
 for i in range(n): num.append(input())
 #print('num :::',num)
 
 A = []
 for i in range(n): A.append(i)
 #print('A :::',A)

 num_list = []
 Comb = list(itertools.permutations(A,k))
 #print('Comb :::',len(Comb))
 
 for i in range(len(Comb)):
  tmp = list(Comb[i])
  tmp_num = ''
  for j in range(len(tmp)):
   tmp_num += num[tmp[j]]
  tmp_num = int(tmp_num)
  num_list.append(tmp_num)

 ans = len(list(set(num_list))) 
 print(ans)
 #print('aaaa')

Submission status: Accepted

Solution code:
while True:
    n=int(input())
    k=int(input())
    if n==0:
        break
    c=[input() for _ in [0]*n]
    
    s=set()
    
    from itertools import permutations
    
    cnt=0
    for i in permutations(range(n),k):
        s.add(int("".join([c[j] for j in i])))
    print(len(s))

Submission status: Accepted

Solution code:
import itertools

while(True):
  n = int(input())
  k = int(input())
  if (n,k) == (0,0):
    break
  num_lis = []
  for _ in range(n):
    num_lis.append(input())
  word_list = []
  
  for item in list(itertools.permutations(num_lis,k)):
    word_list.append("".join(item))
  
  print(len(set(word_list)))

Submission status: Accepted

Solution code:
from itertools import permutations

def solve(n,k):
    c=[]
    s=set()
    for _ in range(n):
        c+=[int(input())]

    for e in permutations(c,k):
        tmp=''
        for f in e:
            tmp+=str(f)
        s.add(tmp)
    print(len(s))


for i in range(5):
    n=int(input())
    k=int(input())
    if n==k==0:exit()
    solve(n,k)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    k=int(input())
    if n==k==0:break
    card=[]
    ans=[]
    for i in range(n):
        card.append(str(input()))
    if k==2:
        for i in range(n):
            for j in range(n):
                if i==j:continue
                ans.append(card[i]+card[j])
    elif k==3:
        for i in range(n):
            for j in range(n):
                if i==j:continue
                for k in range(n):
                    if i==k or j==k:continue
                    ans.append(card[i]+card[j]+card[k])
    elif k==4:
        for i in range(n):
            for j in range(n):
                if i==j:continue
                for k in range(n):
                    if i==k or j==k:continue
                    for l in range(n):
                        if i==l or j==l or k==l:continue
                        ans.append(card[i]+card[j]+card[k]+card[l])    
    ans=list(set(ans))
    print(len(ans))

Submission status: Accepted

Solution code:
n = k = 0
cards = []
mini_ans = {}


def dfs(s: list) -> None:
    global n, k, cards, mini_ans
    if len(s) == k:
        string = ""
        for i in s:
            string += cards[i]
        mini_ans.add(string)
        return

    for i in range(n):
        if i not in s:
            dfs(s + [i])


def main() -> None:
    ans = []

    global n, k, cards, mini_ans
    while True:
        n, k = [int(input()) for _ in range(2)]
        if n + k == 0:
            break
        cards = [input() for _ in range(n)]
        mini_ans = set()

        dfs([])
        ans += [len(mini_ans)]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools

while 1:
    n = int(input())
    k = int(input())
    if n == 0:
        break

    card = []
    for _ in range(n):
        num = int(input())
        card.append(num)

    ans = []
    for permu in itertools.permutations(card, k):
        num = ''.join(str(p) for p in permu)
        ans.append(num)

    ans = list(set(ans))
    print(len(ans))

Submission status: Runtime Error

Solution code:
from itertools import permutations as P
import sys
r=sys.stdin.readline
for e in iter(r,'0'):
 n,k=int(e),int(r())
 C=[r()for _ in[0]*n]
 print(len(set(''.join(s)for s in P(C,k))))

Submission status: Runtime Error

Solution code:
from itertools import permutations

while True:
	n = input()
	if n == 0: break
	k = input()
	nums = [raw_input() for i in xrange(n)]
	result = set("".join(p) for p in permutations(nums, k))
	print len(result)

Submission status: Runtime Error

Solution code:
from itertools import permutations

while True:
	n = input()
	k = input()
	if n == k == 0: break
	nums = [raw_input() for i in xrange(n)]
	result = set("".join(p) for p in permutations(nums, k))
	print len(result)

Submission status: Runtime Error

Solution code:
from itertools import permutations

while True:
	n = int(raw_input().strip())
	k = int(raw_input().strip())
	if n == k == 0: break
	nums = [raw_input().strip() for i in xrange(n)]
	result = set("".join(p) for p in permutations(nums, k))
	print len(result)

Submission status: Runtime Error

Solution code:
from itertools import permutations

while True:
	n = int(raw_input().strip())
	k = int(raw_input().strip())
	if n == k == 0:
		break
	nums = [raw_input().strip() for i in xrange(n)]
	result = set(["".join(p) for p in permutations(nums, k)])
	print len(result)

Submission status: Runtime Error

Solution code:
while True:
	n = int(raw_input().strip())
	k = int(raw_input().strip())
	if n == k == 0:
		break
	nums = [raw_input().strip() for i in xrange(n)]
	print 0

Submission status: Runtime Error

Solution code:
from itertools import permutations

while True:
	n = input()
	k = input()
	if n == k == 0:
		break
	nums = [raw_input().strip() for i in xrange(n)]
	result = set(["".join(p) for p in permutations(nums, k)])
	print(len(result))

Submission status: Runtime Error

Solution code:
import sys
from itertools import permutations

while True:
	n = input()
	k = input()
	if n == k == 0:
		sys.exit(0)
	nums = [raw_input().strip() for i in xrange(n)]
	result = set(["".join(p) for p in permutations(nums, k)])
	print(len(result))

Submission status: Runtime Error

Solution code:
import sys
from itertools import permutations

def main():
	while True:
		n = input()
		k = input()
		if n == k == 0:
			return 0
		nums = [raw_input().strip() for i in xrange(n)]
		result = set(["".join(p) for p in permutations(nums, k)])
		print (len(result))

if __name__ == '__main__':
	sys.exit(main())

Submission status: Runtime Error

Solution code:
import sys
from itertools import permutations

def main():
	while True:
		n, k = map(int, raw_input().split())
		if n == k == 0:
			sys.exit(0)
		nums = [raw_input().strip() for i in xrange(n)]
		result = set(["".join(p) for p in permutations(nums, k)])
		print (len(result))

if __name__ == '__main__':
	sys.exit(main())

Submission status: Runtime Error

Solution code:
import sys
from itertools import permutations

def main():
	while True:
		n = input()
		k = input()
		if n == k == 0:
			sys.exit(0)
		for i in xrange(n):
			input()
#		nums = [raw_input().strip() for i in xrange(n)]
#		result = set(["".join(p) for p in permutations(nums, k)])
#		print (len(result))

if __name__ == '__main__':
	sys.exit(main())

Submission status: Runtime Error

Solution code:
import itertools
while True:
    n=input()
    k=input()
    if n==k==0:break
    L=[raw_input() for i in range(n)]
    S=set()
    for t in itertools.permutations(L,k):
        S.add("".join(t))
    print len(S)

Submission status: Runtime Error

Solution code:
import itertools
while True:
    n=raw_input()
    if n in "0":break
    try:
        k=input()
    except SyntaxError:
        break
    L=[raw_input() for i in range(int(n))]
    S=set()
    for t in itertools.permutations(L,k):
        S.add("".join(t))
    print len(S)