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>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

int n,k,d[10];

int sum(int a,int b,int c=0,int d=0){
	int x=a*(b>=10?100:10)+b;
	if(k==2)return x;
	x=x*(c>=10?100:10)+c;
	if(k==3)return x;
	return x=x*(d>=10?100:10)+d;
}

int main(){
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++)scanf("%d",&d[i]);
	sort(d,d+n);
	vector<int> data;
	for(int i=n-1;i>=0;i--){
		for(int j=n-1;j>=0;j--){
			if(i==j)continue;
			if(k==2){
				data.push_back(sum(d[i],d[j]));
				continue;
			}
			for(int r=n-1;r>=0;r--){
				if(i==r||j==r)continue;
				if(k==3){
					data.push_back(sum(d[i],d[j],d[r]));
					continue;
				}
				for(int z=n-1;z>=0;z--){
					if(i==z||j==z||r==z)continue;
					data.push_back(sum(d[i],d[j],d[r],d[z]));
				}
			}
		}
	}
	sort(data.begin(),data.end());
	data.erase(unique(data.begin(),data.end()),data.end());
	printf("%ld\n",data.size());
}

C++: #include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;

int next_comb(int k) {
    int x = k & -k;
    int y = k + x;
    return (((k & ~y) / x) >> 1) | y;
}

int main()
{
    while (1) {
    int N, K;
    cin >> N >> K;
    if (N == 0 && K == 0) break;
    vector<int> vi(N);
    for (int n = 0; n < N; ++n) {
        cin >> vi[n];
    }

    set<ll> Set;
    int bit = (1<<K) - 1;
    for (; bit < (1<<N); bit = next_comb(bit)) {
        vector<int> tmp;
        for (int n = 0; n < N; ++n) {
            if (bit & (1<<n)) {
                tmp.push_back(vi[n]);
            }
        }
        vector<int> ind;
        for (int k = 0; k <K; ++k) {
            ind.push_back(k);
        }
        do {
            ostringstream oss;
            for (int k = 0; k < K; ++k) {
                 oss << tmp[ind[k]];
            }
            Set.insert(stoll(oss.str()));
        } while (next_permutation(ind.begin(), ind.end()));
    }

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

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main() {
	int n, k;
	while(1){
		cin >> n >> k;
		if(n + k == 0) break;
		vector<string> s(n);
		set<string> st;
		rep(i, 0, n){
			cin >> s[i];
		}
		sort(all(s));
		do{
			string tmp = "";
			rep(i, 0, k){
				tmp += s[i];
			}
			//cout << tmp <<endl;
			if(st.count(tmp)) continue;
			st.insert(tmp);
		}while(next_permutation(all(s)));
		cout << st.size() <<endl;
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1<<29
#define MAX 11
using namespace std;

int a[MAX],se[MAX];

void solve(){
 int n,k;
 for(;;){
  cin>>n;
  cin>>k;
  if(!n&&!k) break;
  set<int> S;
  FOR(i,n) cin>>a[i];
  FOR(i,1<<n){
   int p=0;
   FOR(j,n) if((1<<j)&i) se[p++]=j;
   if(p!=k) continue;
   vector<int> ord;
   FOR(j,k) ord.pb(j);
   do{
    int t=0;
    FOR(j,k){
     int x=a[se[ord[j]]];
     t*=(x>=10)?100:10;
     t+=x;
    }
    S.insert(t);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

int main(){
 solve();
 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[n];
        for (int i = 0; i < n; ++i) cin >> a[i];
        sort(a, a + n);
        vector<string> v;
        do {
            string s;
            for (int i = 0; i < k; ++i) s += to_string(a[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(a, a + n));
        cout << v.size() << endl;
    }
    
    return 0;
}

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

const int nlimit = 12;
int n, k;
string cards[nlimit];

int solve();
int main(){
  while(cin >> n >> k, n!=0 && k!=0){
    for(int i=0; i<n; i++) cin >> cards[i];
    cout << solve() << endl;
  }

  return 0;
}

int solve(){
  set<string> s;

  sort(cards, cards+n);
  do{
    string tmp = "";
    for(int i=0; i<k; i++){
      tmp += cards[i];
    }
    s.insert(tmp);
  }while(next_permutation(cards, cards+n));

  return s.size();
}

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

using namespace std;

vector<string> v;

v_push(string str){
	bool checker;
	
	checker = true;
	for(int len = 0; len < v.size(); len++){
		if(v.at(len) == str){
			checker = false;
			break;
		}
	}
	if(checker){
		v.push_back(str);
	}
}

string str_int(int num){
	string str = "";
	if(num == 0){
		str = "0";
		return str;
	}
	while(true){
		if(num == 0){
			break;
		}
		string d = "D";
		d[0] = '0' + (num % 10);
		str = d + str;
		num /= 10;
	}
	return str;
}

int main(){
	
	int n, k;
	
	int num[20];
	
	while(true){
		
		cin >> n >> k;
		
		if(n == 0 && k == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> num[i];
		}
		
		string str;
		
		if(k == 2){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j){
						continue;
					}else{
						str = str_int(num[i]) + str_int(num[j]);
						v_push(str);
					}
				}
			}
		}else if(k == 3){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					for(int k = 0; k < n; k++){
						if(i == j || i == k || j == k){
							continue;
						}else{
							str = str_int(num[i]) + str_int(num[j]) + str_int(num[k]);
							v_push(str);
						}
					}
				}
			}
		}else if(k == 4){
			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 || i == k || j == k || i == l || j == l || k == l){
								continue;
							}else{
								str = str_int(num[i]) + str_int(num[j]) + str_int(num[k]) + str_int(num[l]);
								v_push(str);
							}
						}
					}
				}
			}
		}
		
		printf("%d\n", v.size());
		
		v.erase(v.begin(), v.end());
	}
	
	return 0;
}