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;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> P;
typedef pair<ll, pair<ll, ll> > P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int n,k;
int s[15];
set<int> st;
bool used[15]{};

void dfs(int i, int t){
    if(i==0){
        st.insert(t);
    }
    REP(j,n){
        if(used[j])continue;
        used[j] = true;
        dfs(i-1,t*(s[j]>9?100:10)+s[j]);
        used[j] = false;
    }
}

int main() {
    while(1){
        cin >> n >> k;
        if(n==0)break;
        REP(i,n)cin >> s[i];
        st.clear();
        fill(used,used+15,false);
        dfs(k,0);
        cout << st.size() << endl;
    }
    return 0;
}

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

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
	while (cin >> n >> m, n) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) cin >> a[i];
		vector<set<int> > dp(1 << n);
		dp[0] = set<int>({ 0 });
		for (int i = 1; i < 1 << n; i++) {
			for (int j = 0; j < n; j++) {
				if (i & (1 << j)) {
					for (int k : dp[i - (1 << j)]) {
						dp[i].insert(stoi(to_string(k) + to_string(a[j])));
					}
				}
			}
		}
		set<int> ret;
		for (int i = 0; i < 1 << n; i++) {
			if (__builtin_popcount(i) == m) {
				ret.insert(dp[i].begin(), dp[i].end());
			}
		}
		cout << ret.size() << endl;
	}
	return 0;
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int n, k, card[10];
int used[10];
set<long long> s;

long long toInt() {
	long long res = 1;
	int a[4];
	REP(i, n) if (used[i] > 0) a[used[i]-1] = card[i];
	REP(i, k) {
		if (a[i] < 10) {
			res = res*10 + a[i];
		} else {
			res = res*10 + a[i]/10;
			res = res*10 + a[i]%10;
		}
	}
	return res;
}

void select(int cnt) {
	if (cnt == k) {
		s.insert(toInt());
		return;
	}
	REP(i, n) {
		if (used[i] == 0) {
			used[i] = cnt + 1;
			select(cnt + 1);
			used[i] = 0;
		}
	}
}

int main() {
	while (true) {
		s.erase(s.begin(), s.end());
		scanf("%d %d", &n, &k);
		if (n == 0 && k == 0) break;
		REP(i, n) scanf("%d", &card[i]);
		select(0);
		cout << s.size() << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;
const int MOD = 1e9 + 7;
 


int n, k;
set<string> st;
string s;
vi v;

void calc(int c, vi u){
	if(c == k){
		do{
			string t;
			for(int i = 0; i < k; i++){
				t += to_string(u[i]);
			}
			st.insert(t);
		}while(next_permutation(all(u));
		return;
	}
	for(int i = c; i + k - c <n; i++){
		u.pb(v[i]);
		calc(c+1, u);
		u.pop_back();
	}
}


int main(){
	while(cin>>n>>k){
		if(n == 0 && k == 0) break;
		v = vi(n);
		rep(i, n) cin>>v[i];
		s = "";
		st.clear();
		vi tmp;
		calc(0,tmp);
		cout<<st.size()<<endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
int n={},k={};
string integer[11]={};
vector<string>num={};
int chousen[10]={};
string complete_integer[1000]={};
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<=10;i++){
            
            integer[i]={};
            chousen[i]={};
        }
    num={};
        for(int i=0;i<=999;i++){
            complete_integer[i]={};
        }
    answer={};
    integer_count=1;
        for(int i=0;i<=99999999;i++){
            check[i]={};
        }
    counter=0;
    
        
    }
    return 0;
}

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;
    }
}