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<vector>
#include<map>

using namespace std;

int n,k,ans;
vector<string> V;
map<string,int> M;

void init(){
  V.clear();
  M.clear();
  ans = 0;
}

void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  if(rev == k && M[s] == 0){
    M[s] = 1;
    ans++;
    return;
  }

  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0)
      solve(s+V[i],rev+1,used+(1<<i));
  
}

int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

C++: //
//  main.cpp

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n,k,cards[11];
bool selected[11];
vector<vector<int>> selectlist;
vector<int> makelist;

void reset(){
	n = k = 0;
	selectlist.clear();
	makelist.clear();
	for(int i=0;i<11;i++){
		cards[i] = 0;
		selected[i] = false;
	}
}

void select_dfs_(int depth,int top,vector<int> vec){
	if(depth == k){
		selectlist.push_back(vec);
		return;
	}
	for(int i=top+1;i<n;i++){
		vec.push_back(cards[i]);
		select_dfs_(depth+1,i,vec);
		vec.pop_back();
	}
}

void makeint_dfs_(int depth,int makedint,vector<int> source){
	if(depth == k){
		makelist.push_back(makedint);
		return;
	}
	for(int i=0;i<k;i++){
		if(!selected[i]){
			selected[i] = true;
			int maked;
			if(source[i]/10 > 0){
				maked = makedint*100+source[i];
			}else{
				maked = makedint*10+source[i];
			}
			makeint_dfs_(depth+1,maked,source);
			selected[i] = false;
		}
	}
}

int main(){
	while(1){
		reset();
		
		cin >> n >> k;
		if(n == 0 && k == 0)break;
		for(int i=0;i<n;i++){
			cin >> cards[i];
		}
		
		for(int i=0;i<n-k+1;i++){
			vector<int> vec;
			vec.push_back(cards[i]);
			select_dfs_(1,i,vec);
		}
		
		for(int i=0;i<selectlist.size();i++){
			for(int j=0;j<selectlist[i].size();j++){
				selected[j] = true;
				makeint_dfs_(1,selectlist[i][j],selectlist[i]);
				selected[j] = false;
			}
		}
		
		sort(makelist.begin(),makelist.end());
		
		int answer=0,memo=-1;
		for(int i=0;i<makelist.size();i++){
			if(memo != makelist[i]){
				answer ++;
				memo = makelist[i];
			}
		}
		
		cout << answer << endl;
		
	}
	
	return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)

int n, k;
vector<string> cards;
set<string> ans;

void dfs(vector<int> s, set<int> used)
{
    if (s.size() == k)
    {
        string str = "";
        for (int i : s)
        {
            str += cards[i];
        }
        ans.emplace(str);
        return;
    }
    rep(i, n)
    {
        if (used.count(i) == 0)
        {
            vector<int> s2 = s;
            s2.push_back(i);
            set<int> used2 = used;
            used2.emplace(i);
            dfs(s2, used2);
        }
    }
}

int main()
{
    while (true)
    {
        ans = {};
        cin >> n >> k;
        if (n + k == 0)
        {
            break;
        }
        cards.resize(n);
        rep(i, n)
        {
            cin >> cards[i];
        }

        dfs({}, {});
        cout << ans.size() << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef vector <int> VI;
typedef set <int> SI;

int get_digit( int n )
{
	int count = 0;
	while ( n >= 10 )
	{
		n /= 10;
		count++;
	}
	return count + 1;
}

int ten_pow( int n )
{
	int answer = 1;
	for ( int i = 0; i < n; i++ ) answer *= 10;
	return answer;
}

int main( void )
{
	int n, k;
	while ( cin >> n >> k && n && k )
	{
		VI cards(n);
		for ( int i = 0; i < n; i++ ) cin >> cards[i];
		sort( cards.begin(), cards.end() );

		SI h;
		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;

			VI C;
			for ( int j = 0; j < n; j++ )
			{
				if ( i & 1 << j ) C.push_back( cards[j] );
			}

			do {
				int m = 0;
				for ( VI::iterator it = C.begin(); it != C.end(); it++ )
				{
					if ( m == 0 ) m += *it;
					else
					{
						m *= ten_pow( get_digit( *it ) );
						m += *it;
					}
				}
				h.insert( m );
			} while ( next_permutation( C.begin(), C.end() ) );
		}

		cout << h.size() << endl;

	}
	return 0;
}

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

using namespace std;

int arr[10];
int sub[10];
int n,k;
int now = 0;
int cont;
int c = 0;

set<int> visit2;

void saiki(int[],int);

int main()
{
	for(;;)
	{
		visit2.clear();
		int i;
		c = 0;
		now = 0;
		
		scanf("%d",&n);
		scanf("%d",&k);
		if(n == 0 && k == 0){
			break;
		}
		
		for(i = 0; i < n; i++){
			scanf("%d",&arr[i]);
		}
		for(i = 0; i < n; i++){
			sub[0] = arr[i];
	//		printf("\næªJ[hF%d\n",arr[i]);
			saiki(sub,1);
			now++;
		}
		cout <<visit2.size()-c << endl;
	}
}

void saiki(int temp[], int kk)
{
	//k:TõK
	int used[50] = {0};
	int i;
	int t = 0;
	char str[256] = "";
	
	for(i = 0; i < kk; i++){
		sprintf(str+strlen(str),"%d",temp[i]);
	}
	
	t = atoi(str);
	
	if(kk == k){
		
		visit2.insert(t);
		return;
	}
	else{ 
		int j;
		
//		cout << "*t = " << t << endl;
		
		for(i = 0; i < kk; i++){
			for(j = 0; j < n; ++j ){     
				if( used[j] == 0 && arr[j] == temp[i] ){
//					printf("J[h%dÍgpÏÝ\n",arr[j]);
					used[j] = 1;
					break;
				}
			}
		}
		
		for(i = 0; i < n; i++){
			if(used[i] == 0){
				temp[kk] = arr[i];
				used[i] = 1;
				saiki(temp, kk+1);
				used[i] = 0;
			}
		}
	}
}

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

int n,k,same,sum = 0;
string fig[11];
bool ju[11];
set <string> chk; 

void rec(int idx,string con){
  if(idx == k){
    if(chk.find(con) == chk.end()) chk.insert(con); 
    return;
  }
  for(int i=0;i<n;i++){
    if(ju[i]){
      ju[i] = false;
      rec(idx+1,con+fig[i]);
      ju[i] = true;
    }
  }
}


int main(){
  while(cin >> n >> k,n||k){
    memset(ju,true,sizeof(ju));
    for(int i=0;i<n;i++) cin >> fig[i];
    for(int i=0;i<n;i++){
      ju[i] = false;
      rec(1,fig[i]);
      ju[i] = true;
    }
    cout << chk.size() << endl;
    chk.clear();
  }
}