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 <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, save4[10000000], 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;
		}
	}
}

C++: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

int N, K;

int main() {
	cin >> N;
	cin >> K;
	while (N&&K) {
		set<int >S;
		int num[10] = {};
		int rate[10] = {};
		for (int i = 0; i < N; i++) {
			cin >> num[i];
			if (num[i] >= 10)rate[i] = 100;
			else rate[i] = 10;
		}
		if (K == 2) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i == j)continue;
					int box = num[i] * rate[i];
					box += num[j];
					S.insert(box);
				}
			}
		}
		if (K == 3) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i == j)continue;
					for (int k = 0; k < N; k++) {
						if (i == k || j == k)continue;
						int box = num[i] * rate[i];
						box += num[j];
						box *= rate[j];
						box += num[k];
						S.insert(box);
					}
				}
			}
		}
		if (K == 4) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i == j)continue;
					for (int k = 0; k < N; k++) {
						if (i == k || j == k) continue;
						for (int l = 0; l < N; l++) {
							if (i == l || j == l || k == l)continue;
							int box = num[i] * rate[i];
							box += num[j];
							box *= rate[j];
							box += num[k];
							box *= rate[k];
							box += rate[l];
							S.insert(l);
						}
					}
				}
			}
		}
		cout << S.size() << endl;
		cin >> N;
		cin >> K;
	}
	return 0;
}

C++: #include<set>
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
	for(int n,k;cin>>n>>k,n;){
		int card[10];
		for(int i=0;i<n;i++)	cin>>card[i];
		sort(card,card+n);

		set<int> st;
		bool mask[10]={};
		for(int i=0;i<k;i++)	mask[i]=true;
		do{
			int chos[10];
			for(int i=0,j=0;i<n;i++)	if(mask[i])	chos[j++]=card[i];
			do{
				int res=0;
				for(int i=0;i<k;i++){
					if(chos[i]<10)	res=res*10+chos[i];
					else			res=res*100+chos[i];
				}
				st.insert(res);
			}while(next_permutation(chos,chos+k));
		}while(prev_permutation(mask,mask+n));

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

	return 0;
}

C++: #include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 1, 1, -1, -1};
int dx[]={1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define mp make_pair
#define fi first
#define sc second
int n,k,c[11];
bool al[11];
set<int> se;
void search(int d,int s){
    if(d >= k){
        se.insert(s);
        return;
    }
    REP(i,n){
        if(!al[i]){
            al[i] = true;
            int t = s * 10;
            if(c[i] > 9) t *= 10;
            search(d + 1,t + c[i]);
            al[i] = false;
        }
    }
}
bool solve(){
    cin >> n >> k;
    if(n == 0 && k == 0)
        return false;
    se.clear();
    REP(i,n)
        cin >> c[i];
    REP(i,n)
        al[i] = false;

    search(0,0);
    printf("%d\n",se.size());
    return true;
}
int main(){
    while(solve());
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main()
{
    int n,k;

    cin >> n >> k;

    while(n!=0||k!=0) {
        int temp;
        vector<int> num;
        vector<int> ord;

        set<int> ans;

        for(int i=0; i<n; ++i) {
            cin >> temp;
            num.push_back(temp);
            ord.push_back(i);
        }

        do {
            temp=0;
            for(int i=0; i<k; ++i) {
                temp*=(num[ord[i]]<10?10:100);
                temp+=num[ord[i]];
            }
            ans.insert(temp);
        } while(next_permutation(ord.begin(),ord.end()));

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

        cin >> n >> k;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<bitset>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<ctime>
#include<complex>
#include<functional>
#include<climits>
#include<cassert>
#include<iterator>
using namespace std;

int n, k;
int card[10];
bool b[10];
set<string>ans;
void solve(string sum, int rest) {
	if (!rest) {
		ans.insert(sum);
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!b[i]){
			b[i] = true;
			solve(sum + to_string(card[i]), rest - 1);
			b[i] = false;
		}
	}
}
int main(){
	while (scanf("%d%d", &n, &k), n){
		ans.clear();
		memset(b, 0, sizeof(b));
		for (int i = 0; i < n; i++)
			scanf("%d", &card[i]);
		solve("\0", k);
		printf("%d\n", ans.size());
	}
	return 0;
}

C++: //aoj 0546
#include<iostream>
#include<set>
#include<string>
using namespace std;

set<string> nums;
int n, k, ans = 0;
string card[10];
bool use[10];
string usenum[4];

int find(int t = 0)
{
	if (t < k) {
		for (int i = 0; i < n; i++) {
			if (use[i]) continue;
			use[i] = true;
			usenum[t] = card[i];
			find(t + 1);
			use[i] = false;
		}
	}
	else {
		string num = "";
		for (int i = 0; i < k; i++) num += usenum[i];
		if (nums.find(num) == nums.end()) {
			ans++;
			nums.insert(num);
		}
	}
}

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> card[i];
	find();
	cout << ans << endl;
	return 0;
}