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 <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(){
	int self;
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++){
		scanf("%d",&self);
		card.push_back(self);
	}
	//printf("%s\n",(VectorToString(card)).c_str());
	vector<int> cc(k);
	
	sort(card.begin(),card.end());
	
	while(next_permutation(card.begin(),card.end())){
		 copy(card.begin(),card.begin()+k ,cc.begin());
		 //cout << VectorToDebugText(card) << endl;
		 cd.push_back(VectorToString(cc));
	}
	sort(cd.begin(),cd.end());
	cd.erase(unique(cd.begin(),cd.end()),cd.end());
	//for(int i=0;i<cd.size();i++){
		//printf("%d:%s\n",i,cd[i].c_str());
	//}
	printf("%d\n",cd.size());
}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;


int card[10],n,k,sen[10];
vector<int> vec;

void dfs(int h,int num){

    if(h==k){
        for(int i=0;i<vec.size();i++)
            if(vec[i]==num) return;
        vec.push_back(num);
        return;
    }
    
    for(int i=0;i<n;i++){
        if(!sen[i]){
            sen[i]=true;
            dfs(h+1,num*pow(10,((int)log10(card[i])+1))+card[i]);
            sen[i]=false;
        }
    }
    
    return;
}

void solve(){
    
    for(int i=0;i<n;i++)
        scanf("%d",&card[i]);

    dfs(0,0);
    
    printf("%d\n",(int)vec.size());
    
    vec.clear();
    
    return;
}


int main(){
    
    while(1){
        scanf("%d%d",&n,&k);
        if(n+k==0) break;
        solve();
    }
    
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T>
int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

template <class T> void max_swap(T& a, const T b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
	int n, k;
	while (scanf("%d%d", &n, &k), n | k)
	{
		int c[10];
		for (int i = 0; i < n; ++i)
			scanf("%d", c + i);

		set<int> combi;
		sort(c, c + n);
		do
		{
			int seq = 0;
			for (int i = 0; i < k; ++i)
			{
				if (c[i] < 10)
					seq = seq*10 + c[i];
				else
					seq = seq*100 + c[i];
			}
			combi.insert(seq);
		} while (next_permutation(c, c + n));
		printf("%d\n", combi.size());
	}
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 11
#define SIZE 6000

int N, K;
char A[MAXN][10];
int used[MAXN];
char num[100];
int size;
int arr[SIZE];

void rec(int k) {
  int i;
  int length = strlen(num);
  if(k == K) {
    arr[size++] = atoi(num);
    return;
  }
  for(i = 0; i < N; ++i) {
    if(!used[i]) {
      used[i] = 1;
      strcat(num, A[i]);
      rec(k+1);
      used[i] = 0;
      num[length] = '\0';
    }
  }
}

void bubbleSort() {
  int i, j, t;
  for(i = 0; i < size; ++i) {
    for(j = 1; j < size-i; ++j) {
      if(arr[j-1] > arr[j]) {
	t = arr[j-1];
	arr[j-1] = arr[j];
	arr[j] = t;
      }
    }
  }
}

int main() {
  int i, ans;
  while(1) {
    scanf("%d", &N);
    scanf("%d", &K);
    if(N == 0 && K == 0) break;
    for(i = 0; i < N; ++i) {
      scanf("%s", A[i]);
    }

    for(i = 0; i < N; ++i) {
      used[i] = 0;
    }
    strcpy(num, "");
    size = 0;
    rec(0);
    bubbleSort();

    ans = 0;
    for(i = 0; i < size; ++i) {
      if(i == 0 || arr[i-1] != arr[i]) {
	++ans;
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}

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

int N, K;
string s[15];

set<string>st;

int dfs(string w, int bit, int k)
{
  if(k == K) {
    if(st.find(w) == st.end()) {
      st.insert(w);
      return 1;
    }else {
      return 0;
    }
  }
  int ret = 0;
  Rep(i,N) {
    if(!((bit >> i) & 1)) {
      ret += dfs(w + s[i], bit | (1 << i), k + 1);
    }
  }
  return ret;
}
  

int main()
{
  while(cin >> N >> K,N || K) {
    Rep(i,N) cin >> s[i];
    cout << dfs("",0,0) << endl;
  }
  return 0;
}

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

using namespace std;

int n,k,ans;
vector<string> V;
vector<string> VV;

void init(){
  V.clear();
  VV.clear();
  ans = 0;
}

void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  if(rev == k){
    sort(VV.begin(),VV.end());
    if(!binary_search(VV.begin(),VV.end(),s)){
      ans++;
      VV.push_back(s);
    }
    return;
  }

  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0)
      solve(s+V[i],rev+1,used+(1<<i));
  
}

int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

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

int main(){
    int n, k;
    while(cin >> n >> k){
        if(n + k == 0)  break;
        string x[n];
        for(int i = 0; i < n; i++)  cin >> x[i];
        sort(x, x+n);

        set<int> ans;
        do{
            string tmp = "";
            for(int i = 0; i < k; i++)  tmp += x[i];
            ans.insert(stoi(tmp));
        }while(next_permutation(x, x+n));

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