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 <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	int n;
	int k;
	string card[10];
	set<string> ans;
	cin >> n >> k;
	REP(i, n){
		cin >> card[i];
	}
	for(int i = 0;i < n;i++){
		string s = card[i];
		for(int j = 0;j < n;j++){
			if(j == i) continue;
			string t = s;
			t += card[j];
			for(int p = 0;p < n && 2 < k;p++){
				if(p == i || p == j) continue;
				string u = t;
				u += card[p];
				for(int q = 0;q < n && 3 < k;q++){
					string v = u;
					if(q == i || q == j || q == p) continue;
					v += card[q];
					ans.insert(v);
				}
				if(k == 3){
					ans.insert(u);
				}
			}
			if(k == 2){
				ans.insert(t);
			}
		}
	}
	cout << ans.size() << endl;
	return 0;
}

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

vector<int> pattern;
int c;

void check(int combi) {
	int size = pattern.size();
	for(int i = 0; i < size; i++) {
		if(pattern[i] == combi)
			return;
	}
	pattern.resize(size+1);
	pattern[size] = combi;
}

int main() {

	//ifstream cin("INPUT.txt");
	//ofstream cout("OUTPUT.txt");

	int n, k, i, j, l, m, temp;
	int card[10];

	while(cin >> n >> k, n != 0 || k != 0) {

		pattern.resize(0);

		for(i = 0; i < n; i++) {
			cin >> card[i];
		}
	
		for(i = 0; i < n; i++) {
			for(j = 0; j < n; j++) {
				if(i == j)
					continue;
				if(k == 2) {
					temp = card[i];
					if(card[j] >= 10) {
						temp *= 100;
					} else {
						temp *= 10;
					}
					temp += card[j];
					check(temp);
				} else {
					for(l = 0; l < n; l++) {
						if(i == l || j == l)
							continue;
						if(k == 3) {
							temp = card[i];
							if(card[j] >= 10) {
								temp *= 100;
							} else {
								temp *= 10;
							}
							temp += card[j];
							if(card[l] >= 10) {
								temp *= 100;
							} else {
								temp *= 10;
							}
							temp += card[l];
							check(temp);
						} else {
							for(m = 0; m < n; m++) {
								if(i == m || j == m || l == m)
									continue;
								temp = card[i];
								if(card[j] >= 10) {
									temp *= 100;
								} else {
									temp *= 10;
								}
								temp += card[j];
								if(card[l] >= 10) {
									temp *= 100;
								} else {
									temp *= 10;
								}
								temp += card[l];
								if(card[m] >= 10) {
									temp *= 100;
								} else {
									temp *= 10;
								}
								temp += card[m];
								check(temp);
							}
						}
					}
				}
			}
		}

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

	}

	return 0;

}

C++: // 2011/09/16 Tazoe

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
	while(true){
		int n, k;
		cin >> n;
		cin >> k;
		if(n==0&&k==0)
			break;

		string C[10];
		for(int i=0; i<n; i++)
			cin >> C[i];

		map<string, bool> memo;

		for(int i0=0; i0<n; i0++){
			for(int i1=0; i1<n; i1++){
				if(i1==i0)
					continue;

				if(k>=3)
					for(int i2=0; i2<n; i2++){
						if(i2==i0||i2==i1)
							continue;

						if(k>=4)
							for(int i3=0; i3<n; i3++){
								if(i3==i0||i3==i1||i3==i2)
									continue;

								string num = C[i0]+C[i1]+C[i2]+C[i3];
								if(memo[num])
									continue;
								else
									memo[num] = true;
							}
						else{
							string num = C[i0]+C[i1]+C[i2];
							if(memo[num])
								continue;
							else
								memo[num] = true;
						}
					}
				else{
					string num = C[i0]+C[i1];
					if(memo[num])
						continue;
					else
						memo[num] = true;
				}
			}
		}

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

	return 0;
}

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

using i64 = int64_t;

template <class BidirectionalIterator>
  inline constexpr bool next_partial_permutation(BidirectionalIterator first,
                                BidirectionalIterator middle,
                                BidirectionalIterator last)
{
  using std::reverse;
  reverse(middle, last);
  return std::next_permutation(first, last);
}

int main(){
    int n, k;
    while(cin >> n >> k, n != 0 || k != 0){
        vector<string> v;
        for(int i=0;i<n;++i){
            string num;
            cin >> num;
            v.push_back(num);
        }
        sort(v.begin(), v.end());
        set<string> st;
        do{
            string s;
            for(int i=0;i<k;++i){
                s += v[i];
            }
            st.insert(s);
        }while(next_partial_permutation(v.begin(), v.begin()+k, v.end()));
        cout << st.size() << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

inline string toStr(int x) {
  stringstream ss; ss<<x;
  return ss.str();
}

int N, K;
vector<string> v;
vector<bool> used;
set<string> st;
void rec(int cnt, string s) {
  
  if(cnt == K) {
    st.insert(s);
    return;
  }
  
  rep(i, N) {
    if(used[i]) continue;
    used[i] = 1;
    rec(cnt+1, s+v[i]);
    used[i] = 0;
  }
}

int main() {
  
  while(cin >> N >> K && N) {
    st.clear();
    v.clear(); v.resize(N);
    used.clear(); used.resize(N);
    for(int i=0; i<N; i++) cin >> v[i];
    rec(0, "");
    cout << st.size() << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int n,k,perm[10];
string s[10];

int fact(int n){
	if(n==0)return 1;
	return n*fact(n-1);
}

int main(){
	while(cin>>n>>k){
		if(!n)return 0;
		set<string> t;
		for(int i=0;i<n;i++)cin>>s[i];
		int wait=0;
		for(int i=0;i<n;i++)perm[i]=i;
		do{
			if(wait){wait--;continue;}
			string a="";
			for(int i=0;i<k;i++)a+=s[perm[i]];
			t.insert(a);
			wait=fact(n-k)-1;
		}while(next_permutation(perm,perm+n));
		cout<<t.size()<<endl;
	}
}