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 <cstdio>
#include <set>
#define N_MAX 11

using namespace std;

int a[N_MAX];	// カードに書かれた数値を格納
std::set<int> S;// 生成された数を格納

// カードの数を連結する
int cat(int s, int t) {
	int pow = 1;
	while(pow <= t) pow *= 10;
	return s * pow + t;
}

int main() {

	int n, k;		// n個からk個選ぶ

	while(1) {
		scanf("%d%d", &n, &k);
		if(n == 0 && k == 0) break;

		for(int i = 0; i < n; i++)
			scanf("%d", a + i);
		// 生成される数の格納
		S.clear();

		if(k == 2) {
			for(int i = 0; i < n; i++) 
				for(int j = 0; j < n; j++)
					if(i != j) S.insert(cat(a[i], a[j]));
		} else if(k == 3) {
			for(int i = 0; i < n; i++) 
				for(int j = 0; j < n; j++)
					for(int t = 0; t < n; t++) 
						if(i != j && j != t && t != i) 
							S.insert(cat(a[i], cat(a[j], a[t])));
		} else {
			for(int i = 0; i < n; i++) 
				for(int j = 0; j < n; j++)
					for(int t = 0; t < n; t++) 
						for(int u = 0; u < n; u++)
						 	if(i != j && i != t && i != u && j != t && j != u && t != u) 
								S.insert(cat(a[i], cat(a[j], cat(a[t], a[u]))));
		}
		printf("%d\n", S.size());
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
	while(true){
	string c[11];
	int n,k;
	vector<string> num;
	int sum=0;
	cin >> n >> k;
	if(n==0 && k==0)
	break;
	for(int i=1;i<=n;i++)
	cin >> c[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			for(int l=(k>=3 ? 1 : 0);l<=(k>=3 ? n : 0);l++)
				for(int ii=(k>=4 ? 1 : 0);ii<=(k>=4 ? n : 0);ii++)
					if(((ii!=i && ii!=j && ii!=l )|| ii==0)&&((l!=i && l!=j) || l==0) && (j!=i))num.push_back(c[i]+c[j]+c[l]+c[ii]);
	sort(num.begin(),num.end());
	for(int i=0;i<num.size();i++){

		if(i+1==num.size() || num[i]!=num[i+1]){
			sum++;
		}
	}
	cout << sum << endl;
}
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <set>
#include <vector>
using namespace std;

constexpr int MAX_N = 10;

int N, K;
int cards[MAX_N];

vector<int> vec;
set<int> numbers;
bool used[MAX_N];

void dfs(int n) {
  if (n == K) {
    ostringstream oss;
    for (int i : vec) {
      oss << i;
    }
    numbers.insert(stoi(oss.str()));
    return;
  }

  ++n;
  for (int i{}; i < N; ++i) {
    if (used[i])
      continue;
    used[i] = true;
    vec.push_back(cards[i]);
    dfs(n);
    vec.pop_back();
    used[i] = false;
  }
}

int main() {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    for (int n{}; n < N; ++n) {
      cin >> cards[n];
    }

    vec.clear();
    numbers.clear();
    dfs(0);
    cout << numbers.size() << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<set>
using namespace std;
int n,k,i,ans;
set<string> se;
string str[11];
void dfs(int j,string sum,int bit){
  if(j == k) {
    if(se.find(sum) == se.end()){
      se.insert(sum);
      ans++;
    }
  }
  else {
    for(int i = 0;i < n;i++){
      if(((1<<i) & bit)==0){
	dfs(j+1,sum+str[i],bit|(1<<i));
      }
    }
  }
}
int main(){
  cin >> n >> k;
  while(n != 0 || k != 0){
    for(i = 0;i < n;i++){
      cin >> str[i];
    }
    ans = 0;
    dfs(0,"",0);
    cout << ans << endl;
    cin >> n >> k;
    se.clear();
  }
  return(0);
}

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

using namespace std;

set<int> se;

int N,K;

int card[10];
bool used[10];

void rec(int num,int k)
{
	if(k==K)
	{
		se.insert(num);
		return;
	}
	for(int i=0;i<N;i++)
	{
		if(used[i]==true) continue;
		used[i]=true;
		int nnum;
		if(card[i]<10) nnum=num*10+card[i];
		else nnum=num*100+card[i];
		rec(nnum,k+1);
		used[i]=false;
	}
}

int main()
{
	scanf("%d%d",&N,&K);
	for(int i=0;i<N;i++)
	{
		scanf("%d",card+i);
	}
	memset(used,false,sizeof(used));
	rec(0,0);
	printf("%d\n",se.size());
	return 0;
}

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

void dfs(string now,int cou,int before);
int n,k;
vector<string> data;
map<string,int> used;
bool flg[15];


int main(){
  while(cin >> n >> k && n && k){
    data.clear();
    used.clear();
    for(int i=0;i<n;i++){
      string str; cin >> str;
      data.push_back(str);
    }
    for(int i=0;i<n;i++){
      memset(flg,true,n+1);
      dfs(data[i],0,i);
    }
    cout << used.size() << endl;
  }
}

void dfs(string now,int cou,int before){
  if(cou == k-1) {
    if(used[now]==0){
      used[now] = 1;
    }
    return;
  }
  flg[before] = false;
  for(int i=0;i<n;i++){
    if(flg[i]){
      string tmp=now;
      tmp += data[i];
      dfs(tmp,cou+1,i);
      tmp = data[i];
      tmp += now;
      dfs(tmp,cou+1,i);
    }
  }
  return;
}

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

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

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() {
	scanf("%d", &n);
	scanf("%d", &k);
	
	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", t);
	}
	
	printf("%d\n", sum);
	
	return  0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, save3[10], roop2, card[10], res, save2;
bool save[100000000];
void solve(){
	roop--;
	for (int i = 0; i<n; i++){
		if(save3[i]==1) continue;
			if (roop == 0){
				save2 = res;
				if(card[i]/10>=1){
					res = res*100;
				}
				else{
					res = res*10;
				}
				res += card[i];
				if (save[res] == 0){
					ans++;
					save[res]=1;
				}
				res = save2;
			}
			else{
				if(card[i]/10>=1){
					res = res*100;
				}
				else{
					res = res*10;
				}
				res += card[i];
				save3[i]=1;
				solve();
				save3[i]=0;
				roop++;
				res -= card[i];
				if(card[i]/10>=1){
					res = res/100;
				}
				else{
					res = res/10;
				}
		}
	}
}
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] = 1;
			solve();
			save3[i]=0;
		}
		cout << ans << endl;
		ans=0;
		for (int i = 0; i < 10000000; i++){
			save[i] = 0;
		}
	}
}