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;

using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;

int main() {
    int n, k;
    while(cin >> n >> k, n) {
        vector<string> d(n);
        for(int i = 0; i < n; ++i) {
            cin >> d[i];
        }
        sort(d.begin(), d.end());
        set<string> s;
        for(int S = 0; S < (1 << n); ++S) {
            if(__builtin_popcount(S) == k) {
                vector<string> t;
                for(int j = 0; j < n; ++j) {
                    if(S & (1 << j)) {
                        t.push_back(d[j]);
                    }
                }
                do {
                    string tmp;
                    for(int j = 0; j < k; ++j) {
                        tmp += t[j];
                    }
                    s.insert(tmp);
                } while(next_permutation(begin(t), end(t)));
            }
        }
        cout << s.size() << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()

typedef long long LL;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<int> LArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

int itiv;
inline int getInt() { return (cin >> itiv, itiv); }
void readAll(Array& vec, int n) { for (int i = 0; i < n; i++) cin >> vec[i]; }
inline bool between(int first, int last, int n) { return first <= n && n <= last; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int m, n;
int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

set<int> ans;
Array cards;

void dfs(int idx, int pre, int rem)
{
	if(rem == 0)
	{
		ans.insert(pre);
		return;
	}
	if(idx == n) return;
	dfs(idx + 1, pre, rem);
	if(cards[idx] >= 10)
		dfs(idx + 1, pre * 100 + cards[idx], rem - 1);
	else dfs(idx + 1, pre * 10 + cards[idx], rem - 1);
}

int main(void)
{
	while(cin >> n && n)
	{
		ans.clear();
		int k = getInt();
		cards.resize(n);
		readAll(cards, n);
		dfs(0, 0, k);
		cout << ans.size() << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<set>
#include<cstring>

using namespace std;

set<int> se;

int N,K;

int card[10];
bool used[10];

void rec(int num,int k)
{
	if(k==K)
	{
		se.insert(num);
		return;
	}
	for(int i=0;i<N;i++)
	{
		if(used[i]==true) continue;
		used[i]=true;
		int nnum;
		if(card[i]<10) nnum=num*10+card[i];
		else nnum=num*100+card[i];
		rec(nnum,k+1);
		used[i]=false;
	}
}

int main()
{
	scanf("%d%d",&N,&K);
	for(int i=0;i<N;i++)
	{
		scanf("%d",card+i);
	}
	memset(used,false,sizeof(used));
	rec(0,0);
	printf("%d\n",se.size());
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using ll = long long;
using namespace std;


int main() {
    int n, k;
    while(cin >> n >> k, n|k) {
        vector<int> v(10);
        vector<int> v2(k);
        set<string> S;
        for (int i = 0; i < n; ++i) {
            cin >> v[i];
        }
        sort(v.begin(), v.begin() + n);
        for (int i = 0; i < (1 << n); ++i) {
            if (__builtin_popcount(i) == k) {
                int c = 0;
                for (int j = 0; j < n; ++j) {
                    if (i & (1 << j)) {
                        v2[c] = v[j];
                        c++;
                    }

                }
                sort(v2.begin(), v2.end());

                do {
                    string s;
                    for (int &item : v2) {
                        s += to_string(item);
                    }
                    S.emplace(s);
                } while (next_permutation(v2.begin(), v2.end()));
            }
        }
        cout << S.size() << "\n";
    }
    return 0;
}

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

int main() {
	while(true){
		int n, k, ans = 0;
		string cards[11];
		vector<string> v;
		
		cin >> n >> k;
		if(n == 0 && k == 0){
			break;
		}
	
		for(int i = 1; i <= n; i++){
			cin >> cards[i];
		}
	
		for(int a = 1; a <= n; a++){
			for(int b = 1; b <= n; b++){
				if(a != b){
					v.push_back(cards[a] + cards[b]);
					
					for(int c = 1; c <= n; c++){
						if(k >= 3){
							if(a != c && b != c){
								v.back() += cards[c];
							}
						} else{
							break;
						}
						for(int d = 1; d <= n; d++){
							if(k >= 4){
								if(a != d && b != d && c != d){
									v.back() += cards[d];
								}
							} else{
								break;
							}
						}
					}
				}
			}
		}
	
		sort(v.begin(), v.end());
		for(int i = 0; i < v.size(); i++){
			if(v[i] != v[i + 1] || i == v.size() - 1){
				ans++;
			}
		}
	
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int n={},k={};
string integer[11]={};
vector<string>num={};
int chousen[10]={};
string complete_integer[10000]={};
string answer={};
int integer_count=1;
bool check[100000000]={};
int counter=0;

void operation(){
    for(int i=1;i<=n;i++){
        if (chousen[i]==0){
            
            chousen[i]=1;
            num.push_back(integer[i]);
        
            if(num.size()==k){
                for(int i=1;i<=k;i++){
                    answer+=num[i-1];
                }
                complete_integer[integer_count]=answer;
                integer_count+=1;
                answer={};
            }else{
                operation();
            }
            num.pop_back();
            chousen[i]=0;
        }
        
    }

}



void compare(){
    for(int i=1;i<=integer_count-1;i++){
        //cout << complete_integer[i] << endl;
        if(check[stoi(complete_integer[i])]){
        }else{
            check[stoi(complete_integer[i])]=1;
            counter+=1;
        }
    }
}



int main(){
    
    while(true){


        
    cin >> n >> k;
    if (n==0&&k==0){
        break;
    }
    for(int i=1;i<=n;i++){
        cin >> integer[i];
    }
        
    operation();
    compare();
    cout<< counter << endl;
    n={};
    k={};
        for(int i=0;i<=9;i++){
            
            integer[i]={};
            chousen[i]={};
        }
    num={};
        for(int i=0;i<=9999;i++){
            complete_integer[i]={};
        }
    answer={};
    integer_count=1;
        for(int i=0;i<=99999999;i++){
            check[i]={};
        }
    counter=0;
    
        
    }
    return 0;
}