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>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(obj) obj.begin(), obj.end()
#define fcout cout << setprecision(10)

const int iINF = 1e9;
const long long llINF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

template <typename T>
void debug(vector<T> &vec) {
    for (auto x : vec) {
        cout << x << " ";
    }
    cout << endl;
    return;
}

template<typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

template <typename T>
int getFormer(const vector<T> &vec, T x) {
    return upper_bound(vec.begin(), vec.end(), x) - vec.begin() - 1;
}

template <typename T>
int getLatter(const vector<T> &vec, T x) {
    return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

vector<string> makeInt(vector<int> vec) {
    vector<string> res;
    sort(ALL(vec));
    do {
        string s = "";
        for (auto x : vec) {
            s += to_string(x);
        }
        res.push_back(s);
    } while (next_permutation(ALL(vec)));

    return res;
}

int solve(int n, int k) {
    vector<int> a(n);
    REP(i, n) { cin >> a[i]; }
    set<string> st;
    REP(S, 1 << n) {
        if (k != __builtin_popcount(S)) continue;
        vector<int> vec;
        REP(i, n) {
            if ((S >> i) & 1) {
                vec.push_back(a[i]);
            }
        }
        vector<string> strs = makeInt(vec);
        for (auto x : strs) {
            st.insert(x);
        }
    }
    cout << st.size() << endl;
}

int main() {
    int n, k;
    while(cin >> n >> k && n) {
        solve(n, k);
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
vector<string>in;
vi visit;
set<string>out;
int n,m;
void f(int b){
	if(b==m){
		string s="";
		rep(i,m)rep(j,n)if(visit[j]==i)s+=in[j];
		out.insert(s);
	}else{
		rep(i,n)if(visit[i]==inf){
			visit[i]=b;
			f(b+1);
			visit[i]=inf;
		}
	}
}
int main(){
	while(cin>>n,n){
		cin>>m;
		in=vector<string>(n);
		rep(i,n)cin>>in[i];
		out.erase(all(out));
		visit=vi(n,inf);
		f(0);
		cout<<out.size()<<endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <queue>
#include <set>

#define rep(i,n) for(int i=0; i<(n); i++)

using namespace std;

set<string> s;
int trace[4];
bool used[10];

void dfs(int depth, int n, int k, vector<int>& num) {
    if (depth == k) {
        stringstream ss;
        rep(i,k)ss << num[trace[i]];
        s.insert(ss.str());
    } else {
        rep(i,n) {
            if (used[i])continue;
            used[i] = true;
            trace[depth] = i;
            dfs(depth + 1, n, k, num);
            used[i] = false;
        }
    }
}

int main() {
    int n,k;
    cin >> n;
    cin >> k;
    vector<int> num(n);
    rep(i,n) cin >> num[i];
    dfs(0, n, k, num);
    cout << s.size() << endl;
    return 0;
}

C++: // clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

main() {
  int n, k;
  while (cin >> n >> k, n) {
    vector<string> a(n);
    cin >> a;
    set<string> s;
    sort(all(a));
    do {
      string t;
      rep(i, k) t += a[i];
      s.emplace(t);
    } while (next_permutation(all(a)));
    cout << s.size() << endl;
  }
}

C++: #include<iostream>
#include<string>
#include<bitset>
#include<map>
using namespace std;
int N, K;

string cards[100];
map<string, int> mem;

int dfs(int k, int m, bitset<100> c, string s)
{
  if(k == K)
  {
    if(mem[s] == 1)
      return 0;
    mem[s] = 1;
    return 1;
  }
  if(m != -1)
    c[m] = false;
  int ans = 0;
  for(int i = 0 ;i < N; i++)
  {
    if(!c[i])
      continue;
    ans += dfs(k + 1, i, c, s + cards[i]);
  }
  return ans;
}

int main()
{
  while(1)
  {
    mem.clear();
    cin >> N >> K;
    if(!N && !K)
      break;
    for(int i = 0; i < N; i++)
      cin >> cards[i];
    cout << dfs(0, -1, bitset<100>(0xFF), "") << endl;
  }
}