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

#define rep(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define rept(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )

int N, K;
std::string num[11];
std::set<std::string> data;

void f( int used, int cnt, std::string d ){
	if( cnt == 0 ) data.insert(d);

	rep( i, N ){
		if( used & 1 << i ) continue;

		f( used | 1 << i, cnt - 1, d + num[i] );
	}
}

int main(){
	while( scanf("%d%d", &N, &K), N * K ){
		data.clear();

		rep( i, N ) std::cin >> num[i];

		f( 0, K, "" );

		std::cout << data.size() << std::endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if( num == k ) {
    all.insert(s);
  }
  for(int i=0; i<n; i++) {
    if( flag[i] ) continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  while( cin >> n >> k, n + k ) {
    all.clear();
    for(int i=0; i<n; i++) {
      cin >> a[i];
    }
    for(int i=0; i<11; i++) flag[i] = false;
    solve(0, "");
    cout << all.size() << endl;
  }
}

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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }
template<class T> ostream& operator<<(ostream &s, set<T> P) {s<<"{ ";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<*it;}return s<<" }"<<endl;}



int n, k;

set<string> se;

int main() {
    while (cin >> n >> k) {
        if (n == 0) break;
        
        vector<string> vec(n);
        for (int i = 0; i < n; ++i) cin >> vec[i];
        
        se.clear();
        
        for (int bit = (1<<k)-1, bx, by; bit < (1<<n); bx = bit&-bit, by = bit+bx, bit = ((bit&~by)/bx>>1)|by) {
            vector<string> tmp;
            for (int i = 0; i < n; ++i) if (bit & (1<<i)) tmp.PB(vec[i]);
            
            sort(ALL(tmp));
            do {
                string str = "";
                for (int i = 0; i < tmp.size(); ++i) str += tmp[i];
                se.insert(str);
            } while (next_permutation(ALL(tmp)));
        }
        
        //COUT(se);
        
        cout << se.size() << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

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

    set<string> ans;
    for(int bit=1;bit<(1<<n);bit++){
      if(__builtin_popcount(bit) == k){
        vector<string> use;
        for(int i=0;i<n;i++){
          if( (bit>>i)&1 ) use.push_back(a[i]);
        }
        do{
          string con;
          for(int i=0;i<k;i++) con += use[i];
          ans.insert(con);
        }while( next_permutation(use.begin(), use.end()) );
      }
    }
    cout << ans.size() << endl;
  }
}