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<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<set>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;


template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
	return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
	for (auto& e : t) fill_v(e, v);
}


#define ARRAY_MAX 100005
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;

int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };


struct Combination {

	vector<ll> fact, rfact;

	Combination(ll sz) : fact(sz + 1), rfact(sz + 1) {

		fact[0] = 1;
		for (ll i = 1; i < fact.size(); i++) {
			fact[i] = fact[i - 1] * i % MOD;
		}
		//逆元
		rfact[sz] = inv(fact[sz]);
		for (ll i = sz - 1; i >= 0; i--) {
			rfact[i] = rfact[i + 1] * (i + 1) % MOD;
		}
	}

	ll inv(ll x) {
		//modpow
		return pow(x, MOD - 2);
	}
	ll pow(ll x, ll n) {
		//累乗
		ll ret = 1;
		while (n > 0) {
			if (n & 1) (ret *= x) %= MOD;
			(x *= x) %= MOD;
			n >>= 1;
		}
		return (ret);
	}
	ll P(ll n, ll r) {
		//順列
		if (r < 0 || n < r) return (0);
		return (fact[n] * rfact[n - r] % MOD);
	}

	ll C(ll p, ll q) {
		//組み合わせ
		if (q < 0 || p < q) return (0);
		return (fact[p] * rfact[q] % MOD * rfact[p - q] % MOD);
	}

	ll H(ll n, ll r) {
		//重複組み合わせ
		if (n < 0 || r < 0) return (0);
		return (r == 0 ? 1 : C(n + r - 1, r));
	}
};


/******************************************************************************************/



int main() {

	int n, k;
	while (cin >> n >> k, n|k) {

		vector<string> a(n);
		set<string> st;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}

		sort(a.begin(), a.end());


		do {

			string tmp;
			for (int i = 0; i < k; i++)
			{
				tmp += a[i];
			}
			st.insert(tmp);
		} while (next_permutation(a.begin(), a.end()));

		cout << st.size() << endl;
		st.clear();
		a.shrink_to_fit();


	}



	return 0;
}

C++: /* AOJ 0546
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0546
 *
 */


//include
#include<bits/stdc++.h>

//using
using namespace std;

//define static const typedef
#define FOR(i, s, n) for(int (i) = (s); (i) < (n); (i)++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\n";
#define DEBUG(x) cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;

//global
set<string> a;
set<pair<string, VS> > e;
int n, k, ans;
VS str_array;
char b[2];
//function
void solve(int many_used, string now_string, VS can_use){
  if(e.count(pair<string, VS>(now_string, can_use))){
      return;
  }
  e.insert(pair<string, VS>(now_string, can_use));
  if(k == many_used && !a.count(now_string)){
    ans++;
    a.insert(now_string);
    //DEBUG(now_string)
    return;
  }
  REP(i, can_use.size()){
    string str = can_use[i];
    can_use.erase(can_use.begin() + i);
    solve(many_used + 1, now_string + str, can_use);
    can_use.PB(str);
    SORT(can_use);
  }
}
int main(){
  while(scanf("%d %d", &n, &k) && n && k){
    REP(i, n){
      scanf("%s", b);
      str_array.PB((string)b);
    }
    ans = 0;
    SORT(str_array);
    solve(0, "", str_array);
    printf("%d\n", ans);
    str_array = VS();
    a = set<string>();
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#define MAX 30
#include<string>
#include<vector>
using namespace std;
int main(){
	int n;
	while(cin>>n,n){
		int k;
		cin>>k;
		vector<string> vec;
		vector<int> ind;
		set<string> st;
		bool isDone[10001];
		for(int i=0;i<10001;i++){
			isDone[i] = false;
		}
		
		for(int i=0;i<n;i++){
			string s;
			cin>>s;
			vec.push_back(s);
			ind.push_back(i);
		}
		sort(vec.begin(),vec.end());
//		cout<<"---"<<endl;
		do{
			int hash = 0;
			for(int i=0;i<k;i++){
				hash = hash*10 + ind[i];
			}
			if(isDone[hash]) continue;
			isDone[hash] = true;
			string t="";
			for(int i=0;i<k;i++){
				t+=vec[ind[i]];
			}
			st.insert(t);
//			cout<<"------"<<endl;
		}while(next_permutation(ind.begin(),ind.end()));
		cout<<st.size()<<endl;
	}
	return 0;
}

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

const int MAXN = 10;
int N;
bool used[MAXN];
string A[MAXN];
set<int> vis;

void rec(string num, int k) {
  if(k == 0) {
    vis.insert(atoi(num.c_str()));
    return;
  }
  for(int i = 0; i < N; ++i) {
    if(used[i]) continue;
    used[i] = true;
    rec(num+A[i], k-1);
    used[i] = false;
  }
}

int main() {
  int k;
  while(cin >> N && N) {
    cin >> k;
    for(int i = 0; i < N; ++i) {
      cin >> A[i];
    }
    vis.clear();
    rec("", k);
    cout << vis.size() << endl;
  }
  return 0;
}