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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <numeric>
#include <iomanip>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, k;
string s;
vector<string> vs;
map<string, int> mp;
bool used[11];

void solve(int i,string t){
	if (i == k){
		mp[t]++;
		return;
	}

	REP(j, vs.size()){
		if (used[j] == 0){
			used[j] = 1;
			solve(i + 1, t + vs[j]);
			used[j] = 0;
		}
	}
}

int main(){
	while (cin >> n >> k&&n+k){
		fill(used, used + 11, 0);
		mp.clear();
		vs.clear();
		REP(i, n){
			cin >> s;
			vs.push_back(s);
		}
		solve(0, "");
		//cout << endl;
		//map<string, int>::iterator it = mp.begin();
		//while (it != mp.end()){
		//	cout << (*it).first << endl;
		//	it++;
		//}
		cout << mp.size() << endl;
	}
}

C++: #include <stdio.h>
#include <string.h>

int N, K;
char A[11][3];
char tmp[10], cards[211][10];
bool ans_flag = true;
int ans = 0;

int main(void) {
	while (true) {
		ans = 0;
		scanf("%d%d", &N, &K);
		if (N == 0 && K == 0) { break; }
		for (int i = 0; i < N; ++i) { scanf("%s", &A[i]); }

		if (K == 2) {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = i+1; j < N; ++j) {
					strcat(tmp, A[j]);
					for (int k = 0; k < ans; ++k) {
						if (strcmp(tmp, cards[k]) == 0) {
							ans_flag = false;
							break;
						}
					}
					if (ans_flag == true) {
						ans++;
						strcpy(cards[ans - 1], tmp);
					}
					ans_flag = true;
					memset(tmp, '\0', sizeof(tmp));
				}
			}
		}
		else if (K == 3) {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = i+1; j < N; ++j) {
					strcat(tmp, A[j]);
					for (int k = j+1; k < N; ++i) {
						strcat(tmp, A[k]);
						for (int l = 0; l < ans; ++l) {
							if (strcmp(tmp, cards[l]) == 0) { ans_flag = false; break; }
						}
						if (ans_flag == true) {
							ans++;
							strcpy(cards[ans - 1], tmp);
						}
						ans_flag = true;
						memset(tmp, '\0', sizeof(tmp));
					}
				}
			}
		}
		else {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = i+1; j < N; ++j) {
					strcat(tmp, A[j]);
					for (int k = j+1; k < N; ++i) {
						strcat(tmp, A[k]);
						for (int l = k+1; l < N; ++l) {
							strcat(tmp, A[l]);
							for (int m = 0; m < ans; ++m) {
								if (strcmp(tmp, cards[m]) == 0) { ans_flag = false; break; }
							}
						}
						if (ans_flag == true) {
							ans++;
							strcpy(cards[ans - 1], tmp);
						}
						ans_flag = true;
						memset(tmp, '\0', sizeof(tmp));
					}
				}
			}
		}

		printf("%d\n", ans);
	}
	return 0;
}

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

int n, k;
int a[10];

bool used[10] = {false};
int b[10];
set<int> dict;

void dfs(int id) {
	if (id == k) {
		int num = 0;
		for (int i = 0; i < k; i++) {
			if (b[i] >= 10) num *= 100;
			else num *= 10;
			num += b[i];
		}
		dict.insert(num);
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			used[i] = true;
			b[id] = a[i];
			dfs(id + 1);
			used[i] = false;
		}
	}
}

int main() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> a[i];
	dfs(0);
	cout << dict.size() << endl;
	return 0;
}

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

int main(){
  int n, k;
  while(cin >> n >> k){
    if(n == 0 && k == 0) break;
    vector<string> card;
    string t;
    for(int i = 0 ; i < n ; i++){
      cin >> t;
      card.push_back(t);
    }
    
    sort(card.begin(), card.end());

    set<string> res;
    
    do{
      string s = "";
      for(int i = 0 ; i < k ; i++){
        s += card[i];
      }
      res.insert(s);
    }while(next_permutation(card.begin(), card.end()));
      
    cout << res.size() << endl;
  }
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()

void solve(int n, int k) {
    vector<string> card(n);
    set<string> S;
    for(auto &i : card) cin >> i;

    sort(ALL(card));
    do {
        string t;
        rep(i,k) t += card[i];
        S.insert(t);
    } while(next_permutation(ALL(card)));

    cout << S.size() << "\n";

    return ;
}

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

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
 
signed main() {
    int n, k;
    while(1){
        cin >> n >> k;
        if(n + k == 0) break;
        vector<string> s(n);
        set<string> st;
        rep(i, 0, n){
            cin >> s[i];
        }
        sort(all(s));
        do{
            string tmp = "";
            rep(i, 0, k){
                tmp += s[i];
            }
            //cout << tmp <<endl;
            if(st.count(tmp)) continue;
            st.insert(tmp);
        }while(next_permutation(all(s)));
        cout << st.size() <<endl;
    }
}