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<set>
#include<string>
 
int n, k;
std::string cards[11];
std::set<std::string>s;
 
void makecard(int p, std::string res, bool used[11]){
    if (p == k){
        s.insert(res);
        return;
    }
 
    for (int i = 0; i < n; i++){
        if (!used[i]){
            used[i] = true;
            makecard(p + 1, res + cards[i], used);
            used[i] = false;
        }
    }
}
 
int main(){
 
    while (std::cin >> n >> k, n || k){
        s.clear();
        for (int i = 0; i < n; i++){
            std::cin >> cards[i];
        }
        bool used[11];
        for (int j = 0; j < 11; j++)used[j] = false;
 
        makecard(0, "", used);
 
        std::cout << s.size() << std::endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define Rep(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define Repr(i, b, e) for(int (i) = (b) - 1; (i) >= (e); --(i))
#define Print(x) cout << (x) << endl 
#define Min2(a, b) ((a) < (b)) ? (a) : (b)
#define Max2(a, b) ((a) > (b)) ? (a) : (b)

typedef long long ll;

using namespace std;

int N, K;
string C[10];
set<string> bt;

void dfs(string str, int k, int state){
    if(k == 0){
        bt.insert(str);
        return;
    }
    Rep(i, 0, N){
        if((1 << i) & state) continue;
        dfs(str + C[i], k - 1, state | (1 << i));
    }
}

int main(){
    while(true){
        cin >> N >> K;
        if(N == 0 && K == 0) break;
        Rep(i, 0, N) cin >> C[i];
        dfs("", K, 0);
        Print(bt.size());
        bt.clear();
    }
    return 0;
}

C++: // Didn't see editorial
#include <iostream>
#include <queue>
#include <iomanip>
#include <map>
#include <algorithm>

using namespace std;

using ll = long long;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main() {
    while(true) {
        int N, K;
        cin >> N >> K;
        if (N == 0) return 0;

        vector<int> R(N);
        int ans = 0;
        map<int, bool> appeared;
        for (int i = 0; i < N; i++) cin >> R[i];

        for (int i = 0; i < (1<<N); i++) {
            vector<int> cards;
            for (int k = 0; k < N; k++) {
                if ((i>>k) & 1) cards.push_back(R[k]);
            }
            if (cards.size() != K) continue;
            sort(cards.begin(), cards.end());

            do {
                string joined = "";
                for (auto val : cards) {
                    joined += to_string(val);
                }
                if (!appeared[stoi(joined)]) {
                    appeared[stoi(joined)] = true;
                    ans++;
                }
            } while (next_permutation(cards.begin(), cards.end()));
        }
        cout << ans << endl;
    }
}

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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;

#define TT template<typename T>
#define ET(T) typename T::value_type
TT inline T sq(T x){return x*x;}
TT inline T In(){T x;cin>>x;return x;}
TT inline int ubnd(T&v,ET(T) x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT inline int lbnd(T&v,ET(T) x){return lower_bound(begin(v),end(v),x)-begin(v);}
TT inline void sort(T&v){sort(begin(v),end(v));}
TT inline void revs(T&v){reverse(begin(v),end(v));}
TT inline void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT inline void inpt(T&v,int n,function<ET(T)()>f=In<ET(T)>){for(v.reserve(n);n--;v.emplace_back(f()));}
TT inline void show(T&v,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),cout<<x;cout<<e;}
#undef TT
#undef ET

inline void fast_io(){ios::sync_with_stdio(0); cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline string instr(){string x;cin>>x;return x;}
inline ll pow_mod(ll a, ll k, ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a, ll p){return pow_mod(a,p-2,p);}

const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000;

struct Solver {
  vector<int> v;
  set<int> s;
  int n, k;
  bool used[10];

  Solver(int n, int k) : n(n), k(k) { fill(used, used + 10, false); }
  void read(){ inpt(v, n); }
  void solve(int ki=0, int a=0){
    if (ki == k){
      s.insert(a);
      return;
    }
    for (int i = 0; i < n; i++){
      if (used[i]) continue;
      int ai = a;
      for (int x = 1; x <= v[i]; x *= 10) ai *= 10;
      ai += v[i];
      used[i] = true;
      solve(ki + 1, ai);
      used[i] = false;
    }
  }
};

int main()
{
  int n, k;
  while (cin >> n >> k, n){
    Solver s(n, k);
    s.read();
    s.solve();
    cout << s.s.size() << endl;
  }
  return (0);
}

// priority_queue queue deque front top push pop map set stringstream max_element min_element insert count find get tie make_tuple

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

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

string to_string(int n)
{
	char buf[3];
	sprintf(buf,"%d",n);
	return buf;
}

int main()
{
	for(int n,k;cin>>n>>k,n|k;){
		vi a(n);
		rep(i,n)
			cin>>a[i];
		sort(allof(a));
		set<string> res;
		do{
			string s;
			rep(i,k)
				s+=to_string(a[i]);
			res.insert(s);
			reverse(k+allof(a));
		}while(next_permutation(allof(a)));
		cout<<res.size()<<endl;
	}
	
	return 0;
}