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<iostream>
#include <stdlib.h>

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;
}

C++: #include<stdio.h>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>     
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <iomanip>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define rep(a,t) for(int a=0;a<t;a++) 
#define forever while(true)
#define Sort(a) sort(a.begin(),a.end())
#define Reverse(a) reverse(a.begin(),a.end())
#define pb push_back
#define int_maxvalue numeric_limits<int>::max()
int n, k;
set<string>a;
string strs[10];
void put() {
	if (k == 2) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j) { continue; }
				a.insert((strs[i] + strs[j]));
			}
		}
	}
	if (k == 3) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int i1 = 0; i1 < n; i1++) {
					if (i == j || i == i1 || j == i1) { continue; }
					a.insert((strs[i] + strs[j] + strs[i1]));
				}
			}
		}
	}
	if (k == 4) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j) { continue; }
				for (int i1 = 0; i1 < n; i1++) {
					if (i == i1 || j == i1) { continue; }
					for (int i2 = 0; i2 < n; i2++) {
						if (i == i2 || j== i2 || i2 == i1) { continue; }
						a.insert((strs[i] + strs[j] + strs[i1]+strs[i2]));
					}
				}
			}
		}
	}
	return;
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	while (true) {
		cin >> n >> k;
		a.clear();

		if (n == 0 && k == 0) { break; }
		rep(i, n) { cin >> strs[i]; }
		put();
		cout << a.size() << endl;
	}
	return 0;
}