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 <set>
#include <vector>
#include <sstream>

using namespace std;

set<string> s;
vector<int> cards;
int k;
int n;

bool used[10];
int perm[10];
vector<vector<int> > permList;

void make_perm(int cnt){
	if(cnt != k){
		for(int i = 0; i < n; i++){
			if(!used[i]){
				used[i] = true;
				perm[cnt] = i;
				make_perm(cnt+1);
				used[i] = false;
			}
		}
	}
	else{
		vector<int> tmp;
		for(int i = 0; i < k; i++){
			tmp.push_back(perm[i]);
		}
		permList.push_back(tmp);
	}
}


int main(){

	while(cin >> n >> k && !(n == 0 && k == 0)){
		for(int i = 0; i < n; i++){
			int c;
			cin >> c;
			cards.push_back(c);
		}
	//	dfs(0,0,0);
		make_perm(0);

		for(int i = 0; i < permList.size(); i++){
			string str;
			for(int j = 0; j < permList[0].size(); j++){
				//cout << permList[i][j] << " " << flush;
				stringstream ss;
				ss << cards[permList[i][j]];
				str += ss.str();
			}
			//cout << str << endl;
			//cout << endl;
			s.insert(str);
		}

		cout << s.size() << endl;

		cards.clear();
		s.clear();
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i =0; i < (n); ++i)
using namespace std;

int main() {
        int N = 0, K = 0;
        cin >> N;
        while (N != 0) {
                vector<string> s(N);
                vector<string> u;
                cin >> K;
                rep(n, N) {
                        cin >> s.at(n);
                }
                switch (K) {
                        case 4:
                                rep(n1, N) {
                                        rep(n2, N) {
                                                if(n1 == n2) continue;
                                                rep(n3, N) {
                                                        if (n1 == n3 || n2 == n3) continue;
                                                        rep(n4, N) {
                                                                if (n1 == n4 || n2 == n4 || n3 == n4) continue;
                                                                u.push_back(s.at(n1) + s.at(n2) + s.at(n3) + s.at(n4));
                                                        }
                                                }
                                        }
                                }
                                break;
                        case 3:
                                rep(n1, N) {
                                        rep(n2, N) {
                                                if(n1 == n2) continue;
                                                rep(n3, N) {
                                                        if (n1 == n3 || n2 == n3) continue;
                                                        u.push_back(s.at(n1) + s.at(n2) + s.at(n3));
                                                }
                                        }
                                }
                                break;
                        case 2:
                                rep(n1, N) {
                                        rep(n2, N) {
                                                if(n1 == n2) continue;
                                                u.push_back(s.at(n1) + s.at(n2));
                                        }
                                }
                                break;
                }
                sort(u.begin(), u.end());
                u.erase(unique(u.begin(), u.end()), u.end());
                cout << u.size() << endl;
                cin >> N;
        }
        return 0;
}

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

int connum(int l, int r)
{
int mul = 1;
while(mul <= r) mul *= 10;
return l * mul + r;
}

void add(int k, int sum, vector<int> &c, vector<int> &result)
{
if(k == 0) {
result.push_back(sum);
return;
}
for(int i = 0; i < c.size(); ++i) {
if(c[i] == 0)
continue;
int tmp = c[i];
c[i] = 0;
add(k - 1, connum(sum, tmp), c, result);
c[i] = tmp;
}
return;
}

int main()
{
while(true) {
int n, k;
vector<int> c;
vector<int> result;
scanf("%d%d", &n, &k);
if(n == 0)
break;
c.resize(n);
for(int i = 0; i < n; ++i) 
scanf("%d", &c[i]);
add(k, 0, c, result);
sort(result.begin(), result.end());
result.erase(unique(result.begin(), result.end()), result.end());
printf("%d\n", result.size());
}
return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int n;
int k;

vector<int> card;
vector< string > cd;

string VectorToString(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
	}
	return str;
}

string VectorToDebugText(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
		str += ":";
	}
	return str;
}


int main(){
	while(1){
		int self;
		scanf("%d %d",&n,&k);
		if(n == 0 && k == 0){
			break;
		}
		card.clear();
		cd.clear();
		for(int i=0;i<n;i++){
			scanf("%d",&self);
			card.push_back(self);
		}
		vector<int> cc(k);
	
		sort(card.begin(),card.end());
	
		while(next_permutation(card.begin(),card.end())){
			 copy(card.begin(),card.begin()+k ,cc.begin());
			 cd.push_back(VectorToString(cc));
			 cd.erase(unique(cd.begin(),cd.end()),cd.end());
		}
		sort(cd.begin(),cd.end());
		cd.erase(unique(cd.begin(),cd.end()),cd.end());
		printf("%d\n",cd.size());
	}
}

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

int main(void) {
  int i, j, n, k, l, m, o;
  while( 1 ) {
    scanf("%d%d", &n, &k);
    if(!n && !k) break;
    vector<string> v;
    string a[n], t;
    for(i = 0; i < n; ++i) cin >> a[i];
    for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) {
      if(i == j) continue;
      t = a[i] + a[j];
      if(k == 2) v.push_back(t);
      else {
        for(l = 0; l < n; ++l) {
          if(i == l || j == l) continue;
          t += a[l];
          if(k == 3) v.push_back(t);
          else {
            for(m = 0; m < n; ++m) {
              if(i == m || j == m || l == m) continue;
              t += a[m];
              v.push_back(t);
              for(o = 0; o < a[m].length(); ++o) t.pop_back();
            }
          }
        for(o = 0; o < a[l].length(); ++o) t.pop_back();
        }
      }
    }
   sort(v.begin(), v.end());
   printf("%d\n", (int)(unique(v.begin(), v.end()) - v.begin()));
   vector<string>::iterator it;
   //for(it = v.begin(); it != unique(v.begin(), v.end()); ++it) cout << (*it) << " ";
  }
  return 0;
}