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 <algorithm>
#include <set>
#include <cmath>
using namespace std;

int n,k;
int a[11];
int b[4];

int main()
{
	int x,sum;
	while(1){
		set<int> s;
		cin >> n >> k;
		if(n==0&&k==0)break;
		for(int i = 0; i < n; ++i){
			cin >> a[i];
		}
		sort(a,a+n);
		do{
			x = 0;
			sum = 0;
			for (int i = 0; i < k; ++i){
				b[i] = a[i];
				x += (int)log10(b[i])+1;
			}
			for (int i = 0; i < k; ++i){
				sum += b[i] * (int)pow(10,x - ((int)log10(b[i]) + 1));
				x -= ((int)log10(b[i]) + 1);
			}
			s.insert(sum);
		}while(next_permutation(a,a+n));
		cout << s.size() << endl;
	}
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int n, k, card[10];
int used[10];
set<long long> s;

long long toInt() {
	long long res = 1;
	int a[4];
	REP(i, n) if (used[i] > 0) a[used[i]-1] = card[i];
	REP(i, k) {
		if (a[i] < 10) {
			res = res*10 + a[i];
		} else {
			res = res*10 + a[i]/10;
			res = res*10 + a[i]%10;
		}
	}
	return res;
}

void select(int cnt) {
	if (cnt == k) {
		s.insert(toInt());
		return;
	}
	REP(i, n) {
		if (used[i] == 0) {
			used[i] = cnt + 1;
			select(cnt + 1);
			used[i] = 0;
		}
	}
}

int main() {
	while (true) {
		s.erase(s.begin(), s.end());
		scanf("%d %d", &n, &k);
		if (n == 0 && k == 0) break;
		REP(i, n) scanf("%d", &card[i]);
		select(0);
		cout << s.size() << endl;
	}

	return 0;
}

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 <iostream>
#include <string>
using namespace std;
int save[10000000], n, k, ans = 0, roop, num, save3, save4[10000000], roop2;
string card[10], res, save2, save5[10000];
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 <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
#include <set>
using namespace std;

int main(){
  int n, k;
  while(cin >> n >> k){
    if(n == 0 && k == 0) break;
    string s;
    vector<string> data;
    for(int i = 0 ; i < n ; i++){
      cin >> s;
      data.push_back(s);
    }
    sort(data.begin(), data.end());
    set<int> res;
    do{
      int t = 0;
      for(int i = 0 ; i < k ; i++){
	for(int j = 0 ; j < data[i].size() ; j++){
	  t *= 10;
	  t += data[i][j] - '0';
	}
      }
      res.insert(t);
    }while(next_permutation(data.begin(), data.end()));
    
    cout << res.size() << endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  int n,k;
  while(scanf("%d%d",&n,&k),n+k){
    int card[n];
    set<int> s;
    for(int i=0; i<n; i++)
      scanf("%d",&card[i]);
    vector<int> v(n);
    for(int i=0; i<k; i++)
      v[n-k+i] = i+1;
    do{
      int a[k];
      for(int i=0; i<n; i++){
	if(card[i]!=0)
	  a[v[i]-1]=card[i];
      }
      int nn = 0;
      for(int i=0; i<k; i++){
	int tmp = a[i];
	int keta = 1;
	while(tmp!=0){
	  keta*=10;
	  tmp/=10;
	}
	nn*=keta;
	nn+=a[i];
      }
      s.insert(nn);
    }while(next_permutation(v.begin(), v.end()));
    cout << s.size() << endl;
  }
  return 0;
}