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 <stdio.h>
#include <string.h>

int N, K;
char A[11][3];
char tmp[10], cards[211][10], ok[211][10];
bool ans_flag = true, ok_flag = true;
int ans = 0;

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

		if (K == 2) {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = 0; j < N; ++j) {
					if (i != j) {
						strcat(tmp, A[j]);
						strcpy(cards[ans], tmp);
						ans++;
						int str_long = strlen(tmp) - 1;
						for (int z = 0; z < strlen(A[j]); ++z){
							tmp[str_long - z] = '\0';
						}
					}
				}
				memset(tmp, '\0', sizeof(tmp));
			}
		}
		else if (K == 3) {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = 0; j < N; ++j) {
					if (i != j) {
						strcat(tmp, A[j]);
						for (int k = 0; k < N; ++k) {
							if (j != k) {
								strcat(tmp, A[k]);
								strcpy(cards[ans], tmp);
								ans++;
								memset(tmp, '\0', sizeof(tmp));
							}
						}
					}
				}
			}
		}
		else {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = 0; j < N; ++j) {
					if (i != j) {
						strcat(tmp, A[j]);
						for (int k = 0; k < N; ++k) {
							if (j != k) {
								strcat(tmp, A[k]);
								for (int l = 0; l < N; ++l) {
									if (k != l) {
										strcat(tmp, A[j]);
										strcpy(cards[ans], tmp);
										ans++;
										memset(tmp, '\0', sizeof(tmp));
									}
								}
							}
						}
					}
				}
			}
		}

		int f = ans;
		ans = 0;
		for (int i = 0; i < f; ++i) {
			for (int j = 0; j < ans; ++j) {
				if (strcmp(cards[i], ok[j]) == 0) {
					ok_flag = false;
					break;
				}
			}
			if (ok_flag == true){ strcpy(ok[ans], cards[i]); ans++; }
			ok_flag = true;
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <unordered_set>
#include <functional>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
_T _DEF(T,in,int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_DEF(int,in,){return in<int>(gcu());}
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &n){int c=gcu();return c==EOF?n=0,false:(n=in<T>(c),true);}
_SCAN(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_SCAN(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}}
#endif
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{
	int e,b=0,s=1; range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it { int v, s; it (int _v, int _s) : v(_v), s(_s) {} operator int()const{return v;} operator int&(){return v;} int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin() {return {b, s};} it end() {return {e, s};}};

int main() {
	for (int n; (n = in());) {
		int k = in();
		vector<int> c(n);
		for (int &i: c)
			i = in();
		unordered_set<int> s;
		function<void(int, int, int)> check = [&](int i, int x, int v){
			if (i == k) {
				s.insert(x);
				return;
			}
			for (int j: range(n)) {
				if (!(v & (1 << j))) {
					int t {x};
					for (int k {c[j]}; k; k /= 10)
						t = t * 10 + k % 10;
					check(i + 1, t, v | (1 << j));
				}
			}
		};
		check(0, 0, 0);
		outl((int) s.size());
	}
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int n,k,a[10],power[5];
vector<string>vec;
int main(){
	while(true){
		cin>>n>>k;vec.clear();power[0]=1;for(int i=1;i<5;i++)power[i]=power[i-1]*n;
		if(n==0 && k==0)break;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<power[k];i++){
			int bit[4];for(int j=0;j<k;j++)bit[j]=(i/power[j])%n;
			bool OK=true;
			if(k==2 && (bit[0]==bit[1]))OK=false;
			if(k==3 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==4 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==3 && (bit[0]==bit[3] || bit[1]==bit[3] || bit[2]==bit[3]))OK=false;
			if(OK==true){
				string S="";
				for(int j=0;j<k;j++)S+=to_string(a[bit[j]]);
				vec.push_back(S);
			}
		}
		sort(vec.begin(),vec.end());int cnt=1;
		for(int i=1;i<vec.size();i++){
			if(vec[i-1]!=vec[i])cnt++;
		}
		cout<<cnt<<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);
	if(N==0&&K==0) break;
	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 <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){
   if(__builtin_popcount(i)!=k) continue;
   int p=0;
   FOR(j,n) if((1<<j)&i) se[p++]=j;
   vector<int> ord(k);
   FOR(j,k) ord.pb(j);
   do{
    ss="";
    FOR(j,k) ss+=to_string(a[se[ord[j]]]);
    S.insert(ss);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

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