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 <cmath>
#include <string>
#include <cstring>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef long long ll;



void dfs(int i, int cnt, string *C){
}

int main(){
    int n, k;
    while(cin>>n>>k, n|k){
        vector<string> C(n);
        REP(j, n) cin >> C[j] ;
        set<string> strs;
        for(int mask=0; mask< (1 << n); mask++){
            int total_1=0;
            for(int i=0; i<n; i++)
                if(mask >> i & 1) total_1++;
            if(total_1 != k) continue;
            vector<string> cards;
            for(int i=0; i<n; i++)
                if(mask >> i & 1) cards.push_back(C[i]);
            sort(ALL(cards));
            do{
                string tmp;
                for( auto num : cards)
                    tmp+=num;
                if(strs.find(tmp)==strs.end())
                    strs.insert(tmp);
            }while(next_permutation(ALL(cards)));
        }
        cout << strs.size() << endl;
    }
    return 0;
}

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

int main()
{
	int n,k;
	string str;
	while( cin >> n >> k && n || k ){
		set<string> ret;
		vector<string> num(n);
		for(int i=0;i<n;i++){
			cin >> num[i];
		}
		sort(num.begin(),num.end());
		do{
			str="";
			for(int i=0;i<k;i++){
				str+=num[i];
			}
			ret.insert(str);
		}while( next_permutation(num.begin(),num.end()) );
		
		cout << ret.size() << endl;
	}
}

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

int main() {
    while (true) {
        int n, k;
        cin >> n >> k;
        if (!n & !k) break;
        int a[10], use[10] = {};
        for (int i = 0; i < n; ++i) cin >> a[i];
        sort(a, a + n);
        for (int i = 0; i < k; ++i) use[i] = 1;
        vector<string> v;
        do {
            int choise[k];
            for (int i = 0, j = 0; i < n; ++i) {
                if (use[i]) choise[j++] = a[i];
            }
            do {
                string s;
                for (int i = 0; i < k; ++i) s += to_string(choise[i]);
                bool can = true;
                for (int i = 0; i < v.size(); ++i) {
                    if (s == v[i]) {
                        can = false;
                        break;
                    }
                }
                if (can) v.push_back(s);
            } while (next_permutation(choise, choise + k));
        } while (prev_permutation(use, use + n));
        cout << v.size() << endl;
    }
    
    return 0;
}

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

int main() {
    while (true) {
        int n, k;
        cin >> n >> k;
        if (!n & !k) break;
        string a[n];
        for (int i = 0; i < n; ++i) cin >> a[i];
        sort(a, a + n);
        vector<string> v;
        do {
            string s;
            for (int i = 0; i < k; ++i) s += a[i];
            bool can = true;
            for (int i = 0; i < v.size(); ++i) {
                if (s == v[i]) {
                    can = false;
                    break;
                }
            }
            if (can) v.push_back(s);
        } while (next_permutation(a, a + n));
        cout << v.size() << endl;
    }
    
    return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>
#include<stdlib.h>

using namespace std;
int main()
{

	bool p[1000001];
	int n, k;
	string m[1000];
	string a;
	int count = 0;
	while (1){
		count = 0;
		cin >> n >> k;
		if (n == 0)return 0;
		for (int i = 0; i < n; i++){
			cin >> m[i];
		}
		if (k == 4){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){
					for (int i3 = 0; i3 < n; i3++){
						for (int i4 = 0; i4 < n; i4++){
							if (i != i2&&i3 != i4&&i != i3&&i != i4&&i2 != i3&&i2 != i4){
								a = m[i] + m[i2] + m[i3] + m[i4];
								p[atol(a.c_str())] = false;
								a.clear();
							}
						}
					}
				}
			}

		}
		long u = 0;
		if (k == 3){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){
					for (int i3 = 0; i3 < n; i3++){
						if (i != i2&&i != i3&&i2 != i3){
							a = m[i];
							a += m[i2];
							a += m[i3];

							u = atol(a.c_str());
							p[u] = false;
							a.clear();
						}
					}
				}
			}
		}
		int o;
		if (k == 2){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){

					if (i != i2){
						a = m[i] + m[i2];
						o = atoi(a.c_str());


						p[o] = false;
						a.clear();
					}
				}
			}
		}
		
		for (int i = 0; i < 100000; i++){
			if (!p[i])count++;
		}
		cout << count << endl;
	}
}

C++: #include "stdio.h"
#include <vector>
#include <queue>

using namespace std;

char flg[100000000];

int x[10];

int main()
{
for(int i = 0; i < 100000000; i++)
{
flg[i] = 0;
}
int n,k;
scanf("%d",&n);
scanf("%d",&k);
for(int i = 0; i < n; i++)
{
scanf("%d",&x[i]);
}

int w = 1;
for(int i = 0; i < k; i++)
{
w *= n;
}

for(int i = 0; i < w; i++)
{
priority_queue<int> q1;
vector<int> q2;
int s = i;
for(int i = 0; i < k; i++)
{
int l = s % n;
q1.push(l);
q2.push_back(l);
s /= n;
}

int flgw = 0;
int mae = -1;
for(int ii = 0; ii < k; ii++)
{
if(mae == q1.top())
{
flgw = 1;
break;
}
mae = q1.top();
q1.pop();
}
if(flgw == 1)
{
continue;
}

int g = 0;
for(int ii = 0; ii < k; ii++)
{
if(x[q2[ii]] < 10)
{
g = g * 10 + x[q2[ii]];
}
else
{
g = g * 100 + x[q2[ii]];
}
}
flg[g]++;
}

int all = 0;
for(int i = 0; i < 100000000; i++)
{
if(flg[i] > 0)
{
all++;
}
}
printf("%d\n",all);
return 0;
}