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 <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} pr(a[a.size()-1]);}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

//nコからkコを取り出す。
//for(int bit = (1<<k)-1; bit < (1<<n); bit=next_combination(bit)){}
int next_combination(int sub) {
int x = sub & -sub, y = sub + x;
return (((sub & ~y) / x) >> 1) | y;
}

ll join(ll x, ll y){
    int dig = 1;
    ll div = 10;
    while(y/div != 0){
        dig++;
        div *= 10;
    }

    x *= div;
    return x+y;
}

int main()
{
    int n, k; 
    while(cin >> n >> k){
    if(n==0 && k==0) break;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    set<int> s;
    int w = 1;
    rep(i, k) w*=(i+1);

    for(int bit = (1<<k)-1; bit < (1<<n); bit=next_combination(bit)){
        vector<int> p(k);
        rep(i, k) p[i] = i;
        vector<int> b;
        rep(i, n){
            if(bit >> i & 1) {
                b.push_back(a[i]);
            }
        }
        // prvec(b);
        rep(wi, w){
            ll num = join(b[p[0]], b[p[1]]);
            FOR(i, 2, k) num = join(num, b[p[i]]);
            // pr(num);
            s.insert(num);
            next_permutation(ALL(p));
        }
    }

    pr(s.size());
    }
    return 0;}

C++: #include<iostream>
#include<vector>
#include<map>
 
using namespace std;
 
int n,k,ans;
vector<string> V;
map<string,int> M;
 
void init(){
  V.clear();
  M.clear();
  ans = 0;
}
 
void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  if(rev == k){
    if(M[s] == 0){
      M[s] = 1;
      ans++;
    }
    return;
  }
 
  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0)
      solve(s+V[i],rev+1,used+(1<<i));
   
}
 
int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

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

#define MAX_N 10

int n, k;
vector<int> cs;
set<string> ps;

string to_s(int r) {
	ostringstream os;
	os << r;
	return os.str();
}

bool used[MAX_N];
void enumerate(int k_) {
	if (k_ == k) {
		vector<string> part;
		for (int i = 0; i < n; i++) {
			if (used[i]) {
				part.push_back(to_s(cs[i]));
			}
		}
		sort(part.begin(), part.end());
		do {
			string p = "";
			for (int i = 0; i < k; i++) p.append(part[i]);
			ps.insert(p);
		} while (next_permutation(part.begin(), part.end()));
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			used[i] = true;
			enumerate(k_ + 1);
			used[i] = false;
		} 
	}
}

int main() {
	while (cin >> n >> k) {
		if (n == 0 && k == 0) break;
		cs.clear();
		cs.resize(n);
		ps.clear();
		for (int i = 0; i < MAX_N; i++) used[i] = false;
		for (int i = 0; i < n; i++) cin >> cs[i];
		enumerate(0);
		cout << ps.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);


bool ans[10000001];
int a[11];
int n, k;

void calc(int i, int j, int qqq, int l) {
	int num = a[i];
	if (a[j] >= 10)num *= 100;
	else num *= 10;
	num += a[j];
	if (3 <= k) {
		if (a[qqq] >= 10)num *= 100;
		else num *= 10;
		num += a[qqq];
		if (4 <= k) {
			if (a[l] >= 10)num *= 100;
			else num *= 10;
			num += a[l];
		}
	}
	//cout << num << endl;
	//system("pause");
	ans[num] = true;
}

int main() {
	while (cin >> n >> k&&n&&k) {
		for (int i = 0; i < 10000011; i++)ans[i] = false;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int qqq = 0; qqq < n; qqq++) {
					for (int l = 0; l < n; l++) {
						if (i == j)continue;
						if (i == qqq)continue;
						if (i == l)continue;
						if (j == qqq)continue;
						if (j == l)continue;
						if (qqq == l)continue;
						calc(i, j, qqq, l);
					}
				}
			}
		}
		int s = 0;
		for (int i = 0; i < 10000000; i++) {
			if (ans[i])s++;
		}
		cout << s << endl;
	}
}