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<cstdio>
#include<string>
#include<set>
#include<string.h>
#include<ctype.h>

using namespace std;

int arr[10];
int sub[10];
int n,k;
int now = 0;
int cont;
int c = 0;

set<int> visit2;

void saiki(int[],int);

int main()
{
	for(;;)
	{
		visit2.clear();
		int i;
		c = 0;
		now = 0;
		
		scanf("%d",&n);
		scanf("%d",&k);
		if(n == 0 && k == 0){
			break;
		}
		
		for(i = 0; i < n; i++){
			scanf("%d",&arr[i]);
		}
		for(i = 0; i < n; i++){
			sub[0] = arr[i];
			saiki(sub,1);
			now++;
		}
		cout <<visit2.size()-c << endl;
	}
}

void saiki(int temp[], int kk)
{
	//k:TõK
	int used[50] = {0};
	int i,j;
	int t = 0;
	char str[256] = "";
	
	for(i = 0; i < kk; i++){
		sprintf(str+strlen(str),"%d",temp[i]);
	}
	
	t = atoi(str);
	
	if(kk == k){
		visit2.insert(t);
		return;
	}
	else{
		for(i = 0; i < kk; i++){
			for(j = 0; j < n; ++j ){     
				if( used[j] == 0 && arr[j] == temp[i] ){
					used[j] = 1;
					break;
				}
			}
		}
		
		for(i = 0; i < n; i++){
			if(used[i] == 0){
				temp[kk] = arr[i];
				used[i] = 1;
				saiki(temp, kk+1);
				used[i] = 0;
			}
		}
	}
}

C++: #include "bits/stdc++.h"

using namespace std;

#define int long long

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define VDEL(v, n) v.erase(v.begin() + (n))
#define ALL(v) v.begin(), v.end()
#define INTMAX 2147483647
#define INF 200000000
#define STOI(str) atoi(str.c_str())
#define ITOS(num) to_string(num)
#define NPT(begin, end) next_permutation(begin, end)
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<vector<int>> VII;

signed main()
{
    while(true)
    {
        int n,k;
        cin >> n >> k;
        if(n == 0 && k == 0) break;
        VI vi;
        REP(i, n)
        {
            int a;
            cin >> a;
            vi.push_back(a);
        }
        sort(ALL(vi));
        set<int> ss;
        do
        {
            int sum = 0;
            REP(i, k)
            {
                int r = vi[i];
                if(r >= 10) sum = sum * 100 + r;
                else sum = sum * 10 + r;
            }
            ss.insert(sum);
        }
        while(next_permutation(ALL(vi)));
        cout << ss.size() << endl;
    }
    return 0;
}

C++: #include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;

vector<int> num;
vector<int> sofa;
vector<int> vals;
int n;
int k;
void search(int i)
{
	if(sofa.size()==k){
		int order[4]={0,1,2,3};
		do{
			int v=0;
			for(int i=0;i<k;i++){
				int a=vals[sofa[order[i]]];
				if(a>=10){
					v*=100;
				}
				else
				{
					v*=10;
				}
				v+=a;
			}
			//cout << v << endl;
			num.push_back(v);
		}while(next_permutation(order,order+k));
	}
	else if (i==vals.size()){
		return;
	}
	else{
		sofa.push_back(i);
		search(i+1);
		sofa.pop_back();
		search(i+1);
	}
}
int main() {
while(1){
	cin >> n >> k;
	if(n==0&&k==0){
		return 0;
	}
	num.clear();
	vals.clear();
	for(int i=0;i<n;i++){
		int a;
		cin >> a;
		vals.push_back(a);
	}
	search(0);
	sort(num.begin(),num.end());
	int ret=unique(num.begin(),num.end())-num.begin();
	cout << ret << endl;

}
}

C++: // Didn't see editorial

#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <algorithm>

using namespace std;

using ll = long long;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
    while(true) {
        int N, K;
        cin >> N >> K;
        if (N == 0) return 0;

        vector<int> R(N);
        int ans = 0;
        map<int, bool> appeared;
        for (int i = 0; i < N; i++) cin >> R[i];

        for (int i = 0; i < (1<<N); i++) {
            vector<int> cards;
            for (int k = 0; k < N; k++) {
                if ((i>>k) & 1) cards.push_back(R[k]);
            }
            if (cards.size() != K) continue;
            sort(cards.begin(), cards.end());

            do {
                string joined = "";
                for (auto val : cards) {
                    joined += to_string(val);
                }
                if (!appeared[stoi(joined)]) {
                    appeared[stoi(joined)] = true;
                    ans++;
                }
            } while (next_permutation(cards.begin(), cards.end()));
        }
        cout << ans << endl;
    }
}