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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

vector<string> card;
set<int> used;
set<int> ans;
int n,k;

void saiki(int t,string s){
    if(t == k){
	ans.insert(stoi(s));
    }
    REP(i,n){
	if(used.find(i) == used.end()){
	    used.insert(i);
	    saiki(t+1,s+card[i]);
	    used.erase(i);
	}
    }
}

int main(){
    while(true){
	cin >> n >> k;
	if(n==0&&k==0) break;
	card = vector<string>(n);
	memset(c,false,sizeof(c));
	REP(i,n){
	    cin >> card[i];
	}
	saiki(0,"");
	cout << ans.size() << endl;
	ans.clear();
	used.clear();
    }
    return 0;
}

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

int N, K;
char A[11][10];
int used[11];
char num[100];
int size, arr[6000];
int ans = 0;

void rec(int k) {
	int length = strlen(num);
	if (k == K) {
		arr[size++] = atoi(num);
		return;
	}
	for (int i = 0; i < N; ++i) {
		if (!used[i]) {
			used[i] = 1;
			strcat(num, A[i]);
			rec(k + 1);
			used[i] = 0;
			num[length] = '\0';
		}
	}
}

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

		memset(used, 0, sizeof(used));
		strcpy(num, "");
		size = 0;
		rec(0);
		std::sort(arr, arr + size);
		for (int i = 0; i < size; ++i) {
			if (i == 0 || arr[i - 1] != arr[i]) { ans++; }
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, roop2, card[10], res, save2,save5[10000000];
bool save[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;
				if(card[i]/10==1){
					res = res*100;
				}
				else{
					res = res*10;
				}
				res += card[i];
				if (save[res] == 0){
					ans++;
					save[res]=1;
				}
				res = save2;
			}
			else{
				save5[i]=res;
				if(card[i]/10==1){
					res = res*100;
				}
				else{
					res = res*10;
				}
				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++){
			save[i] = 0;
		}
	}
}

C++: #include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    while(cin >> n >> k, n or k) {
        vector<string> a(n);
        for(int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a.begin(), a.end());
        set<string> ans;
        do {
            string s;
            for(int i = 0; i < k; i++) {
                s += a[i];
            }
            ans.insert(s);
        } while(next_permutation(a.begin(), a.end()));
        cout << ans.size() << endl;
    }
    return 0;
}

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

int n, k;
string t[12];
bool used[12];
set<string> ans;

string itos(int x){
  stringstream ss;
  ss << x;
  return ss.str();
}

void dfs(int cnt, string s){
  if(cnt == k){
    ans.insert(s);
    return;
  }

  for(int i = 0; i < n; i++){
    if(used[i]) continue;
    used[i] = true;
    dfs(cnt + 1, s + t[i]);
    used[i] = false;
  }
}

int main(){
  while(cin >> n >> k, n || k){
    for(int i = 0; i < n; i++){
      int x;
      cin >> x;
      t[i] = itos(x);
    }
    ans.clear();
    memset(used, 0, sizeof(used));
    dfs(0, "");
    cout << ans.size() << endl;
  }
}

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],s[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){
   if(__builtin_popcount(i)!=k) continue;
   int p=0;
   FOR(j,n) if((1<<j)&i) s[p++]=j;
   vector<int> ord(k);
   FOR(j,k) ord.pb(j);
   do{
    ss="";
    FOR(j,k) s+=to_string(a[s[ord[j]]]);
    S.insert(ss);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

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