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<stdio.h>
#include<string>
#include<set>
using namespace std;

int n, k;
int card[10];
bool b[10];
set<string>ans;
void solve(string sum, int rest) {
	if (!rest) {
		ans.insert(sum);
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!b[i]){
			b[i] = true;
			solve(sum + to_string(card[i]), rest - 1);
			b[i] = false;
		}
	}
}
int main(){
	while (scanf("%d%d", &n, &k), n){
		ans.clear();
		memset(b, 0, sizeof(b));
		for (int i = 0; i < n; i++)
			scanf("%d", &card[i]);
		solve("\0", k);
		printf("%d\n", ans.size());
	}
	return 0;
}

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

int main() {
    while (true) {
        int n, k;
        cin >> n >> k;
        if (!n & !k) break;
        int a[10], use[10] = {};
        for (int i = 0; i < n; ++i) cin >> a[i];
        sort(a, a + n);
        for (int i = 0; i < k; ++i) use[i] = 1;
        vector<string> v;
        do {
            int choise[k];
            for (int i = 0, j = 0; i < n; ++i) {
                if (use[i]) choise[j++] = a[i];
            }
            do {
                string s;
                for (int i = 0; i < k; ++i) s += to_string(choise[i]);
                bool can = true;
                for (int i = 0; i < v.size(); ++i) {
                    if (s == v[i]) {
                        can = false;
                        break;
                    }
                }
                if (can) v.push_back(s);
            } while (next_permutation(choise, choise + k));
        } while (prev_permutation(use, use + n));
        cout << v.size() << endl;
    }
    
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<stack>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

vector<int> card[10];
map<stack<int> ,int> check;
int visit[10];

stack<int> ub;
int cont;

void saiki(int dep,int k,int n){
	if(dep==k){
		if(check[ub]==0){
			check[ub]=1;
			cont++;
		}
		return;
	}
	
	rep(i,n){
		if(visit[i]==0){
			visit[i]=1;
			rep(j,card[i].size()){
				ub.push(card[i][j]);
			}
			
			saiki(dep+1,k,n);
			
			rep(j,card[i].size()){
				ub.pop();
			}
			visit[i]=0;
		}
	}
}

int main(){
	while(1){
		int n;
		int k;
		
		scanf("%d%d",&n,&k);
		if(n==0)break;
		
		rep(i,10){
			card[i].clear();
			visit[i]=0;
		}
		
		check.clear();
		cont=0;
		
		rep(i,n){
			int a;
			scanf("%d",&a);
			
			if(a<10){
				card[i].push_back(a);
			}else{
				card[i].push_back(a/10);
				card[i].push_back(a%10);
			}
		}
		
		saiki(0,k,n);
		
		printf("%d\n",cont);
	}
}

C++: #include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;

map<string,bool> m;
int n,k;
string a[20];

bool f[20];
void dfs(string str,int dep){
    if(dep == k){

    m[str] = true;
        return;
    }
    for(int i = 0;i < n;i++){
        if(f[i] == false){
            f[i] = true;
            dfs(str + a[i],dep+1);
            f[i] = false;
        }
    }
}
int main(void){

    cin>>n>>k;
    for(int i = 0;i < n;i++){
        cin>>a[i];
    }
    dfs("",0);
    int cnt = 0;
    map<string,bool>::iterator it = m.begin();
	while( it != m.end() )
	{
		//cout << (*it).first << ":" << (*it).second << endl;
		++it;
        cnt++;
	}
    cout<<cnt<<endl;

    return 0;
}

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

using namespace std;

int main() {
    while(true) {
        int N, K;
        cin >> N >> K;
        if(!N && !K) break;

        vector<string> cards(N);
        for(int i = 0; i < N; ++i) {
            cin >> cards[i];
        }
        int pos[] = {0,1,2,3};
        set<int> made;
        while(true) {
            vector<int> arr(&pos[0], &pos[K]);
            do {
                string str;
                for(int i = 0; i < K; ++i) {
                    str += cards[arr[i]];
                }
                int n = atoi(str.c_str());
                made.insert(n);
            } while(next_permutation(arr.begin(), arr.end()));

            ++pos[K-1];
            for(int i = K-1; i >= 0; --i) {
                if(pos[i] >= N-(K-i-1)) {
                    if(i > 0) ++pos[i-1];
                    pos[i] = -1;
                }
                else break;
            }
            /*
            cout << "pos[0]: " << pos[0] << endl;
            cout << "pos[1]: " << pos[1] << endl;
            cout << "pos[2]: " << pos[2] << endl;
            */
            if(pos[0] == -1) break;
            for(int i = 1; i < K; ++i) {
                if(pos[i] != -1) continue;
                pos[i] = pos[i-1]+1;
            }
        }
        cout << made.size() << endl;
    }
    return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int n;
	while(cin >> n, n) {
		int k;
		cin >> k;
		vs cards(n);
		vi num(n);
		REP(i, n) {
			cin >> cards[i];
			num[i] = i;
		}

		set<string> ans;
		do {
			string s;
			REP(i, k) {
				s += cards[num[i]];
			}
			ans.insert(s);
		} while(next_permutation(ALL(num)));

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