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 <cstdio>
#include <vector>

using namespace std;

vector<string> v;

void 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;
}

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

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

string card[15];

int main(void){
	int n,k;
	cin >> n >> k;
	for(int i=0;i<n;++i) cin >> card[i];
	set<int> ans;
	sort(card,card+n);
	do{
		string cur="";
		for(int i=0;i<k;++i) cur+=card[i];
		ans.insert(stoi(cur));
	}while(next_permutation(card,card+n));
	cout << ans.size() << endl;
	return 0;
}

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

int count(int n)
{
int ret = 1;
for(int mul = 10; mul <= n; mul *= 10) ++ret; 
return ret;
}

void add(int k, int sum, vector<int> &c, vector<int> &result)
{
if(k == 0) {
result.push_back(sum);
return;
}
for(int i = 0; i < c.size(); ++i) {
if(c[i] == 0)
continue;
int tmp = c[i];
c[i] = 0;
add(k - 1, sum * (count(tmp) + 1) + tmp, c, result);
c[i] = tmp;
}
return;
}

int main()
{
while(true) {
int n, k;
vector<int> c;
vector<int> result;
scanf("%d%d", &n, &k);
if(n == 0)
break;
c.resize(n);
for(int i = 0; i < n; ++i) 
scanf("%d", &c[i]);
add(k, 0, c, result);
sort(result.begin(), result.end());
result.erase(unique(result.begin(), result.end()), result.end());
printf("%d\n", result.size());
}
return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<sstream>
using namespace std;
bool memo[100000000];
class P{
    public:
    int num;
    int cnt;
    bool used[10];
    P(){}
    P(int num,int cnt,bool a[10]):num(num),cnt(cnt){
        for(int i = 0; i < 10; i++){
            used[i] = a[i];
        }
    }
};
int bond(int x,int y){
    stringstream ss1;
    ss1 << x;
    stringstream ss2;
    ss2 << y;
    string ans = ss1.str() + ss2.str();
    stringstream Ans;
    Ans << ans;
    int res;
    Ans >> res;
    return res;
}
int main(){
    int n;
    int m;
    while(scanf("%d%d",&n,&m),(n|m)){
        int card[10];
        queue<P> Q;
        memset(memo,0,sizeof(memo));
        for(short i = 0; i < n; i++){
            bool x[10]={0};
            x[i] = 1;
            scanf("%d",&card[i]);
            Q.push(P(card[i],1,x));
        }
        short ans = 0;
        while( Q.size() ){
            P p = Q.front();Q.pop();
            if(p.cnt == m){
                if( memo[p.num] )continue;
                ans++;
                memo[p.num] = true;
            }
            else{
                for(int i = 0; i < n; i++){
                    if(p.used[i] == 0){
                        p.used[i] = 1;
                        Q.push(P(bond(p.num,card[i]),p.cnt+1,p.used));
                        p.used[i] = 0;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

int f(int x,int y){
	if(y<10)return x*10+y;
	return x*100+y;
}

int main(){
while(1){
	int n,k,c[100],ans=1;
	vector<int> sum;
	scanf("%d%d",&n,&k);
	if(n==0&&k==0)break;
	for(int i=0;i<n;i++)scanf("%d",&c[i]);
	int a=0;
	for(int i=0;i<n;i++){
		int a0=c[i];
		for(int j=0;j<n;j++){
			if(j!=i){
				int a1=f(a0,c[j]);
				if(k==2)sum.push_back(a1);
				else {
					for(int l=0;l<n;l++){
						if(l!=j&&l!=i){
							int a2=f(a1,c[l]);
							if(k==3)sum.push_back(a2);
							else{
								for(int m=0;m<n;m++){
									if(m!=l&&m!=j&&m!=i){
										int a3=f(a2,c[m]);
										sum.push_back(a3);
									}
								}
							}
						}
					}
				}
			}
		}
	}
	sort(sum.begin(),sum.end());
	int s=sum.size();
	for(int i=1;i<s;i++){
		if(sum[i]!=sum[i-1])ans++;
	}
	printf("%d\n",ans);
}
}