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<cmath>
using namespace std;

int main(){
	int n,k,c[10],l[10],e[10000],d,a,b;
	while(true){
		cin>>n;
		cin>>k;
		if(n==0 && k==0){
			break;
		}
		for(int i=0;i<n;i++){
			cin>>c[i];
			if(c[i]>=10){
				l[i]=100;
			}
			else{
				l[i]=10;
			}
		}
		d=0;
		if(k==2){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					b=0;
					if(i!=j){
						a=c[i]*l[j]+c[j];
						for(int k=0;k<d;k++){
							if(a==e[k]){
								goto Exit1;
							}

						}
						e[d]=a;
						d++;
Exit1:
						a=0;
					}
				}
			}
		}

		if(k==3){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					for(int m=0;m<n;m++){
						b=0;
						if(i!=j && (j!=m && m!=i)){
							a=c[i]*(l[j]*l[m])+c[j]*l[m]+c[m];
							for(int k=0;k<d;k++){
								if(a==e[k]){
									goto Exit2;
								}
							}
							e[d]=a;
							d++;
Exit2:
							a=0;
						}

					}
				}
			}
		}

		if(k==4){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					for(int m=0;m<n;m++){
						for(int o=0;o<n;o++){
							b=0;
							if(((i!=j && o!=i) && (j!=m && m!=i)) && (o!=j && o!=m)){
								a=c[i]*(l[j]*l[m]*l[o])+c[j]*(l[m]*l[o])+c[m]*l[o]+c[o];
								for(int k=0;k<d;k++){
									if(a==e[k]){
										goto Exit3;
									}
								}
								e[d]=a;
								d++;
Exit3:
								a=0;
							}
						}

					}
				}
			}
		}

		std::cout<<d<<endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<string>

using namespace std;
int main()
{

	bool p[1000001];
	int n, k;
	string m[1000];
	string a;
	cin >> n >> k;
	for (int i = 0; i < n; i++){
		cin >> m[i];
	}
	if (k == 4){
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				for (int i3 = 0; i3 < n; i3++){
					for (int i4 = 0; i4 < n; i4++){
						if (i != i2&&i3 != i4&&i != i3&&i != i4&&i2!=i3&&i2!=i4){
							a = m[i] + m[i2] + m[i3] + m[i4];
							p[atol(a.c_str())]=false;
							a.clear();
						}
					}
				}
			}
		}

	}
	long u=0;
	if (k == 3){
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){
				for (int i3 = 0; i3 < n; i3++){
					if (i != i2&&i != i3&&i2 != i3){
						a = m[i];
						a += m[i2];
						a+=m[i3];
						
						u = atol(a.c_str());
						p[u] = false;
						a.clear();
					}
				}
			}
		}
	}
	int o;
	if (k == 2){
		for (int i = 0; i < n; i++){
			for (int i2 = 0; i2 < n; i2++){

				if (i != i2){
					a = m[i] + m[i2];
					 o = atoi(a.c_str());
				

					p[o] = false;
					a.clear();
				}
			}
		}
	}
	int count = 0;
	for (int i = 0; i < 100000; i++){
		if (!p[i])count++;
	}
	cout << count << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF (1<<28)-1
#define MOD 1000000007
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

int n, k;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
    while (true) {
        cin >> n >> k;
        if (n == 0 && k == 0) break;
        vector<string> a(n);
        map<string, bool> mm;
        rep(i, 0, n) cin >> a[i];
        sort(all(a));
        do {
            string sub = "";
            rep(i, 0, k) sub += a[i];
            mm[sub] = true;
        } while (next_permutation(all(a)));
        cout << mm.size() << endl;
    }
    return 0;
}

C++: // 2013/11/10 Tazoe

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	while(true){
		int n, k;
		cin >> n;
		cin >> k;

		if(n==0 && k==0)
			break;

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

		vector<string> V;

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

			if(k==2){
				string T = S[i0]+S[i1];
				if(find(V.begin(), V.end(), T)==V.end()){
					V.push_back(T);
				}
				continue;
			}
		for(int i2=0; i2<n; i2++){
			if(i2==i0 || i2==i1)
				continue;

			if(k==3){
				string T = S[i0]+S[i1]+S[i2];
				if(find(V.begin(), V.end(), T)==V.end()){
					V.push_back(T);
				}
				continue;
			}
		for(int i3=0; i3<n; i3++){
			if(i3==i0 || i3==i1 || i3==i2)
				continue;

			if(k==4){
				string T = S[i0]+S[i1]+S[i2]+S[i3];
				if(find(V.begin(), V.end(), T)==V.end()){
					V.push_back(T);
				}
				continue;
			}
		}
		}
		}
		}

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

	return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
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 FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int n;
	while(cin >> n, n) {
		int k;
		cin >> k;
		vs cards(n);
		REP(i, n) {
			cin >> cards[i];
		}

		set<string> num;
		sort(ALL(cards));
		do {
			string s;
			REP(i, k) {
				s += cards[i];
			}
			num.insert(s);
		} while(next_permutation(ALL(cards)));

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

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

#define loop(i,n) for(int i=0;i<n;i++)
int d[11];
set<int> s;

int main(){
  int n,k;
  while(cin>>n>>k,n){
    loop(i,n)cin>>d[i];
    s.clear();
    sort(d,d+n);
    do{
      int t=0;
      loop(i,k){
        if(d[i]<10)t=t*10+d[i];
        else t=t*100+d[i];
      }
      s.insert(t);
    }while(next_permutation(d,d+n));
    cout<<s.size()<<endl;
  }
  return 0;
}