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 <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;
ll n,k;
set<string> s;

int main()
{while(1){
	cin>>n>>k;
	if(!n) return 0;
	s.clear();
	vector<string> data(n);
	for(ll i=0;i<n;++i) cin>>data[i];
	sort(data.begin(),data.end());
	do{
		string tmp;
		for(ll i=0;i<k;i++){
			tmp+=data[i];
		}
		s.insert(tmp);
	}while(next_permutation(data.begin(),data.end()));
	cout<<s.size()<<endl;
}}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int N,K;
        string str[11];
        string lis[10021];
        cin>>N>>K;
        if(N==0&&K==0)break;
        for(int i=0;i<N;i++)cin>>str[i];
        int ii=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                for(int k=0;k<N;k++){
                    for(int l=0;l<N;l++){
                        if(i!=j&&j!=k&&k!=l&&i!=k&&j!=l&&i!=l){
                            if(K==2)lis[ii]=str[i]+str[j];
                            if(K==3)lis[ii]=str[i]+str[j]+str[k];
                            if(K==4)lis[ii]=str[i]+str[j]+str[k]+str[l];
                            ii++;
                        }
                    }
                }
            }
        }
        sort(lis,lis+10012);
        int ans=0;
        for(int i=0;i<10015;i++){
            if(lis[i]!=lis[i+1])ans++;
            cout<<lis[i]<<endl;
        }
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
int to_i(string s){
  int k=1,res=0;
  for(int i=s.size()-1;i>=0;i--){
    res+=(s[i]-'0')*k;
    k*=10;
  }
}
int main(){
  int n,k;
  while(cin>>n>>k&&n&&k){
    string a[n];
    for(int i=0;i<n;i++)cin>>a[i];
    set<string> f;
    int ans=0;
    for(int i=0;i<1<<n;i++){
      int c=0;
      vector<string> t;
      for(int j=0;j<n;j++)if((i>>j)&1){
          c++;t.PB(a[j]);
      }
      if(c!=k)continue;
      sort(t.begin(),t.end());
      do{
        string res="";
        for(int j=0;j<t.size();j++)res+=t[j];
        if(f.count(res)==0){
          ans++;
          f.insert(res);
        }
      }while(next_permutation(t.begin(),t.end()));
    }
    cout<<f.size()<<endl;
  }
}

C++: #include<vector>
#include<map>
#include<string>
#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int main(){for(int n,k;cin>>n>>k,n;){
	vector<string> v;string s;
	for(;n;n--)cin>>s,v.push_back(s);
	map<string,int>m;
	sort(v.begin(),v.end());
	do{
		m[accumulate(v.begin(),v.begin()+k,string(),[](string x,string y){return x+y;})]=1;
		//http://origin.googlecode.com/svn-history/r366/trunk/core/include/origin/algorithm/boost_combination.hpp
		if(v.begin()==v.begin()+k)break;
		reverse(v.begin()+k,v.end());
	}while(next_permutation(v.begin(),v.end()));
	cout<<m.size()<<endl;
}}

C++: #include <bits/stdc++.h>
using namespace std;

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if( num == k ) {
    all.insert(s);
    return;
  }
  for(int i=0; i<n; i++) {
    if( flag[i] ) continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  while( cin >> n >> k, n + k ) {
    all.clear();
    for(int i=0; i<n; i++) {
      cin >> a[i];
    }
    for(int i=0; i<11; i++) flag[i] = false;
    solve(0, "");
    cout << all.size() << endl;
  }
}

C++: #include<bits/stdc++.h>
#define LL long long
static const LL INF = 1<<50;

using namespace std;

int n,k,ans=1;
vector<string> s;
string num[11];
int t=0;
void cal(string sum,int depth,int bit){
    if(depth==k){
        s.push_back(sum);
        return;
    }
    for(int i=1;i<=n;++i){
        int b=1<<i;
        if(bit&b){
            int k=bit-b;
            cal(sum+num[i],depth+1,k);
        }
    }
}

int main(){
    while(1){
        int p=1;
        t=0;
        cin>>n>>k;
        if(n==0&&k==0)break;
        for(int i=1;i<=n;++i){
            p+=pow(2,i);
            cin>>num[i];
        }
        cal("",0,p);
        sort(s.begin(),s.end());
        for(int i=1;i<s.size();++i){
            if(s[i]!=s[i-1]){
                ans++;
            }
        }
        cout<<ans<<endl;
        ans=1;
        s.clear();
    }
}

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

int n, k;
int array[10];

int c[4];
bool use[10];
char str[10000][100];
int m;

bool number[100000000];

void Search(int i, int j)
{
	if (j == k){
		for (int l = 0; l < k; l++){
			char nstr[3];
			sprintf(nstr, "%d", c[l]);
			strcat(str[m], nstr);
		}
		
		int num;
		sscanf(str[m], "%d", &num);
//		printf("%d\n", num);
		number[num] = true;
		
		m++;
		return;
	}
	
	if (i == n){
		return;
	}
	
	Search(i + 1, j);
	
	if (use[i]){
		return;
	}
	
	c[j] = array[i];
	
	use[i] = true;
	
	Search(0, j + 1);
	
	use[i] = false;
}

int main()
{
	while (scanf("%d %d", &n, &k), n + k){
		memset(array, 0, sizeof(array));
		memset(c, 0, sizeof(c));
		memset(use, 0, sizeof(use));
		memset(str, 0, sizeof(str));
		memset(number, 0, sizeof(number));
		m = 0;
		
		
		for (int i = 0; i < n; i++){
			scanf("%d", &array[i]);
		}
		
		Search(0, 0);
		
		int cnt = 0;
		for (int i = 0; i < 100000000; i++){
			cnt += number[i];
		}
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

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

int N, K;
char A[11][10];
int used[11];
char num[100];
int size, arr[6000];
int ans = 0;

void rec(int k) {
	int length = strlen(num);
	if (k == K) {
		arr[size++] = atoi(num);
		return;
	}
	for (int 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() {
	for (int i = 0; i < size; ++i) {
		for (int j = 1; j < size - i; ++j) {
			if (arr[j - 1] > arr[j]) {
				std::swap(arr[j - 1], arr[j]);
			}
		}
	}
}

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

		for (int i = 0; i < N; ++i) { used[i] = 0; }
		strcpy(num, "");
		size = 0;
		rec(0);
		bubbleSort();
		ans = 0;
		for (int i = 0; i < size; ++i) {
			if (i == 0 || arr[i - 1] != arr[i]) { ans++; }
		}
		printf("%d\n", ans);
	}
	return 0;
}