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 <map>
#include <string>
using namespace std;

int main() {
	int n, k;
	while(cin >> n >> k && (n || k)){
		int card[10] = {0};
		map<string, int> m;
		for(int i = 0; i < n; ++i)
			cin >> card[i];
			
		int ans = 0;
		if(k == 2){
			for(int i = 0; i < n; ++i){
				for(int j  = 0; j < n; ++j){
					if(i == j)
						continue;
					string str = to_string(card[i]) + to_string(card[j]);
					if(m[str] == 0){
						ans++;
						m[str] = 1;
					}
				}
			}
		}
		else if(k == 3){
			for(int i = 0; i < n; ++i){
				for(int j  = 0; j < n; ++j){
					if(i == j)
						continue;
					for(int k = 0; k < n; ++k){
						if(i == k || j == k)
							continue;
						string str = to_string(card[i]) + to_string(card[j]) + to_string(card[k]);
						if(m[str] == 0){
							ans++;
							m[str] = 1;
						}
					}
				}
			}
		}
		else if(k == 4){
			for(int i = 0; i < n; ++i){
				for(int j  = 0; j < n; ++j){
					if(i == j)
						continue;
					for(int k = 0; k < n; ++k){
						if(i == k || j == k)
							continue;
						for(int l = 0; l < n; ++l){
							if(i == l || j ==l ||  k == l)
								continue;
							string str = to_string(card[i]) + to_string(card[j]) + to_string(card[k]) + to_string(card[l]);
				 			if(m[str] == 0){
								ans++;
								m[str] = 1;
							}
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, int> P;
typedef pair<ll, pair<ll, ll> > P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int n,k;
string s[15];
set<string> st;
bool used[15]{};

void dfs(int i, string t){
    if(i==0){
        st.insert(t);
    }
    REP(j,n){
        if(used[j])continue;
        used[j] = true;
        dfs(i-1,t+s[j]);
        used[j] = false;
    }
}

int main() {
    while(1){
        cin >> n >> k;
        if(n==0)break;
        REP(i,n)cin >> s[i];
        st.clear();
        fill(used,used+15,false);
        dfs(k,"");
        cout << st.size() << endl;
    }
    return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

std::string to_binString(unsigned int val)
{
  if( !val )
    return std::string("0");
  std::string str;
  while( val != 0 ) {
    if( (val & 1) == 0 )
      str.insert(str.begin(), '0');
    else
      str.insert(str.begin(), '1');
    val >>= 1;
  }
  return str;
}

vector<ll> v;
set<ll> s;
set<ll> bits;
void dfs(ll bit, ll t){
  if(find(all(bits),bit) != bits.end() || t < 0){
    return;
  }
  bits.insert(bit);
  if(t == 0){
    vector<ll> lis;
    rep(i,v.size()){
      if(bit & (1 << i)){
        lis.push_back(v[i]);
      }
    }
    ll cnt = 0;
    do{
      ll num = 0;
      rep(i,lis.size()){
//         cout << lis[i] << " ";
        if(lis[i] < 10){
          num = num * 10 + lis[i];
        }
        else{
          num = num * 100 + lis[i];
        }
      }
      if(s.insert(num).se){
      }
      ++cnt;
    }while(next_permutation(all(lis)));
//     debug(cnt);
  }
  else{
    REP(i,0,v.size()){
      if(!(bit & (1 << i))){
        dfs(bit | (1 << i),t-1);
      }
    }
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,k;
  while(cin >> n >> k && n && k){
    v.resize(n);
    s.clear();
    bits.clear();
    rep(i,n){
      cin >> v[i];
    }
    sort(all(v));
    rep(i,n){
      dfs((1 << i),k-1);
    }
    cout << s.size()<<endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>

using namespace std;

set<string>mo;
int N,k;
bool use[15];
map<string,int>memo;
string n[10];
void g(int m,string a){
    if(m==N){
        memo[a]=1;;
    return;
    }else {
        if(use[m]){
            use[m]=1;
            g(m+1,a+n[m]);
            use[m]=0;
        }
    }
}
int main(){
while(1){
        int ans=0;
        cin >> N >> k;
    if(N==0&&k==0)break;
    for(int y=0;y<N;y++){
        cin >> n[y];
    }g(N,"");
    cout << memo.size() << endl;
}

return 0;
}