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;

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<bits/stdc++.h>

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
int n,k;
bool used[11]={false};
str s[11];
bool memo[11][11];
set<str> ans;
void f(int num,str st){
    if(num==k){
        ans.insert(st);
        return;
    }else{
        rep(i,0,n){
            if(!used[i]){
                used[i]=true;
                f(num+1,st+s[i]);
                used[i]=false;
            }
        }
        return;
    }
}
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(1){
    ans.clear();
    cin >> n;
    cin >> k;
    if(n==0&&k==0)break;
    rep(i,0,n)cin >> s[i];
    f(0,"");
    cout << ans.size() << endl;
    }
    /*for(str k:ans){
        cout << k << endl;
    }*/
    return 0;
}

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

#define INF (1<<29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str)
{
    if (K == 0) {
        st.insert(str);
    }
    
    int N = s.size();
    for (int i = 0; i < N; i++) {
        if (S >> i & 1) continue;
        solve(K-1, S|(1<<i), s, str + s[i]);
    }
}

int main()
{
    int N, K;
    while (cin >> N, N) {
        vector<string> s(N);
        cin >> K;
        for (int i = 0; i < N; i++) {
            cin >> s[i];
        }
        st.clear();
        solve(K, 0, s, "");
        cout << st.size() << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1<<29
#define MAX 11
using namespace std;

int a[MAX],s[MAX];

void solve(){
 int n,k;
 for(;;){
  cin>>n;
  cin>>k;
  if(!n&&!k) break;
  set<string> S;
  string ss;
  FOR(i,n) cin>>a[i];
  FOR(i,1<<n){
   if(__builtin_popcount(i)!=k) continue;
   int p=0;
   FOR(j,n) if((1<<j)&i) s[p++]=j;
   vector<int> ord(k);
   FOR(j,k) ord.pb(j);
   do{
    ss="";
    FOR(j,k) ss+=to_string(a[s[ord[j]]]);
    S.insert(ss);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

int main(){
 solve();
 return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, k;
int a[10];
int v[10000] = {};
int s;

void set_vec(int t, int f, int sum) {
	if(t>=k) {
		v[s] = sum;
		s++;
	}else {
		for(int i=0;i<n;i++) {
			if((1<<i) & f)continue;
			set_vec(t+1, f+(1<<i), sum*(a[i]<10?10:100)+a[i]);
		}
	}
}

int main() {
	
	while(1) {
		scanf("%d", &n);
		scanf("%d", &k);
		if(n==0 && k==0)break;
		s = 0;
		for(int i=0;i<n;i++) {
			scanf("%d", &a[i]);
		}
		
		set_vec(0, 0, 0);
		
		sort(v, v+s);
		
		int t = 0;
		int sum = 0;
		
		for(int i=0;i<s;i++) {
			if(t==v[i])continue;
			sum++;
			t=v[i];
		}
		
		printf("%d\n", sum);
		
	}
	
	return  0;
}

C++: #include<iostream>
#include<map>
#include<string>

int n, k;
std::string a[10];
std::map<std::string, int> m;

void rec(int i, int S, std::string str){//i個選んで今strという文字列ができている(Sは選んだ集合)
	if(i == k){
		m[str]++;
		return;
	}

	for(int j=0;j<n;j++){
		if(!(S >> j & 1)){//使っていない
			rec(i + 1, S | (1 << j), str + a[j]);
		}
	}
}

int main(){
	while(std::cin >> n >> k, n){
		m.clear();

		for(int i=0;i<n;i++){
			int j;
			std::cin >> j;
			a[i] = std::to_string(j);
		}

		rec(0, 0, "");

		std::cout << m.size() << std::endl;
	}
}