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 <string>
#include <map>
#include <sstream>
#include <algorithm>

using namespace std;

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

int CountBit(int i){
	int ret = 0;
	while(i > 0){
		if(i&1){
			ret++;
		}
		i = i >> 1;
	}
	return ret;
}

int main(void){
	int n, k;
	
	while(cin >> n){
		cin >> k;
		if((n|k) == 0) break;
		vector<int> cards;
		vector<int> candidates;
		int tmp;
		int ret = 0;
		string tmpstr;
		map<string, bool> m;
		for(int i=0;i<n;i++){
			cin >> tmp;
			cards.push_back(tmp);
		}
		for(int i=0;i < (1 << n); i++){
			if(CountBit(i) == k){
				//add candidates
				for(int j=0;j<n;j++){
					if(i & (1 << j)){
						candidates.push_back(cards[j]);
					}
				}
				//sort them
				sort(candidates.begin(), candidates.end());
				/*
				//begin region test print
				for(int j=0;j<candidates.size();j++) cout << candidates[j] << "  ";
				cout << endl;
				//end region test print
				*/
				do{
					tmpstr = "";
					for(int j=0;j<k;j++){
						tmpstr += toString(candidates[j]);
					}
					m[tmpstr] = true;
				}while(next_permutation(candidates.begin(), candidates.end()));
				
				candidates.clear();
			}
		}
		
		map<string,bool>::iterator it = m.begin();
		for(;it!=m.end();++it){
			if((*it).second == true) ret++;
		}
		cout << ret << endl;
	}
}

C++: #include<iostream>
#include<map>
using namespace std;
map < string , bool > used;

int n,k,ans;
string t[10];
bool u[10];

void dfs(int depth,string str){
  if(depth==k){
    if(!used[str])ans++;
    used[str]=true;
    return;
  }
  for(int i=0;i<n;i++){
    if(u[i])continue;
    u[i]=true;
    dfs(depth+1,str+t[i]);
    u[i]=false;
  }
}

int main(){
  while(cin>>n>>k&&n&&k){
    for(int i=0;i<n;i++)u[i]=false;
    ans=0;
    used.clear();
    for(int i=0;i<n;i++)cin>>t[i];
    dfs(0,"");
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<set>
#include<cstring>

using namespace std;

set<int> se;

int N,K;

int card[10];
bool used[10];

void rec(int num,int k)
{
	if(k==K)
	{
		se.insert(num);
		return;
	}
	for(int i=0;i<N;i++)
	{
		if(used[i]==true) continue;
		used[i]=true;
		int nnum;
		if(card[i]<10) nnum=num*10+card[i];
		else nnum=num*100+card[i];
		rec(nnum,k+1);
		used[i]=false;
	}
}

int main()
{
	while(true)
	{
	scanf("%d%d",&N,&K);
	se.clear();
	for(int i=0;i<N;i++)
	{
		scanf("%d",card+i);
	}
	memset(used,false,sizeof(used));
	rec(0,0);
	printf("%d\n",se.size());
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#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 reps(i,f,n) for(int i=(f);i<(n);i++)

int main(void){
	while(1){
		int n, k;
		scanf("%d %d", &n, &k);
		if(n == 0 && k == 0) return 0;
		vector<string> data(n);
		set<string> num;
		rep(i, n) scanf("%d", &data[i]);
		
		sort(data.begin(), data.end());

		do{
			string tmp;
			rep(i, k){
				tmp += data[i]
			}
			num.insert(tmp);
		}while(next_permutation(data.begin(), data.second()));
		printf("%d\n", num.size());
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int n, k;
set<string> ans;

void loop(string str, vector<string> s, vector<bool> used, int cnt){
    if(cnt >= k){
        ans.insert(str);
    } else{
        REP(i, used.size()){
            if(used[i] == false){
                used[i] = true;
                loop(str + s[i], s, used, cnt + 1);
                used[i] = false;
            }
        }
    }
}


int main(){
    while(cin >>n >>k && (n || k) ){
        ans.clear();
        vector<string> s(n);
        vector<bool> used(n, false);
        REP(i, n) cin >>s[i];

        loop("", s, used, 0);
        cout <<ans.size() <<endl;
    }

    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>

using namespace std;

typedef complex<double> xy_t;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF= 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;

int nums[10];
bool used[10];
int n, k;

map<int, bool> mp;
int pow(int n){
	int res = 1;
	while(n--) res *= 10;
	return res;
}

void dfs(int c, int num){
	if(c == k){
		mp[num] = true;
	}else{
		for(int i = 0; i < n; i++){
			if(!used[i]){
				int d = 1;
				if(nums[i] >= 10) d++;
				used[i] = true;
				dfs(c + 1, num * pow(d) + nums[i]);
				used[i] = false;
			}
		}
	}
}

int main(){
	while(cin >> n >> k && (n || k)){
		mp.clear();
		for(int i = 0; i < n; i++){
			cin >> nums[i];
		}
		fill(used, used + 10, false);
		dfs(0, 0);
		cout << mp.size() << endl;
	}
}