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 <bits/stdc++.h>
using namespace std;

int main() {

    int n,k;
    cin >> n >> k;

    string a[n];
    string s;

    for(int i=0;i<n;i++){
        cin >> a[i];
    }

    set<string> se;

    if(k==4) {
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int x = j + 1; x < n; x++) {
                    for (int y = x + 1; y < n; y++) {
                        s = a[i] + a[j] + a[x] + a[y];
                        se.insert(s);
                        s = a[i] + a[j] + a[y] + a[x];
                        se.insert(s);
                        s = a[i] + a[x] + a[j] + a[y];
                        se.insert(s);
                        s = a[i] + a[x] + a[y] + a[j];
                        se.insert(s);
                        s = a[i] + a[y] + a[j] + a[x];
                        se.insert(s);
                        s = a[i] + a[y] + a[x] + a[j];
                        se.insert(s);

                        s = a[j] + a[i] + a[x] + a[y];
                        se.insert(s);
                        s = a[j] + a[i] + a[y] + a[x];
                        se.insert(s);
                        s = a[j] + a[x] + a[i] + a[y];
                        se.insert(s);
                        s = a[j] + a[x] + a[y] + a[i];
                        se.insert(s);
                        s = a[j] + a[y] + a[i] + a[x];
                        se.insert(s);
                        s = a[j] + a[y] + a[x] + a[i];
                        se.insert(s);

                        s = a[x] + a[j] + a[i] + a[y];
                        se.insert(s);
                        s = a[x] + a[j] + a[y] + a[i];
                        se.insert(s);
                        s = a[x] + a[i] + a[j] + a[y];
                        se.insert(s);
                        s = a[x] + a[i] + a[y] + a[j];
                        se.insert(s);
                        s = a[x] + a[y] + a[j] + a[i];
                        se.insert(s);
                        s = a[x] + a[y] + a[i] + a[j];
                        se.insert(s);

                        s = a[y] + a[j] + a[x] + a[i];
                        se.insert(s);
                        s = a[y] + a[j] + a[i] + a[x];
                        se.insert(s);
                        s = a[y] + a[x] + a[j] + a[i];
                        se.insert(s);
                        s = a[y] + a[x] + a[i] + a[j];
                        se.insert(s);
                        s = a[y] + a[i] + a[j] + a[x];
                        se.insert(s);
                        s = a[y] + a[i] + a[x] + a[j];
                        se.insert(s);
                    }
                }
            }
        }
    }
    else if(k==3) {
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int x = j + 1; x < n; x++) {
                    s = a[i] + a[j] + a[x];
                    se.insert(s);
                    s = a[i] + a[x] + a[j];
                    se.insert(s);
                    s = a[j] + a[i] + a[x];
                    se.insert(s);
                    s = a[j] + a[x] + a[i];
                    se.insert(s);
                    s = a[x] + a[i] + a[j];
                    se.insert(s);
                    s = a[x] + a[j] + a[i];
                    se.insert(s);
                }
            }
        }
    }
    else{
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                s = a[i] + a[j];
                se.insert(s);
                s = a[j] + a[i];
                se.insert(s);
            }
        }
    }

    cout << se.size() << endl;


    return 0;
}

C++: //aoj 0546
#include<iostream>
#include<set>
#include<string>
using namespace std;

int n, k, ans;
set<string> nums;
string card[10], usenum[4];
bool use[10];

void find(int t = 0)
{
	if (t < k) {
		for (int i = 0; i < n; i++) {
			if (use[i]) continue;
			use[i] = true;
			usenum[t] = card[i];
			find(t + 1);
			use[i] = false;
		}
	}
	else {
		string num = "";
		for (int i = 0; i < k; i++) num += usenum[i];
		if (nums.find(num) == nums.end()) {
			ans++;
			nums.insert(num);
		}
	}
	return;
}

int main()
{
	while (true) {
		cin >> n >> k;
		if (!n && !k) break;
		ans = 0;
		nums.clear();
		for (int i = 0; i < n; i++) cin >> card[i];
		find();
		cout << ans << endl;
	}
	return 0;
}

C++: #include <cstdlib>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int n, k;
vector<string> cards(n);
set<string> ans;

void dfs(string number = "", int depth = 0,  int used = 0) {
	if(depth == k)
		ans.insert(number);

	else
		for(int i = 0; i < n; ++i)
			if(!((used >> i) & 1))
				dfs(number + cards[i], depth + 1, used | (1 << i));
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(cin >> n >> k, n) {
		cards.clear();
		cards.resize(n);
		for(int i = 0; i < n; ++i)
			cin >> cards[i];

		ans.clear();
		dfs();
		cout << ans.size() << endl;
	}

	return EXIT_SUCCESS;
}