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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int n;
	while(cin >> n, n) {
		int k;
		cin >> k;
		vs cards(n);
		REP(i, n) {
			cin >> cards[i];
		}

		map<string, int> num;
		sort(ALL(cards));
		do {
			string s;
			REP(i, k) {
				s += cards[i];
			}
			num[s]++;
		} while(next_permutation(ALL(cards)));

		cout << num.size() << endl;
	}
}

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

int count(int n)
{
int ret = 1;
for(int mul = 10; mul <= n; mul *= 10) ++ret; 
return ret;
}

void add(int k, int sum, vector<int> &c, vector<int> &result)
{
if(k == 0) {
result.push_back(sum);
return;
}
for(int i = pos + 1; i < c.size(); ++i) {
if(c[i] == 0)
continue;
int tmp = c[i];
c[i] = 0;
add(k - 1, sum * count(tmp) + tmp, c, result);
c[i] = tmp;
}
return;
}

int main()
{
while(true) {
int n, k;
vector<int> c;
vector<int> result;
scanf("%d%d", &n, &k);
if(n == 0)
break;
c.resize(n);
for(int i = 0; i < n; ++i) 
scanf("%d", &c[i]);
count(k, 0, c, result);
sort(result.begin(), result.end());
result.remove(unique(result.begin(), result.end()), result.end());
printf("%d\n", result.size());
}
return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<sstream>
#include<cstdlib>

using namespace std;

int main(){
	while(1){
		int n,k;
		scanf("%d%d",&n,&k);
		
		if(n==0)
			break;
		
		vector<int> card(n);
		for(int i=0;i<n;i++){
			scanf("%d",&card[i]);
		}
		
		sort(card.begin(),card.end());
		
		vector<int> data;
		do{
			
			stringstream sst;
			for(int i=0;i<k;i++){
				sst<<card[i];
			}
			data.push_back(atoi(sst.str().begin()));
			
			
			reverse( card.begin()+k,card.end());
		}while( next_permutation(card.begin(),card.end()) );
		
		sort(data.begin(),data.end());
		
		
		printf("%d\n",distance(data.begin(),unique(data.begin(),data.end())) );
	}
}

C++: #import<iostream>
#import<set>
using namespace std;int C[10],n,k,j;set<int>V;int S(int b){int i,c,v;k--;for(i=n;i--;(c=C[i])?k?C[i]=0,S(v),C[i]=c:(V.insert(v),0):0)v=b*(C[i]>9?100:10)+C[i];k++;}main(){for(;cin>>n>>k,n;S(0),cout<<V.size()<<endl,V.clear())for(j=n;j--;)cin>>C[j];}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int N,K;
        string str[11];
        string lis[10021];
        cin>>N>>K;
        if(N==0&&K==0)break;
        for(int i=0;i<N;i++)cin>>str[i];
        int ii=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                for(int k=0;k<N;k++){
                    for(int l=0;l<N;l++){
                        if(i!=j&&j!=k&&k!=l&&i!=k&&j!=l&&i!=l){
                            if(K==2)lis[ii]=str[i]+str[j];
                            if(K==3)lis[ii]=str[i]+str[j]+str[k];
                            if(K==4)lis[ii]=str[i]+str[j]+str[k]+str[l];
                            ii++;
                        }
                    }
                }
            }
        }
        sort(lis,lis+10012);
        int ans=0;
        for(int i=0;i<10015;i++)if(lis[i]!=lis[i+1])ans++;
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>    
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<queue>
#include<tuple>
#include<cassert>
#define int long long
using namespace std;
signed main() {
	for(int d=0;d<10;d++){
	int n, k, a = 0, sum = 0; string s; 
	cin >> n >> k;
string aa[213456], test[213456];
	if(n==0&&k==0){return 0;}
	for (int h = 0; h < n; h++) {
		cin >> s; aa[h] = s;
	}
	if (k == 2) {
		for (int h = 0; h < n; h++) {
			for (int i = h + 1; i < n; i++) {
				test[a] = aa[h] + aa[i]; a++;
				test[a] = aa[i] + aa[h]; a++;
			}
		}
	}
	if (k == 3) {
		for (int h = 0; h < n; h++) {
			for (int i = h + 1; i < n; i++) {
				for (int j = i + 1; j < n; j++) {
					test[a] = aa[h] + aa[i] + aa[j]; a++;
					test[a] = aa[h] + aa[j] + aa[i]; a++;
					test[a] = aa[i] + aa[h] + aa[j]; a++;
					test[a] = aa[i] + aa[j] + aa[h]; a++;
					test[a] = aa[j] + aa[h] + aa[i]; a++;
					test[a] = aa[j] + aa[i] + aa[h]; a++;
				}
			}
		}
	}
	if (k == 4) {
		for (int h = 0; h < n; h++) {
			for (int i = h + 1; i < n; i++) {
				for (int j = i + 1; j < n; j++) {
					for (int k = j + 1; k < n; k++) {
						test[a] = aa[h] + aa[i] + aa[j] + aa[k]; a++;
						test[a] = aa[h] + aa[i] + aa[k] + aa[j]; a++;
						test[a] = aa[h] + aa[j] + aa[i] + aa[k]; a++;
						test[a] = aa[h] + aa[j] + aa[k] + aa[i]; a++;
						test[a] = aa[h] + aa[k] + aa[i] + aa[j]; a++;
						test[a] = aa[h] + aa[k] + aa[j] + aa[i]; a++;
						test[a] = aa[i] + aa[h] + aa[j] + aa[k]; a++;
						test[a] = aa[i] + aa[h] + aa[k] + aa[j]; a++;
						test[a] = aa[i] + aa[j] + aa[h] + aa[k]; a++;
						test[a] = aa[i] + aa[j] + aa[k] + aa[h]; a++;
						test[a] = aa[i] + aa[k] + aa[h] + aa[j]; a++;
						test[a] = aa[i] + aa[k] + aa[j] + aa[h]; a++;
						test[a] = aa[j] + aa[h] + aa[i] + aa[k]; a++;
						test[a] = aa[j] + aa[h] + aa[k] + aa[i]; a++;
						test[a] = aa[j] + aa[i] + aa[h] + aa[k]; a++;
						test[a] = aa[j] + aa[i] + aa[k] + aa[h]; a++;
						test[a] = aa[j] + aa[k] + aa[h] + aa[i]; a++;
						test[a] = aa[j] + aa[k] + aa[i] + aa[h]; a++;
						test[a] = aa[k] + aa[h] + aa[i] + aa[j]; a++;
						test[a] = aa[k] + aa[h] + aa[j] + aa[i]; a++;
						test[a] = aa[k] + aa[i] + aa[h] + aa[j]; a++;
						test[a] = aa[k] + aa[i] + aa[j] + aa[h]; a++;
						test[a] = aa[k] + aa[j] + aa[h] + aa[i]; a++;
						test[a] = aa[k] + aa[j] + aa[i] + aa[h]; a++;
					}
				}
			}
		}
	}
	sort(test, test + a);
	for (int h = 1; h < a; h++) {
		if (test[h - 1] == test[h]) {
			sum++;
		}
	}
	cout << a - sum << endl;
	}
}