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 <vector>
#include <set>
#include <algorithm>
using namespace std;

int n, k, m;
set<int> t;

void ck(int* b) {
		int s = 0;

		for (int i = 0; i < k; i++) {
			if (b[i] > 9)
				s = s * 100 + b[i];
			else
				s = s * 10 + b[i];
		}

		if (t.find(s) == t.end()) {
			t.insert(s);
			m++;
		}
}

int main()
{
	cin >> n >> k;

	while (n > 0 || k > 0) {
		vector<int> a(n);

		for (int i = 0; i < n; i++)
			cin >> a[i];

		m = 0;
		int i = (1 << k) - 1;
		t.clear();

		while (i < (1 << n)) {
			vector<int> b;
			for (int j = 0; j < n; j++) {
				if((1 << j) & i)
					b.push_back(j);
			}

			do {
				vector<int> c;
				for (int i = 0; i < k; i++)
					c.push_back(a[b[i]]);

				ck(&c[0]);
			} while (next_permutation(b.begin(), b.end()));

			int x = i & -i, y = i + x;
			i = ((i & ~y) / x >> 1) | y;
		}

		cout << m << endl;
		
		cin >> n >> k;
	}

	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;
        int 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 += to_string(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 <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
# define ALL(x)     (x).begin(),(x).end()
# define UNIQ(c)    (c).erase(unique(ALL((c))), end((c)))
# define mp         make_pair
# define eb         emplace_back
# define FOR(i,a,b) for(int i=(a);i<(b);++i)
# define REP(i,n)   FOR(i,0,n)
# define INIT       std::ios::sync_with_stdio(false);std::cin.tie(0);


int main() {
	int N, K;
	while (cin >> N >> K&&N&&K) {
		vector<int> R(N);
		int ans = 0;
		map<int, bool> appeared;
		for (int i = 0; i < N; i++) cin >> R[i];

		for (int i = 0; i < (1 << N); i++) {
			vector<int> cards;
			for (int k = 0; k < N; k++) {
				if ((i >> k) & 1) cards.push_back(R[k]);
			}
			if (cards.size() != K) continue;
			sort(cards.begin(), cards.end());

			do {
				string joined = "";
				for (auto val : cards) {
					joined += to_string(val);
				}
				if (!appeared[stoi(joined)]) {
					appeared[stoi(joined)] = true;
					ans++;
				}
			} while (next_permutation(cards.begin(), cards.end()));
		}
		cout << ans << endl;
	}
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	while(1){
	int a,b,c,x[11];
vector<int>vec;
	scanf("%d",&a);
	scanf("%d",&b);
	if(a==0){
		break;
	}
	for(int i=0;i<a;i++){
		scanf("%d",&x[i]);
	}
	if(b==2){
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				if(j!=i){
				if(x[j]>=10){
					vec.push_back(x[i]*100+x[j]);
				}else{
					vec.push_back(x[i]*10+x[j]);
				}
				}
			}
		}
	}else if(b==3){
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				if(j!=i){
				for(int k=0;k<a;k++){
					if(k!=i){
						if(k!=j){
					if(x[k]>=10){
						if(x[j]>=10){
							vec.push_back(x[i]*10000+x[j]*100+x[k]);
						}else{
							vec.push_back(x[i]*1000+x[j]*100+x[k]);
						}
					}
					else{
						if(x[j]>=10){
							vec.push_back(x[i]*1000+x[j]*10+x[k]);
						}else{
							vec.push_back(x[i]*100+x[j]*10+x[k]);
						}
					}
				}
			}
		}
				}
			}
		}
	}else if(b==4){
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				if(j!=i){
				for(int k=0;k<a;k++){
					if(k!=i){
						if(k!=j){
					for(int h=0;h<a;h++){
						if(h!=i){
							if(h!=j){
								if(h!=k){
						if(x[h]>=10){
							if(x[k]>=10){
								if(x[j]>=10){
									vec.push_back(x[i]*1000000+x[j]*10000+x[k]*100+x[h]);
								}else{
									vec.push_back(x[i]*100000+x[j]*10000+x[k]*100+x[h]);
								}
							}else{
								if(x[j]>=10){
									vec.push_back(x[i]*100000+x[j]*1000+x[k]*100+x[h]);
								}else{
									vec.push_back(x[i]*10000+x[j]*1000+x[k]*100+x[h]);
								}
							}
						}else{
							if(x[k]>=10){
								if(x[j]>=10){
									vec.push_back(x[i]*100000+x[j]*1000+x[k]*10+x[h]);
								}else{
									vec.push_back(x[i]*10000+x[j]*1000+x[k]*10+x[h]);
								}
							}else{
								if(x[j]>=10){
									vec.push_back(x[i]*10000+x[j]*100+x[k]*10+x[h]);
								}else{
									vec.push_back(x[i]*1000+x[j]*100+x[k]*10+x[h]);
								}
							}
						}
					}
				}
					}
					}
						}
						}
				}
				}
			}
		}
	}
	sort(vec.begin(),vec.end());
	int ans=1;
	for(int i=0;i<vec.size()-1;i++){
		if(vec[i]!=vec[i+1]){
			ans++;
		}
	}
	printf("%d\n",ans);
	}
	return 0;
}

C++: #include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;

map<string,bool> m;
int n,k;
string a[20];

bool f[20];
void dfs(string str,int dep){
    if(dep == k){
        m[str] = true;
        return;
    }
    for(int i = 0;i < n;i++){
        if(f[i] == false&&(dep!=0||a[i][0]!='0')){
            f[i] = true;
            dfs(str + a[i],dep+1);
            f[i] = false;
        }
    }
}
int main(void){

    while(cin>>n>>k,k){
        m.clear();
        for(int i = 0;i < 20;i++)a[i] = "",f[i] = false;
        for(int i = 0;i < n;i++){
            cin>>a[i];
        }
        dfs("",0);
        int cnt = 0;
        map<string,bool>::iterator it = m.begin();
        while( it != m.end() )
        {
            //cout << (*it).first << ":" << (*it).second << endl;
            ++it;
            cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}