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<set>
#include<algorithm>
using namespace std;
static const int MAX_N = 10;

int main(){
	for(;;){
		int n, k;
		string s[MAX_N];
		set<string> sa;
		cin >> n >> k;
		if(n == 0 && k == 0) break;
		for(int i = 0; i < n; i++) cin >> s[i];
		sort(s, s + n);
		do{
			string t = "";
			for(int i = 0; i < k; i++) t += s[i];
			sa.insert(t);
		}while(next_permutation(s, s + n));
		cout << sa.size() << endl;
	}
	return 0;
}

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


using namespace std ;


string s[ 10 ] ;

set< string > ans ;


int main( void )
{
	int n ,k ;
	while( 1 )
	{
		scanf( "%d %d" ,&n ,&k ) ;

		if( n | k )
		{
			for( int i = 0 ; i < n ; ++i )
			{
				cin >> s[ i ] ;
			}

			sort( s ,s + n ) ;

			do
			{
				string in = "" ;

				for( int i = 0 ; i < k ; ++i )
					in += s[ i ] ;

				ans.insert( in ) ;

			}while( next_permutation( s ,s + n ) ) ;


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

			ans.clear() ;
		}

		else
		{
			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

class Combination {
  int r;
  vector<int> data;

public:
  Combination(int n, int r) : r(r) {
    data.resize(n);
    fill(data.end() - (n - r), data.end(), 1);
  }

  bool next() { return next_permutation(data.begin(), data.end()); }

  vector<int> get() {
    vector<int> v;
    v.reserve(r);
    rep(i, data.size()) if ((*this)[i]) v.emplace_back(i);
    return v;
  }

  bool operator[](int i) { return !data[i]; }
};

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

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
 
int n, k;
set<string> st;
string s;
vi v;
 
void calc(int c, vi u){
    if(u.size() == k){
    	// rep(i, u.size()){
    	// 	cout<<" "<<u[i];
    	// }
    	// cout<<endl;
        do{
            string t;
            for(int i = 0; i < k; i++){
                t += to_string(u[i]);
            }
            st.insert(t);
        }while(next_permutation(all(u)));
        return;
    }
    for(int i = c; i<n; i++){
        u.pb(v[i]);
        calc(i+1, u);
        u.pop_back();
    }
}
 
 
int main(){
    while(cin>>n>>k){
        if(n == 0 && k == 0) break;
        v = vi(n);
        rep(i, n) cin>>v[i];
        s = "";
        st.clear();
        vi tmp;
        calc(0,tmp);
        // for(auto it = st.begin(); it != st.end(); it++){
        // 	cout<<" "<<*it<<endl;
        // }
        cout<<st.size()<<endl;
    }
}

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

template<class T> inline string toString(T x) {
	ostringstream sout;
	sout << x;
	return sout.str();
}

vector<int> nums;
set<int> used;
set<string> results;

void permutations(const string& str, int k) {
	if (k == 0) {
		results.insert(str);
		return;
	}
	for (unsigned i = 0; i < nums.size(); i++) {
		if (used.find(i) != used.end()) continue;
		used.insert(i);
		permutations(str + toString(nums[i]), k - 1);
		used.erase(i);
	}
}

int main() {
	int n, k;
	while (cin >> n >> k, n || k) {
		nums.clear();
		used.clear();
		results.clear();
		for (int i = 0; i < n; i++) {
			int x;
			cin >> x;
			nums.push_back(x);
		}
		permutations("", k);
		cout << results.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, n + k) {
		vector<int> card(n);
		for (int i = 0; i < n; ++i) cin >> card[i];
		sort(card.begin(), card.end());
		set<int> cnt;
		do {
			int tmp = 0;
			for (int i = 0; i < k; ++i)
				tmp = tmp * (card[i] >= 10 ? 100 : 10) + card[i];
			cnt.insert(tmp);
		} while (next_permutation(card.begin(), card.end()));
		cout << cnt.size() << endl;
	}

	return 0;
}