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 <string>
#include <vector>
#include <set>
#include <algorithm>





using namespace std;



template < class BidirectionalIterator >
bool next_combination ( BidirectionalIterator first1 ,
  BidirectionalIterator last1 ,
  BidirectionalIterator first2 ,
  BidirectionalIterator last2 )
{
  if (( first1 == last1 ) || ( first2 == last2 )) {
    return false ;
  }
  BidirectionalIterator m1 = last1 ;
  BidirectionalIterator m2 = last2 ; --m2;
  while (--m1 != first1 && !(* m1 < *m2 )){
  }
  bool result = (m1 == first1 ) && !(* first1 < *m2 );
  if (! result ) {
    // テ「ツ堕?
    while ( first2 != m2 && !(* m1 < * first2 )) {
      ++ first2 ;
    }
    first1 = m1;
    std :: iter_swap (first1 , first2 );
    ++ first1 ;
    ++ first2 ;
  }
  if (( first1 != last1 ) && ( first2 != last2 )) {
    // テ「ツ堕。
    m1 = last1 ; m2 = first2 ;
    while (( m1 != first1 ) && (m2 != last2 )) {
      std :: iter_swap (--m1 , m2 );
      ++ m2;
    }
    // テ「ツ堕「
    std :: reverse (first1 , m1 );
    std :: reverse (first1 , last1 );
    std :: reverse (m2 , last2 );
    std :: reverse (first2 , last2 );
  }
  return ! result ;
}
 
template < class BidirectionalIterator >
bool next_combination ( BidirectionalIterator first ,
  BidirectionalIterator middle ,
  BidirectionalIterator last )
{
  return next_combination (first , middle , middle , last );
}


void push_string(vector<string>& ss, set<string>& hoge){
  do{
    string tmp="";
    for(int i=0; i<ss.size(); i++)
      tmp += ss[i];
    //    cout << tmp << "\n";
    hoge.insert(tmp);
  }while(next_permutation(ss.begin(),ss.end()));
}



int main(){
  while(1){
    int n,k; cin >> n >> k;
    if(n==0 && k==0) break;
    vector<string> ss;
    set<string> hoge;
    for(int i=0; i<n; i++){
      string x; cin >> x;
      ss.push_back(x);
    }
    sort(ss.begin(),ss.end());
    
    do{
      for(int i=0; i<k; i++){
	vector<string> tmp;
	for(int i=0; i<k; i++)
	  tmp.push_back(ss[i]);
	push_string(tmp,hoge);
      }
    }while(next_combination(ss.begin(), ss.begin()+k, ss.end()));
    cout << hoge.size() << "\n";
  }

}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, save4[10000000], roop2;
bool save[10000000]; 
string card[10], res, save2, save5[1000000];
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;
		}
	}
}

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

int main(){
    int n, k;
    while(cin >> n >> k){
        if(n + k == 0)  break;
        string x[n];
        for(int i = 0; i < n; i++)  cin >> x[i];
        sort(x, x+n);

        set<int> ans;
        do{
            string tmp = "";
            for(int i = 0; i < k; i++)  tmp += x[i];
            ans.insert(stoi(tmp));
        }while(next_permutation(x, x+n));

        cout << ans.size() << endl;
    }
    return 0;
}

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

int n,k;
string a[11];
char t[5];
set<string> ss;
string s;

int main()
{
	while(scanf(" %d%d",&n,&k),n|k){
		ss.clear();
		for(int i = 0; i < n; ++i){
			scanf(" %s ",t);
			a[i] = t;
		}
		sort(a,a+n);
		do{
			s.clear();
			for (int i = 0; i < k; ++i){
				s += a[i];
			}
			ss.insert(s);
		}while(next_permutation(a,a+n));
		printf("%d\n",(int)ss.size());
	}
}