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<algorithm>
#include<functional>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int main(){
	int n,k;
	while(cin>>n>>k){
		if(n==0){
			break;
		}

	int d[20]={},cnt[10000]={},c;
	for(int i=0;i<n;i++){
		cin>>d[i];
	}
	if(k==2){
	c=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(i!=j){
					if(d[j]>=10){
						cnt[c]+=d[i]*100+d[j];
						
					}
				else{
				cnt[c]+=d[i]*10+d[j];
				
				}
				c++;
				}
			}
		}

	}
	if(k==3){
	c=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(i!=j){
				for(int k=0;k<n;k++){
					if(i!=k && j!=k){
						if(d[k]>=10){
							if(d[j]>=10){
								cnt[c]=d[i]*10000+d[j]*100+d[k];
							}
							else{
								cnt[c]=d[i]*1000+d[j]*100+d[k];
							}
						}
						else{
							if(d[j]>=10){
								cnt[c]=d[i]*1000+d[j]*10+d[k];
							}
							else{
								cnt[c]=d[i]*100+d[j]*10+d[k];
							}
						}
					c++;
					}

			}
				}
		}

	}
	}
	if(k==4){
	c=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
			if(i!=j){
				for(int k=0;k<n;k++){
				if(i!=k && j!=k){
					for(int l=0;l<n;l++){
						if(i!=l && j!=l && k!=l){
					if(d[l]>=10){
						if(d[k]>=10){
							if(d[j]>=10){
								cnt[c]=d[i]*1000000+d[j]*10000+d[k]*100+d[l];
							}
							else{
								cnt[c]=d[i]*100000+d[j]*10000+d[k]*100+d[l];
							}
						}
						else{
							if(d[j]>=10){
								cnt[c]=d[i]*100000+d[j]*1000+d[k]*100+d[l];
							}
							else{
								cnt[c]=d[i]*10000+d[j]*1000+d[k]*100+d[l];
							}
						}
					}
					else{
						if(d[k]>=10){
							if(d[j]>=10){
								cnt[c]=d[i]*100000+d[j]*1000+d[k]*10+d[l];
							}
							else{
								cnt[c]=d[i]*10000+d[j]*1000+d[k]*10+d[l];
							}
						}
						else{
							if(d[j]>=10){
								cnt[c]=d[i]*10000+d[j]*100+d[k]*10+d[l];
							}
							else{
								cnt[c]=d[i]*1000+d[j]*100+d[k]*10+d[l];
							}
						}
					}
					c++;
				}
					}
				}
				}
			}
			}
			}
		
	}
	sort(cnt,cnt+c+1,greater<int>());
	int ans=0;
	for(int i=0;i<c;i++){
		if(cnt[i]>cnt[i+1]){
				ans++;
		}
	}
	cout<<ans<<endl;
	}
	return 0;
	}

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

int n, k;
char card[10];
set<int>ans;
void solve(int a, vector<char>vec, vector<bool>b) {
	vec.push_back(card[a]);
	b[a] = true;
	if (vec.size() == k) {
		ostringstream oss;
		for (char i : vec)
			oss << (int)i;
		ans.insert(stoi(oss.str()));
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!b[i])
			solve(i, vec, b);
	}
}
int main(){
	int cnt = ans.size();
	scanf("%d%d", &n, &k);
	if (!(n | k))return 0;
	for (int i = 0; i < n; i++)
		scanf("%d", &card[i]);
	vector<char>vec;
	vector<bool>b(10);
	for (int i = 0; i < n; i++)
		solve(i, vec, b);
	printf(" %d\n", ans.size() - cnt);
	main();
	return 0;
}

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

#define SIZE 10

int n;
vector<int> d(SIZE + 1);
set<string> nums;

void run(int k, string num) {
    if (k == 0) {
        nums.insert(num);
        return;
    }
    for (vector<int>::iterator it = d.begin(); it != d.end(); it++) {
        int temp = *it;
        d.erase(it);
        stringstream ss;
        ss << temp;
        run(k - 1, num + ss.str());
        d.insert(it, temp);
    }
}

int main() {
    while (true) {
        int k;
        cin >> n >> k;
        if ((n | k) == 0) {
            break;
        }
        d.resize(n);
        for (int i = 0; i < n; i++) {
            cin >> d[i];
        }
        nums.clear();
        string num;
        run(k, num);
        cout << nums.size() << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
int n,k;
string cards[10];
bool used[10];
map<string,int>memo;
void func(int m,string str){
    if(m==k){
        memo[str]=1;
        return;
    }

    for(int i=0;i<n;i++){
        if(!used[i]){
            string _str=str+cards[i];
            used[i]=true;
            func(m+1,_str);
            used[i]=false;
        }
    }

}
int main(){
    while(cin>>n>>k,n or k){
        for(int i=0;i<n;i++)cin>>cards[i];
        fill(used,used+10,false);
        memo.clear();
        func(0,"");
        cout<<memo.size()<<endl;
    }
}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, save4[1000000], roop2;
bool save[10000000];
string card[10], res, save2, save5[10000000];
void solve(int a){
	roop--;
	for (int i = 0; i<n; i++){
		if(i==a) continue;
		if (save3 != i){
			if (roop == 0){
				save2 = res;
				res += card[i];
				num = stoi(res);
				if (save[num] == 0){
					ans++;
				}
				save[num] = 1;
				save[roop2] = num;
				roop2++;
				res = save2;
			}
			else{
				save5[i]=res;
				res += card[i];
				solve(i);
				roop++;
				res = save5[i];
			}
		}
	}
}
int main() {
	while (true){
		cin >> n >> k;
		roop2 = 0;
		if (n == 0 && k == 0){
			return 0;
		}
		for (int i = 0; i < n; i++){
			cin >> card[i];
		}
		for (int i = 0; i < n; i++){
			roop = k - 1;
			res = card[i];
			save3 = i;
			solve(i);
		}
		cout << ans << endl;
		for (int i = 0; i < 10000000; i++){
			if (save[save4[i]] == 0)
				break;
			save[save4[i]] = 0;
		}
	}
}