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 <bits/stdc++.h>
using namespace std;
string arr[10];

void solve2(const int &n) {
    set<string> s;
    for (int i = 0; i < n-1; i++) {
        for (int j = i+1; j < n; j++) {
            s.insert(arr[i]+arr[j]);
            if (arr[i] != arr[j]) s.insert(arr[j] + arr[i]);
        }
    }
    cout << s.size() << endl;
}

void solve3(const int &n) {
    set<string> s;
    for (int i = 0; i < n-2; i++) {
        for (int j = i+1; j < n-1; j++) {
            for (int k = j+1; k < n; k++) {
                s.insert(arr[i]+arr[j]+arr[k]);
                s.insert(arr[i]+arr[k]+arr[j]);
                s.insert(arr[j]+arr[i]+arr[k]);
                s.insert(arr[j]+arr[k]+arr[i]);
                s.insert(arr[k]+arr[i]+arr[j]);
                s.insert(arr[k]+arr[j]+arr[i]);
            }
        }
    }
    cout << s.size() << endl;
}

void solve4(const int &n) {
    set<string> s;
    for (int i = 0; i < n-3; i++) {
        for (int j = i+1; j < n-2; j++) {
            for (int k = j+1; k < n-1; k++) {
                for (int l = k+1; l < n; l++) {

                    s.insert(arr[i]+arr[j]+arr[k]+arr[l]);
                    s.insert(arr[i]+arr[j]+arr[l]+arr[k]);
                    s.insert(arr[i]+arr[k]+arr[j]+arr[l]);
                    s.insert(arr[i]+arr[k]+arr[l]+arr[j]);
                    s.insert(arr[i]+arr[l]+arr[j]+arr[k]);
                    s.insert(arr[i]+arr[l]+arr[k]+arr[j]);

                    s.insert(arr[j]+arr[i]+arr[k]+arr[l]);
                    s.insert(arr[j]+arr[i]+arr[l]+arr[k]);
                    s.insert(arr[j]+arr[k]+arr[i]+arr[l]);
                    s.insert(arr[j]+arr[k]+arr[l]+arr[i]);
                    s.insert(arr[j]+arr[l]+arr[i]+arr[k]);
                    s.insert(arr[j]+arr[l]+arr[k]+arr[i]);

                    s.insert(arr[k]+arr[i]+arr[j]+arr[l]);
                    s.insert(arr[k]+arr[i]+arr[l]+arr[j]);
                    s.insert(arr[k]+arr[j]+arr[i]+arr[l]);
                    s.insert(arr[k]+arr[j]+arr[l]+arr[i]);
                    s.insert(arr[k]+arr[l]+arr[i]+arr[j]);
                    s.insert(arr[k]+arr[l]+arr[j]+arr[i]);

                    s.insert(arr[l]+arr[i]+arr[j]+arr[k]);
                    s.insert(arr[l]+arr[i]+arr[k]+arr[j]);
                    s.insert(arr[l]+arr[j]+arr[i]+arr[k]);
                    s.insert(arr[l]+arr[j]+arr[k]+arr[i]);
                    s.insert(arr[l]+arr[k]+arr[i]+arr[j]);
                    s.insert(arr[l]+arr[k]+arr[j]+arr[i]);

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


int main() {

    int n, m;
    for(; ;) {
        cin >> n;
        if (n == 0) break;
        cin >> m;
        for (int i = 0; i < n; i++) cin >> arr[i];
        if (m == 2) solve2(n);
        else if (m == 3) solve3(n);
        else if (m == 4) solve4(n);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <iomanip>

#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
const int inf = 1e9;
const ll linf = 1LL << 50;

int n, k;
int a[11];


int main(int argc, char const* argv[])
{
	while(true){
			cin >> n >> k;
			if(n == 0)break;
			vector<int> vec;
			for(int i = 0; i < n; i++){
					vec.pb(i);
					cin >> a[i];
			}
			set<ll> st;
			do{
					ll aa = 0;
					for(int i = 0; i < k; i++){
							if(a[vec[i]] > 9)aa = aa * 100;
							else aa = aa * 10;
							aa += a[vec[i]];
					}
					st.insert(aa);
			}while(next_permutation(vec.begin(), vec.end()));
			cout << st.size() << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
 
#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
 
#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}
 
 
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
 
int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
 
  int N, K;
while(
  cin >> N >> K,N){
  VI xs(N);
  REP(i,N) cin >> xs[i];
 
  SORT(xs);
  set<int> ans;
  do{
	int s = 0;
	REP(i,K){
	  s = s * (xs[i] >= 10? 100: 10) + xs[i];
	}
	ans.insert(s);
  }while(next_permutation(ALL(xs)));
 
  cout << SZ(ans) << endl;
 }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);   

    int n,k;

    
    while(cin >> n >> k){
        if(!n && !k) break;

        int card[n];
        rep(i,n) cin >> card[i];

        sort( card, card + n );

        int ans = 0;
        map<int,int> memo;
        do{
            int temp = 0;
            rep(i,k){
                temp *= 10;
                if(card[i]>9) temp *= 10;
                temp += card[i];
            }
            if(memo[temp] == 0) ans++;
            memo[temp] = 1;
        }while(next_permutation( card, card + n ));

        cout << ans << endl;
    }

    return 0;
}