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++: /* 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;
int n, k, ans;
VS str_array;
char b[2];
//function
void solve(int many_used, VI used_card, string now_string){
  SORT(used_card);
  if(k == many_used && !a.count(now_string)){
    ans++;
    a.insert(now_string);
    //DEBUG(now_string)
    return;
  }
  REP(i, n){
    if(binary_search(ALL(used_card), i)){
      continue;
    }
    used_card.PB(i);
    solve(many_used + 1, used_card, now_string + str_array[i]);
    used_card.pop_back();
  }
}
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, VI(), "");
    printf("%d\n", ans);
    str_array = VS();
    a = set<string>();
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
string num[11];
int n,k,used[11];
set<string> S;
void dfs(int dep,string str){
  if(dep==k){S.insert(str);return; }

  for(int i=0;i<n;i++){
    if(used[i])continue;
    used[i]=1;
    dfs(dep+1,str+num[i]);
    used[i]=0;
  }
}

int main(){
  while(cin>>n,n){
    cin>>k;
    for(int i=0;i<n;i++)cin>>num[i];
  memset(used,0,sizeof(used));
  S.clear();
  dfs(0,"");
  cout <<S.size()<<endl;
  }
  return 0;
}

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

using namespace std;

set<string> s;
bool used[10];
string card[10];
int n,k,u[4];

int dfs(int c){
    if(c==k){
        string cb;
        for(int i=0;i<k;++i) cb +=card[u[i]];
        s.insert(cb);
        return 0;
    }
    for(int i=0;i<n;++i){
        if(!used[i]){
            used[i]=1;u[c]=i;
            dfs(c+1);
            used[i]=0;
        }
    }
}
int main(){
    while(cin>>n>>k,n){
        s.clear();
        for(int i=0;i<n;++i) cin>>card[i];
        memset(used,0,sizeof(used));
        dfs(0);
        cout<<s.size()<<endl;
    }
    return 0;
}

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

int n, k, c[10], d[10000];

int main() {
  while (scanf("%d%d", &n, &k), n) {
    for (int i = 0; i < n; i++) scanf("%d", &c[i]);
    int now[4], counter = 0;
    for (int i = 0; i < n; i++) {
      now[0] = c[i];
      for (int j = 0; j < n; j++) {
        if (i == j) continue;
        if (c[j] < 10) now[1] = now[0] * 10 + c[j];
        else now[1] = now[0] * 100 + c[j];
        if (k == 2) {
          d[counter++] = now[1];
          continue;
        }
        for (int x = 0; x < n; x++) {
          if (x == i || x == j) continue;
          if (c[x] < 10) now[2] = now[1] * 10 + c[x];
          else now[2] = now[1] * 100 + c[x];
          if (k == 3) {
            d[counter++] = now[2];
            continue;
          }
          for (int y = 0; y < n; y++) {
            if (y == i || y == j || y == x) continue;
            if (c[y] < 10) now[3] = now[2] * 10 + c[y];
            else now[3] = now[2] * 100 + c[y];
            d[counter++] = now[3];
          }
        }
      }
    }
    sort(d, d+counter);
    int ans = counter;
    for (int i = 0; i < counter - 1; i++) if (d[i] == d[i+1]) ans--;
    printf("%d\n", ans);
  }
}

C++: ??£???

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1<<29
#define MAX 11
using namespace std;

int a[MAX],se[MAX];

void solve(){
 int n,k;
 for(;;){
  cin>>n;
  cin>>k;
  if(!n&&!k) break;
  set<string> S;
  string ss;
  FOR(i,n) cin>>a[i];
  FOR(i,1<<n){
   int p=0;
   FOR(j,n) if((1<<j)&i) se[p++]=j;
   if(p!=k) continue;
   vector<int> ord(k);
   FOR(j,k) ord.pb(j);
   do{
    ss="";
    FOR(j,k) ss+=to_string(a[se[ord[j]]]);
    if(S.find(ss)==S.end()) S.insert(ss);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

int main(){
 solve();
 return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cstring>
 
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
 
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1<<29;

int popcnt(int x){
	int cnt = 0;
	while(x){
		x &= x-1;
		cnt++;
	}
	return cnt;
}

int main(){
    int n, k;
	while(cin >> n >> k, n|k){
		vector<string> v(n);
		set<string> s;
		rep(i, n) cin >> v[i];
		for(int i = 0; i < (1<<n); i++){
			if(popcnt(i) != k) continue;
			vector<string> sub;
			rep(j, n){
				if((i>>j)&1) sub.push_back(v[j]);
			}
			sort(all(sub));
			do{
				string x;
				rep(j, k){
					x += sub[j];
				}
				s.insert(x);
			}while(next_permutation(all(sub)));
		}
		cout << s.size() << endl;
	}
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, save4[1000000], roop2;
bool save[10000000];
string card[10], res, save2, save5[10000000];
void solve(int a){
	roop--;
	for (int i = 0; i<n; i++){
		if(i==a) continue;
		if (save3 != i){
			if (roop == 0){
				save2 = res;
				res += card[i];
				num = stoi(res);
				if (save[num] == 0){
					ans++;
				}
				save[num] = 1;
				save[roop2] = num;
				roop2++;
				res = save2;
			}
			else{
				save5[i]=res;
				res += card[i];
				solve(i);
				roop++;
				res = save5[i];
			}
		}
	}
}
int main() {
	while (true){
		cin >> n >> k;
		roop2 = 0;
		if (n == 0 && k == 0){
			return 0;
		}
		for (int i = 0; i < n; i++){
			cin >> card[i];
		}
		for (int i = 0; i < n; i++){
			roop = k - 1;
			res = card[i];
			save3 = i;
			solve(i);
		}
		cout << ans << endl;
		for (int i = 0; i < 10000000; i++){
			if (save[save4[i]] == 0)
				break;
			save[save4[i]] = 0;
		}
	}
}