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 <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

int toInt(string s){
  int ret = 0;
  for(int i = 0 ; i < s.size() ; i++){
    ret *= 10;
    ret += s[i] -'0';
  }
  return ret;
}

int main(){
  int n, k;
  while(cin >> n >> k){
    if(n == 0 && k == 0) break;
    string s;
    vector<string> data;
    for(int i = 0 ; i < n ; i++){
      cin >> s;
      data.push_back(s);
    }
    sort(data.begin(), data.end());
    set<int> res;
    do{
      string t = "";
      for(int i = 0 ; i < k ; i++){
    t += data[i];
      }
      int tt = toInt(t);
      res.insert(tt);
    }while(next_permutation(data.begin(), data.end()));
     
    cout << res.size() << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int save[10000000], n, k, ans = 0, roop, num, save3, save4[10000000], roop2;
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;
		}
	}
}

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);
	REP(i,n){
	    cin >> card[i];
	}
	saiki(0,"");
	cout << ans.size() << endl;
	ans.clear();
	used.clear();
    }
    return 0;
}

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

using namespace std;

int main()
{
  int n, k;
  while(cin >> n >> k, n && k) {
    set<string> st;
    vector<string> card(n);
    for(string& s : card) cin >> s;
    sort(card.begin(), card.end());
    do {
      string t = "";
      for(int i = 0; i < k; i++) {
	t += card[i];
      }
      st.insert(t);      
    }while(next_permutation(card.begin(), card.end()));
    cout << st.size() << endl;
  }
  
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
map<string,bool> bmap;
int card[10];
int selected[4],temp[4];
bool used[10];
int n,k,num;
void solve(int m) {
	if(m==k) {
		for(int i=0;i<k;i++)temp[i]=selected[i];
		do {
			stringstream str;
			for(int i=0;i<k;i++)str<<selected[i];
			if(bmap.find(str.str())==bmap.end())num++;
			bmap[str.str()]=true;
		}while(next_permutation(selected,selected+k));
		for(int i=0;i<k;i++)selected[i]=temp[i];
	}else {
		for(int i=0;i<n;i++) {
			if(used[i]==false) {
				used[i]=true;
				selected[m]=card[i];
				solve(m+1); 
				used[i]=false;
			}
		}
	}
}
int main() {
	while(scanf("%d %d",&n,&k),n) {
		for(int i=0;i<n;i++)scanf("%d",&card[i]);
		fill(used,used+10,0);
		bmap.clear();
		num=0;
		solve(0);
		printf("%d\n",num);
	}
}