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 <set>
#include <string>

using namespace std;


void solve()
{
	int n, k;
	while (cin >> n >> k, n || k)
	{
		vector<string> card(n);
		for (int i = 0; i < n; ++i)
		{
			cin >> card[i];
		}
		set<string> S;
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				if (k == 2)
				{
					if (i != j)
					{
						S.insert(card[i] + card[j]);
						S.insert(card[j] + card[i]);
					}
				}
				else
				{
					if (k == 3)
					{
						for (int a = 0; a < n; ++a)
						{
							if (i != j && j != a && i != a)
							{
								S.insert(card[i] + card[j] + card[a]);
								S.insert(card[i] + card[a] + card[j]);
								S.insert(card[j] + card[i] + card[a]);
								S.insert(card[j] + card[a] + card[i]);
								S.insert(card[a] + card[i] + card[j]);
								S.insert(card[a] + card[j] + card[i]);
							}
						}
					}
					else
					{
						for (int a = 0; a < n; ++a)
						{
							for (int b = 0; b < n; ++b)
							{
								if (i != j && i != a && i != b &&
									j != a && j != b &&
									a != b)
								{
									S.insert(card[i] + card[j] + card[a] + card[b]);
									S.insert(card[i] + card[j] + card[b] + card[a]);
									S.insert(card[i] + card[a] + card[b] + card[j]);
									S.insert(card[i] + card[a] + card[j] + card[b]);
									S.insert(card[i] + card[b] + card[a] + card[j]);
									S.insert(card[i] + card[b] + card[j] + card[a]);
									
									S.insert(card[j] + card[i] + card[a] + card[b]);
									S.insert(card[j] + card[i] + card[b] + card[a]);
									S.insert(card[j] + card[a] + card[b] + card[i]);
									S.insert(card[j] + card[a] + card[i] + card[b]);
									S.insert(card[j] + card[b] + card[a] + card[i]);
									S.insert(card[j] + card[b] + card[i] + card[a]);

									S.insert(card[a] + card[i] + card[b] + card[j]);									
									S.insert(card[a] + card[i] + card[j] + card[b]);
									S.insert(card[a] + card[j] + card[b] + card[i]);
									S.insert(card[a] + card[j] + card[i] + card[b]);
									S.insert(card[a] + card[b] + card[i] + card[j]);
									S.insert(card[a] + card[b] + card[j] + card[i]);

									S.insert(card[b] + card[i] + card[a] + card[j]);
									S.insert(card[b] + card[i] + card[j] + card[a]);
									S.insert(card[b] + card[a] + card[i] + card[j]);
									S.insert(card[b] + card[a] + card[j] + card[i]);
									S.insert(card[b] + card[j] + card[a] + card[i]);
									S.insert(card[b] + card[j] + card[i] + card[a]);
								}
							}
						}
					}
				}
			}
		}
		cout << S.size() << endl;
	}
}

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

C++: #include<iostream>
#include<vector>
#include<map>
  
using namespace std;
  
int n,k,ans;
vector<string> V;
map<string,vector<int> > M;
map<string,int> USED;
void init(){
  V.clear();
  M.clear();
  USED.clear();
  ans = 0;
}
 
bool find(string s,int num){
  for(int i = 0; i < M[s].size(); i++)
    if(M[s][i] == num) return true;
 
  return false;
}
  
void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  M[s].push_back(rev);

  if(rev == k && USED[s] == 0){
    USED[s] = 1;
    ans++;
    return;
  }
  
  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0){
      if(!find(s+V[i],rev+1))
	solve(s+V[i],rev+1,used+(1<<i));
      
    }
}
  
int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

int N, K;

int main() {
	cin >> N;
	cin >> K;
	while (N&&K) {
		set<int>S;
		int num[10] = {};
		int rate[10] = {};
		for (int i = 0; i < N; i++) {
			cin >> num[i];
			if (num[i] >= 10)rate[i] = 100;
			else rate[i] = 10;
		}
		if (K == 2) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i == j)continue;
					int box = num[i] * rate[j];
					box += num[j];
					S.insert(box);
				}
			}
		}
		if (K == 3) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i == j)continue;
					for (int k = 0; k < N; k++) {
						if (i == k || j == k)continue;
						int box = num[i] * rate[j];
						box += num[j];
						box *= rate[k];
						box += num[k];
						S.insert(box);
					}
				}
			}
		}
		if (K == 4) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i == j)continue;
					for (int k = 0; k < N; k++) {
						if (i == k || j == k) continue;
						for (int l = 0; l < N; l++) {
							if (i == l || j == l || k == l)continue;
							int box = num[i] * rate[j];
							box += num[j];
							box *= rate[k];
							box += num[k];
							box *= rate[l];
							box += num[l];
							S.insert(box);
						}
					}
				}
			}
		}
		cout << S.size() << endl;
		cin >> N;
		cin >> K;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n, k;
  while (cin >> n >> k, n) {
    vector<string> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    set<string> st;
    for (int i = 0; i < 1 << n; i++) {
      if (bitset<10>(i).count() != k) continue;
      vector<int> b;
      for (int j = 0; j < n; j++) {
        if (i >> j & 1) {
          b.push_back(j);
        }
      }

      do {
        string w = "";
        for (int j = 0; j < k; j++) {
          w += a[b[j]];
        }
        st.insert(w);
      } while (next_permutation(b.begin(), b.end()));
    }

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

  return 0;
}