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 <climits>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

#define str to_string
#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 11
#define MAX_ANS 99999999

int n, k;
int list[MAX_N];
int checked[MAX_ANS];

int solve() {
  rep(i, MAX_ANS) checked[i] = false;

  int ans = 0;
  do {
    int score = 0;
    rep(i, k) {
      int x = list[i];
      if (x < 10) score = score*10 + x;
      else        score = score*100 + x;
    }
    if (!checked[score]) {
      ans++;
      checked[score] = true;
    }
  } while (next_permutation(list, list+n));
  return ans;
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0) break;
    rep(i, n) cin >> list[i];
    sort(list, list+n);

    cout << solve() << '\n';
  }
  return 0;
}

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

using namespace std;

int n,k;
string s[1000];
char in[10000];
map<string,bool> Array;


int recroop(string ss,int counter,int index,bool used[]){
  if(counter > k)return 1;
  if(counter == k && !Array[ss]){
    //cout << ss << endl;
    Array[ss]=true; 
  }
  string press = ss;
  bool preused[n];
  for(int i=0;i<n;i++)preused[i]=used[i];

  for(int i=0;i<n;i++){
    if(i != index && !used[i]){
      ss+=s[i];
      used[i]=true;
      recroop(ss,counter+1,i,used);
      used[i]=preused[i];
      ss.clear();
      ss = press;
    }
  }



}


int main(){
  bool used[100];

  while(true){
    cin >> n >> k;
    if(n+k == 0)break;
    for(int i=0;i<100;i++)used[i]=false;
    for(int i=0;i<n;i++)cin >> s[i]; 

    recroop("",0,-1,used); 
      cout << Array.size() << endl;    
      Array.clear();
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

int n;
int k;
set<string> st;
string ss[11];

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	while(1){
		st.clear();
		cin >> n >> k;
		if(!n)break;
		for(int i=0;i<n;i++){
			cin >> ss[i];
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				for(int p=0;p<n;p++){
					for(int q=0;q<n;q++){
						if(k==2){
							if(i!=j){
								st.insert(ss[i]+ss[j]);
							}
						}
						else if(k==3){
							if(i!=j&&j!=p&&i!=p){
								st.insert(ss[i]+ss[j]+ss[p]);
							}
						}
						else{
							if(i!=j&&i!=p&&i!=q&&j!=p&&j!=q&&p!=q){
								st.insert(ss[i]+ss[j]+ss[p]+ss[q]);
							}
						}
					}
				}
			}
		}
		printf("%d\n",st.size());
	}
}

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

int n, k;
int array[10];

int c[4];
bool use[10];
char str[10000][100];
int m;

int number[10000];
int p;

void Search(int i, int j)
{
	if (j == k){
		for (int l = 0; l < k; l++){
			char nstr[3];
			sprintf(nstr, "%d", c[l]);
			strcat(str[m], nstr);
		}
		
		int num;
		sscanf(str[m], "%d", &num);
//		printf("%d\n", num);

		bool f = false;
		for (int l = 0; l < p; l++){
			if (number[l] == num){
				f = true;
				break;
			}
		}
		
		if (!f){
			number[p] = num;
			p++;
		}
		
		m++;
		return;
	}
	
	if (i == n){
		return;
	}
	
	Search(i + 1, j);
	
	if (use[i]){
		return;
	}
	
	c[j] = array[i];
	
	use[i] = true;
	
	Search(0, j + 1);
	
	use[i] = false;
}

int main()
{
	while (scanf("%d %d", &n, &k), n + k){
		memset(array, 0, sizeof(array));
		memset(c, 0, sizeof(c));
		memset(use, 0, sizeof(use));
		memset(str, 0, sizeof(str));
		memset(number, 0, sizeof(number));
		m = 0;
		p = 0;
		
		for (int i = 0; i < n; i++){
			scanf("%d", &array[i]);
		}
		
		Search(0, 0);
		
		printf("%d\n", p);
	}
	
	return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;
int k;
string str;
int main(){
	while(cin>>n>>k,n||k){
		map<string,int> ma;
		vector<string> num;
		rep(i,n){
			string d;
			cin>>d;
			num.push_back(d);
		}
		sort(num.begin(),num.end());
		for(int i = 0;i <1<<n;i++){//全探索最強!!ビットで集合表せるのすごくね?
			int cnt = 0;
			for(int j = 0;j < n;j++){
				if(i&(1<<j))cnt++;
			}
			if(cnt!=k)
				continue;
			vector<string> w;
			for(int j = 0;j < n;j++){
				if(i&(1<<j))
					w.push_back(num[j]);
			}
			do{
				string check;
				for(int j = 0;j < w.size();j++){
					check +=w[j];
				}
				ma[check] +=1;
			}while(next_permutation(w.begin(),w.end()));
			w.clear();
		}
		cout <<ma.size()<<endl;
		ma.clear();
		num.clear();
	}
	return 0;
}

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