Go: ///
// File:  0546.go
// Author: ymiyamoto
//
// Created on Tue Mar  6 23:19:41 2018
//
package main

import (
	"fmt"
)

var n, k int

func perm(arr []int) [][]int {
	if len(arr) == 1 {
		return [][]int{arr}
	}

	p := perm(arr[1:])
	var ret [][]int
	for _, val := range p {
		for i := 0; i <= len(val); i++ {
			tmp := make([]int, len(val))
			copy(tmp, val)
			tmp = append(tmp, 0)
			copy(tmp[i+1:], tmp[i:])
			tmp[i] = arr[0]
			ret = append(ret, tmp)
		}
	}

	return ret
}

func permutation(n, k int) [][]int {
	ret := [][]int{}

	for i := 0; i < 1<<uint(n); i++ {
		var selects []int
		for j := 0; j < n; j++ {
			if (i>>uint(j))&1 == 1 {
				selects = append(selects, j)
			}
		}
		if len(selects) == k {
			ret = append(ret, perm(selects)...)
		}
	}

	return ret
}

func main() {
	for {
		fmt.Scan(&n, &k)
		if n == 0 && k == 0 {
			break
		}

		cards := make([]string, n)
		for i := 0; i < n; i++ {
			fmt.Scan(&cards[i])
		}

		ret := permutation(len(cards), k)
		nums := map[string]bool{}
		for _, val := range ret {
			var num string
			for i := 0; i < len(val); i++ {
				num += cards[val[i]]
			}
			nums[num] = true
		}

		fmt.Println(len(nums))
	}
}

C++: #include <set>
#include <string>
#include <iostream>

using namespace std;

int N, K; string D[10];

set<string> s;

void dfs(string S, bool ok[10], int k)
{
	if (k == K)
	{
		s.insert(S);
	}
	else
	{
		for (int i = 0; i < N; i++)
		{
			if (ok[i] == false)
			{
				bool ok2[10];

				for (int j = 0; j < 10; j++)
				{
					ok2[j] = ok[j];
				}

				ok2[i] = true;

				dfs(S + D[i], ok2, k + 1);
			}
		}
	}
}

int main()
{
	while (true)
	{
		s.clear();

		cin >> N >> K;

		if (N == 0) { break; }

		for (int i = 0; i < N; i++)
		{
			cin >> D[i];
		}

		bool ok_[10];

		for (int i = 0; i < 10; i++) { ok_[i] = false; }

		dfs("", ok_, 0);

		cout << s.size() << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;

unordered_map<string, bool> map;
void to_str(vector<int> x) {
	string str = "";
	for (int i = 0; i < x.size(); i++) {
		str += to_string(x[i]);
	}
	map[str] = true;
}

int main() {
	while (1) {
		map.clear();
		int n, k;
		cin >> n >> k;
		if (n == 0 && k == 0)break;
		vector<int> cards(n);
		for_each(cards.begin(), cards.end(), [](int &x) {cin >> x; });

		if (k == 2) {
			for (int i = 0; i < n - k + 1; i++) {
				for (int j = i + 1; j < n; j++) {
					vector<int> combi = { cards[i],cards[j] };
					sort(combi.begin(), combi.end());
					do {
						to_str(combi);
					} while (next_permutation(combi.begin(), combi.end()));
				}
			}
		}
		else if (k == 3) {
			for (int i = 0; i < n - k + 1; i++) {
				for (int j = i + 1; j < n - k + 2; j++) {
					for (int s = j + 1; s < n; s++) {
						vector<int> combi = { cards[i],cards[j],cards[s] };
						sort(combi.begin(), combi.end());
						do {
							to_str(combi);
						} while (next_permutation(combi.begin(), combi.end()));
					}
				}
			}
		}
		else if (k == 4) {
			for (int i = 0; i < n - k + 1; i++) {
				for (int j = i + 1; j < n - k + 2; j++) {
					for (int s = j + 1; s < n - k + 3; s++) {
						for (int t = s + 1; t < n; t++) {
							vector<int> combi = { cards[i],cards[j],cards[s],cards[t] };
							sort(combi.begin(), combi.end());
							do {
								to_str(combi);
							} while (next_permutation(combi.begin(), combi.end()));
						}
					}
				}
			}
		}

		cout << map.size() << endl;
	}
	return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
	int n, k, result[100], i, p, a, b, c, d, e;
	char card[10][3];
	char number[10000][9];

	for (i = 0;; i++)
	{
		cin >> n;
		cin >> k;

		a = 0;
		b = 0;
		c = 0;
		d = 0;
		e = 0;
		p = 0;

		if (n == 0 && k == 0)
		{
			break;
		}

		for (int a = 0; a < n; a++)
		{
			cin >> card[a];
		}

		if (k == 2)
		{
			result[i] = n*(n - 1);
			for (a; a < n*n; a++)
			{
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				c++;
			}
			for (int a = 0; a < n*n - 1; a++)
			{
				if (atoi(number[a]) != 0)
				{
					for (int b = a + 1; b < n*n; b++)
					{
						if (atoi(number[a]) == atoi(number[b]))
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}

		else if (k == 3)
		{
			result[i] = n*(n - 1)*(n - 2);
			for (int a = 0; a < n*n*n; a++)
			{
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && c != d && d != b)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				d++;
			}
			for (int a = 0; a < n*n*n - 1; a++)
			{
				if (atoi(number[a]) != 0)
				{
					for (int b = a + 1; b < n*n*n; b++)
					{
						if (atoi(number[a]) == atoi(number[b]))
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}
	
		else if (k == 4)
		{
			result[i] = n*(n - 1)*(n - 2)*(n - 3);
			for (int a = 0; a < n*n*n*n; a++)
			{
				if (e >= n)
				{
					e = 0;
					d++;
				}
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && b != d && b != e && c != d && c != e && d != e)
					{
						for (int f = 0; f < 3; f++)
						{
							for (int f = 0; f < 3; f++)
							{
								if (card[b][f] != *"")
								{
									number[a][p] = card[b][f];
									p++;
								}
								else
								{
									break;
								}
							}
							for (int f = 0; f < 3; f++)
							{
								if (card[c][f] != *"")
								{
									number[a][p] = card[c][f];
									p++;
								}
								else
								{
									break;
								}
							}
							for (int f = 0; f < 3; f++)
							{
								if (card[d][f] != *"")
								{
									number[a][p] = card[d][f];
									p++;
								}
								else
								{
									break;
								}
							}
							for (int f = 0; f < 3; f++)
							{
								if (card[e][f] != *"")
								{
									number[a][p] = card[e][f];
									p++;
								}
								else
								{
									number[a][p] = *"";
									p = 0;
									break;
								}
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
			}
		}
		for (int a = 0; a < n*n*n*n - 1; a++)
		{
			if (atoi(number[a]) != 0)
			{
				for (int b = a + 1; b < n*n*n*n; b++)
				{
					if (atoi(number[a]) == atoi(number[b]))
					{
						number[a][0] = *"0";
						number[a][1] = *"";
						result[i]--;
						break;
					}
				}
			}
		}
	}

	for (int a = 0; a < i; a++)
	{
		cout << result[a] << endl;
	}

	return 0;
}