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

int    N,K;
bool   v[10];
string s[10];
set<string> c;

void f(int k, string str)
{
	if(k==K)
	{
		c.insert(str);
		return;
	}

	for(int i=0; i<N; i++)
	{
		if(v[i]) continue;

		v[i]=1;
		f(k+1, str+s[i]);
		v[i]=0;
	}
}


int main()
{
	
	while(cin>>N>>K, N||K)
	{
		for(int i=0; i<N; i++)
		{
			cin>>s[i];
		}

		c.clear();
		memset(v, 0, sizeof(v));
		f(0, "");
		cout << c.size() << endl;
	}
}

C++: #include "stdio.h"
#include <vector>
#include <queue>
#include <set>

using namespace std;

int x[10];

int main()
{
while(1)
{
int n,k;
scanf("%d",&n);
scanf("%d",&k);
if(n == 0 && k == 0)
{
break;
}
for(int i = 0; i < n; i++)
{
scanf("%d",&x[i]);
}

int w = 1;
for(int i = 0; i < k; i++)
{
w *= n;
}

set<int> sw;
for(int i = 0; i < w; i++)
{
priority_queue<int> q1;
vector<int> q2;
int s = i;
for(int i = 0; i < k; i++)
{
int l = s % n;
q1.push(l);
q2.push_back(l);
s /= n;
}

int flgw = 0;
int mae = -1;
for(int ii = 0; ii < k; ii++)
{
if(mae == q1.top())
{
flgw = 1;
break;
}
mae = q1.top();
q1.pop();
}
if(flgw == 1)
{
continue;
}

int g = 0;
for(int ii = 0; ii < k; ii++)
{
if(x[q2[ii]] < 10)
{
g = g * 10 + x[q2[ii]];
}
else
{
g = g * 100 + x[q2[ii]];
}
}
sw.insert(g);
}

printf("%d\n",sw.size());
}
return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, k, c[10];
map<string, int>m;

void dfs(int cnt, string s, int v[]){
  if(cnt == k){
    if(m.count(s)) return;
    m[s] = 1;
    return;
  }

  rep(i, n){
    if(v[i]) continue;
    v[i] = 1;
    stringstream ss;
    ss << c[i];
    dfs(cnt + 1, s + ss.str(), v);
    v[i] = 0;
  }
}

int main(){
  while(scanf("%d%d", &n, &k) && n+k){
    rep(i, n) scanf("%d", c+i);
    m.clear();
    string s = "";
    int v[10] = {};
    dfs(0, s, v);
    printf("%d\n", (int)m.size());
  }
  return 0;
}

C++: #include "bits/stdc++.h"
 
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define all(co) co.begin(), co.end()
using namespace std;
//using P = pair<int, int>;
 
int main() {
    int n;
    while (cin >> n && n) {
        int k; cin >> k;
        vector<string> vec(n);
        rep(i, n) cin >> vec[i];
 
        set<string> sets;
        vector<bool> used(n);
        extern "C"{ void dfs (string str, int count) {
            if (count == k) {
                sets.insert(str);
                return;
            }
            rep(i, n) if (!used[i]) {
                used[i] = true;
                dfs(str + vec[i], count + 1);
                used[i] = false;
            }
        }}
        dfs("", 0);
        cout << sets.size() << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	int n;
	int k;
	while(cin >> n >> k && n && k){
	string card[10];
	set<string> ans;
	REP(i, n){
		cin >> card[i];
	}
	for(int i = 0;i < n;i++){
		string s = card[i];
		for(int j = 0;j < n;j++){
			if(j == i) continue;
			string t = s;
			t += card[j];
			for(int p = 0;p < n && 2 < k;p++){
				if(p == i || p == j) continue;
				string u = t;
				u += card[p];
				for(int q = 0;q < n && 3 < k;q++){
					string v = u;
					if(q == i || q == j || q == p) continue;
					v += card[q];
					ans.insert(v);
				}
				if(k == 3){
					ans.insert(u);
				}
			}
			if(k == 2){
				ans.insert(t);
			}
		}
	}
	cout << ans.size() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <numeric>
#include <algorithm>
using namespace std;


int main()
{
	while(true) {
		int N, K;
		cin >> N >> K;

		if(N == 0 && K == 0) break;

		vector<string> vs(N);
		for(int i = 0; i < N; i++) cin >> vs[i];

		sort(vs.begin(), vs.end());

		vector<string> res;
		do res.push_back(accumulate(vs.begin(), vs.begin() + K, string()));
		while (next_permutation(vs.begin(), vs.end()));

		sort(res.begin(), res.end());
		res.erase(unique(res.begin(), res.end()), res.end());

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