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 <map>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstring>
#include <tuple>
#include <utility>
#include <list>
#include <cmath>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const LL LINF = 1LL << 60;
const LL MOD = 100000;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define all(c) (c).begin(), (c).end()

struct edge{ LL to, cost; edge(int t, LL c) :to(t), cost(c){} };

int main()
{
	int n;
	while (cin >> n && n)
	{
		int k; cin >> k;
		set<int> nums;
		vector<int> arr(n);
		rep(i, n)cin >> arr[i];

		function<int(int)> getKeta = [](int num)
		{
			int ret = 10;
			while (num /= 10)
			{
				ret *= 10;
			}
			return ret;
		};

		vector<bool> used(10);
		function<void(int, int, int)> dfs = [&](int idx, int pre, int rem)
		{
			if (used[idx]) return;
			if (rem == 0)
			{
				nums.insert(pre);
				return;
			}
			if (idx == n) return;
			used[idx] = true;
			int next1 = (pre * (arr[idx] >= 10 ? 100 : 10)) + arr[idx];
			for (int i = 0; i < n; i++)
			{
				dfs(i, next1, rem - 1);
			}
			used[idx] = false;
		};
		rep(i, n)dfs(i, 0, k);
		cout << nums.size() << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int n, k;
    while (cin >> n >> k, n) {
        vector<string> a(n);
        rep(i, n) cin >> a[i];

        set<string> st;
        rep(s, 1 << n) {
            if (__builtin_popcount(s) != k) continue;

            vector<int> b;
            rep(i, n) {
                if ((s >> i) & 1) {
                    b.emplace_back(i);
                }
            }

            do {
                string t = "";
                rep(i, b.size()) {
                    t += a[b[i]];
                }
                if (st.count(t) == 0) {
                    st.insert(t);
                }
            } while (next_permutation(all(b)));
        }

        cout << st.size() << endl;

    }
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

int n;
int k;

vector<int> card;
set< string > cd;

string VectorToString(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
	}
	return str;
}

string VectorToDebugText(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
		str += ":";
	}
	return str;
}


int main(){
	while(1){
		int self;
		scanf("%d %d",&n,&k);
		if(n == 0 && k == 0){
			break;
		}
		card.clear();
		cd.clear();
		for(int i=0;i<n;i++){
			scanf("%d",&self);
			card.push_back(self);
		}
		vector<int> cc(k);
	
		sort(card.begin(),card.end());
	
		while(next_permutation(card.begin(),card.end())){
			 copy(card.begin(),card.begin()+k ,cc.begin());
			 cd.insert(VectorToString(cc));
		}
		printf("%d\n",cd.size());
	}
}

C++: #include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < n ; i++)

int main(){
    int n,K;
    
    while(cin >> n >> K , n + K){
        map<string,int> m;
        string str[11],in;
        rep(i,n){
            cin >> str[i];
        }
        int cnt = 0;
        if(K == 2){
            rep(i,n){
                rep(j,n){
                    if(i == j) continue;
                    in = str[i]+str[j];
                    if(!m[in]){
                        m[in]++;
                        cnt++;
                    }
                }
            }
        }
        else if(K == 3){
            rep(i,n){
                rep(j,n){
                    if(i == j) continue;
                    rep(k,n){
                        if(i == k ||j == k) continue;
                        in = str[i]+str[j]+str[k];
                        if(!m[in]){
                            m[in]++;
                            cnt++;
                        }
                    }
                }
            }
        }
        else{
            rep(i,n){
               rep(j,n){
                   if(i == j) continue;
                    rep(k,n){
                        if(i == k || j == k) continue;
                        rep(l,n){
                            if(i == l || j == l || k == l) continue;
                            in = str[i]+str[j]+str[k]+str[l];
                            if(!m[in]){
                                m[in]++;
                                cnt++;
                            }
                        }
                    }
                }
            }
        }
        cout << cnt << endl;
    }
    
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <set>
#include <string>
using namespace std;

int N, K;
set<int> result;
int card[16];
bool used[16];

void solve(int k, int ans) {
	if(k == K) {
		result.insert(ans);
		return;
	}
	for(int i = 0; i < N; i++) {
		if(used[i] == true) continue;
		used[i] = true;
		int t = card[i];
		int next;
		if(t < 10) {
			next = ans * 10;
		} else {
			next = ans * 100;
		}
		next += t;
		solve(k + 1, next);
		used[i] = false;
	}
}

int main() {
	for(;;) {
		result.clear();
		memset(used, 0, sizeof used);

		scanf("%d %d", &N, &K);
		if(N == 0 && K == 0)
			break;

		for(int i = 0; i < N; i++) {
			scanf(" %d", &card[i]);
		}

		solve(0, 0);
		printf("%d\n", result.size());
	}
	return 0;
}

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

int N, K;
set<int> result;
int card[16];
bool used[16];

void solve(int k, int ans) {
	if(k == K) {
		result.insert(ans);
		return;
	}
	for(int i = 0; i < N; i++) {
		if(used[i] == true) continue;
		used[i] = true;
		int t = card[i];
		int next;
		if(t < 10) {
			next = ans * 10;
		} else {
			next = ans * 100;
		}
		next += t;
		solve(k + 1, next);
		used[i] = false;
	}
}

int main() {
	for(;;) {
		result.clear();
		memset(used, 0, sizeof used);

		cin >> N >> K;
		if(N == 0 && K == 0)
			break;

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

		solve(0, 0);
		cout << result.size() << endl;
	}
	return 0;
}