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<algorithm>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n, k;
vector<string> v;
set<string> st;

// https://github.com/drken1215/algorithm/blob/master/Others/next_combination.cpp
int next_combination(int sub) {
    int x = sub & -sub, y = sub + x;
    return (((sub & ~y) / x) >> 1) | y;
}

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

    while(cin >> n >> k, n != 0 || k != 0){
        v.resize(n);
        st.clear();
        rep(i, n){
            cin >> v[i];
        }

        for(int bit = (1 << k) - 1 ; bit < (1 << n) ; bit = next_combination(bit)){
            vector<int> t;
            rep(i, n){
                if(bit >> i & 1){
                    t.emplace_back(i);
                }
            }

            do{
                string s = "";
                rep(i, k){
                    s = s + v[t[i]];
                }
                st.insert(s);
            }while(next_permutation(t.begin(), t.end()));
        }

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

    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

template <unsigned T>
bool nextBitset(bitset<T> &bs, int digit)
{
    if(bs.none())
        return false;
    bitset<T> x, y, z;
    x = bs.to_ulong() & (~(bs.to_ulong()) + 1ULL);
    y = bs.to_ulong() + x.to_ulong() + 0ULL;
    z = bs & ~y;
    if(bs[digit-1] && bs == z)
        return false;
    bs = ((z.to_ulong() / x.to_ulong()) >> 1) + 0ULL;
    bs |= y;
    return true;
}

int main()
{
    for(;;){
        int n, k;
        cin >> n >> k;
        if(n == 0)
            return 0;

        vector<int> num(n);
        for(int i=0; i<n; ++i)
            cin >> num[i];

        set<int> si;
        bitset<10> bs((1<<k)-1);
        do{
            vector<int> tmp;
            for(int i=0; i<n; ++i){
                if(bs[i])
                    tmp.push_back(num[i]);
            }
            sort(tmp.begin(), tmp.end());
            do{
                int x = 0;
                for(int i=0; i<k; ++i){
                    if(tmp[i] >= 10)
                        x *= 100;
                    else
                        x *= 10;
                    x += tmp[i];
                }
                si.insert(x);
            }while(next_permutation(tmp.begin(), tmp.end()));
        }while(nextBitset(bs, n));

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

C++: #include <iostream>
#include <cstdio>
#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 <iostream>
#include <cmath>
#include <cstring>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;

int main(){
    int n, k, C[100];
    int checked_num[1000000]={};
    while(1){
        cin >> n >> k;
        if(n==0 && k==0) break;
        for(int i=0; i<n; i++)
            cin >> C[i];
        sort(C, C+n);

        // 全ての組み合わせを考える
        do{
            string num="";
            for(int i=0; i<k; i++)
                num+=to_string(C[i]);
            checked_num[stoi(num)]=1;
        }while(next_permutation(C, C+n));

        int ans=0;
        for(int i=0; i<1000000; i++)
            ans+=checked_num[i];
        cout << ans << endl;

        memset(C, 0, 100*sizeof(int));
        memset(checked_num, 0, 1000000*sizeof(int));
    }
    return 0;
}