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

int main()
{
  fast_io();
  int n, k;
  while (cin >> n >> k, n){
    vector<int> v;
    inpt(v, n);
    sort(v);
    set<int> s;
    do {
      stringstream ss;
      for (int i = 0; i < k; i++){
        ss << v[i];
      }
      int x;
      ss >> x;
      s.insert(x);
    } while (next_permutation(v.begin(), v.end()));
    cout << s.size() << "\n";
  }
  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<stdio.h>
#include<string>
#include<string.h>
#include<set>
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++: #include<iostream>
#include<string>
#include<bitset>
#include<set>
using namespace std;
int N, K;

string cards[100];
set<string> ans;

void dfs(int k, bitset<100> c, string s)
{
  if(k == K)
  {
    ans.insert(s);
    return;
  }
  for(int i = 0 ;i < N; i++)
  {
    if(!c[i])
      continue;
    c[i] = false;
    dfs(k + 1, c, s + cards[i]);
    c[i] = true;
  }
  return;
}

int main()
{
  while(1)
  {
    ans.clear();
    cin >> N >> K;
    if(!N && !K)
      break;
    for(int i = 0; i < N; i++)
      cin >> cards[i];
    dfs(0, bitset<100>(0xFFFF), "");
    cout << ans.size() << endl;
  }
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
#include <map>
#include <set>
using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

int n,k;
string v[20];
vector<bool> used;
set<string> S;

void dfs(int cnt, string s) {
    if(cnt==k) {
        S.insert(s);
        return;
    } else {
        rep(i,n) {
            if(!used[i]){
                used[i] = 1;
                dfs(cnt+1, s+v[i]);
                used[i] = 0;
            }
        }
    }
}

int main() {
    while(cin>>n>>k && n) {
        S.clear();
        used.assign(n,false);
        rep(i,n)cin>>v[i];
        dfs(0,"");
        cout << S.size() << endl;
    }
}

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

using namespace std;
int main()
{

	vector<long> p;
	set < long > w;
	int n, k;
	string m[1000];
	string a;
	int e;
	int count = 0;
	long  y;
	int f = 0;
	while (1){
		w.clear();
		count = 0;
		cin >> n >> k;
		if (n == 0)return 0;
		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];
								y = atol(a.c_str());
								w.insert(y);
								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];

							y = atol(a.c_str());
							w.insert(y);
							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];
						y = atol(a.c_str());
						w.insert(y);
						a.clear();
					}
				}
			}
		}
		cout << w.size() << endl;
		
	}
}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, save4[10000], 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;
		}
	}
}