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 int long long

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> plli;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#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);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()

const ll mod = 1e9 + 7;
const ll INF = 1<<30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, k;
bool used[15] = {};
vector<int> ret;


vector<vector<int>> permutation(int n, vector<vector<int>>& per) {
    for(int i = 1; i <= n; i++) {
        if (used[i]) continue;
        used[i] = true;
        ret.push_back(i);
        permutation(n, per);
        ret.pop_back();
        used[i] = false;
    }
    if (ret.size() == n) per.push_back(ret);
    return per;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<vector<int>> per;
    while (true) {
        cin >> n >> k;
        if (n == 0) break;
        int card[n] = {};
        rep(i, n) cin >> card[i];
        set<string> st;
        rep(i, 1 << n) {
            int cnt = 0;
            vector<int> idx;
            rep(j, n) if (i>>j & 1) {
                cnt++;
                idx.push_back(j);
            }
            if (cnt != k) continue;
            vector<vector<int>> per;
            per = permutation(k, per);
            rep(j, per.size()){
                string tmp = "";
                rep(l, k) {
                    tmp += to_string(card[idx[per[j][l] - 1]]);
                }
                st.insert(tmp);
            }
        }
        cout << st.size() << endl;
    }
}

C++: #import<iostream>
#import<set>
using namespace std;int C[10],n,k,j;set<int>V;int S(int b){int i,c,v;k--;for(i=n;i--;(c=C[i])?!k?V.insert(v),0:C[i]=0,S(v),C[i]=c:0)v=b*(C[i]>9?100:10)+C[i];k++;}main(){for(;cin>>n>>k,n;S(0),cout<<V.size()<<endl,V.clear())for(j=n;j--;)cin>>C[j];}

C++: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int n, k;
vector <int> a;
set <int> st;

void rec(int c = 0, int s = 0, int v = 0, string num = "") {
    if (c == k) {
        st.insert(stoi(num.c_str()));
        return;
    }

    rep (i, n) {
        if ((s >> i) & 1) continue;
        rec(c + 1, s | (1 << i), i, num + to_string(a[i]));
    }

}

int main() {
    while(cin >> n, n) {
        cin >> k;
        a = vector <int> (n);
        rep (i, n) cin >> a[i];
        st.clear();
        rec();
        cout << st.size() << endl;
    }
    return 0;
}

C++: #include <bitset>
#include <cstring>
#include <iostream>
#include <set>

using namespace std;

int n, k;
int cards[10];
bitset<10> used;
int permutation[4];
set<int> integers;

void rec(int i) {
  if (i < k) {
    for (int j = 0; j < n; j++) if (!used[j]) {
      permutation[i] = j;
      used[j] = 1;
      rec(i+1);
      used[j] = 0;
    }
  } else {
    int integer = 0;
    for (int j = 0; j < k; j++) {
      int x = cards[permutation[j]];
      integer *= (x >= 10 ? 100 : 10);
      integer += x;
    }
    integers.insert(integer);
  }
}

int main() {
  for (;;) {
    memset(cards, 0, sizeof cards);
    used.reset();
    memset(permutation, 0, sizeof permutation);
    integers.clear();
    cin >> n >> k;
    if (!n && !k) return 0;
    for (int i = 0; i < n; i++)
      cin >> cards[i];
    rec(0);
    cout << integers.size() << endl;
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>

using namespace std;

typedef unsigned uint;
typedef long long Int;

const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;

template<typename T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<typename T> void chmin(T& a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T& a, T b) { if (a < b) a = b; }

int main()
{
	int n, k;
	while (cin >> n >> k, n && k){
		string card[20];
		vector <string> a;
		for (int i = 0; i < n; i++){
			cin >> card[i];
		}
		if (k == 2){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					if (i != j) {
						// cout << i << " " << j << endl;
						a.push_back(card[i] + card[j]);
					}
				}
			}
		}
		if (k == 3){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					for (int k = 0; k < n; k++){
						if (i != j && j != k && i != k) a.push_back(card[i] + card[j] + card[k]);
					}
				}
			}
		}
		if (k == 4){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					for (int k = 0; k < n; k++){
						for (int l = 0; l < n; l++){
							if (i != j && i != k && i != l && j != k && j != l && k != l) a.push_back(card[i] + card[j] + card[k] + card[l]);
						}
					}
				}
			}
		}
		a.push_back("9999999999");
		sort(a.begin(), a.end());
		int ret = 0;
		for (int i = 0; i < a.size()-1; i++){
			if (a[i] != a[i + 1]) ret++;
		}
		// puts("s");
		cout << ret << endl;
	}

    return 0;
}

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

using namespace std;

int main(){
    int n,k;
    string card[10];
    while(cin>>n>>k,n){
        set<string> s;
        for(int i=0;i<n;++i) cin>>card[i];
        sort(card,card+n);
        do{
            string cb;
            for(int i=0;i<k;++i) cb +=card[i];
            s.insert(cb);
        }while(next_permutation(card,card+n));
        cout<<s.size()<<endl;
    }
    return 0;
}