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<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int N,K;
        string str[11];
        string lis[10021];
        cin>>N>>K;
        if(N==0&&K==0)break;
        for(int i=0;i<N;i++)cin>>str[i];
        int ii=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                for(int k=0;k<N;k++){
                    for(int l=0;l<N;l++){
                        if(i!=j&&j!=k&&k!=l&&i!=k&&j!=l&&i!=l){
                            if(K==2)lis[ii]=str[i]+str[j];
                            if(K==3)lis[ii]=str[i]+str[j]+str[k];
                            if(K==4)lis[ii]=str[i]+str[j]+str[k]+str[l];
                            ii++;
                        }
                    }
                }
            }
        }
        sort(lis,lis+10012);
        int ans=-1;
        for(int i=0;i<10015;i++)if(lis[i]!=lis[i+1])ans++;
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

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);
}

int main() {
	while (1) {
		int N; cin >> N; int K; cin >> K;
		if (!N)break;
		vector<string>sts;
		for (int i = 0; i < N; ++i) {
			string st; cin >> st;
			sts.emplace_back(st);
		}
		vector<int>nums(N);
		iota(nums.begin(), nums.end(), 0);
		set<string>anss;
		do {
			string ans;
			for (int i = 0; i < K; ++i) {
				ans += sts[nums[i]];
			}
			anss.emplace(ans);

		} while (next_permutation(nums.begin(), nums.end()));
		cout << anss.size() << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<set>

using namespace std;

int main(void){
	int a,b,c,d;
	int n,k;
	while(1){
		cin >> n;
		cin >> k;
		if(n==0 && k==0)break;
		vector<string> num;
		set<string> str;
		for(a=0;a<n;a++){
			string s;
			cin >> s;
			num.push_back(s);
		}
		if(k==2){
			for(a=0;a<n;a++){
				for(b=0;b<n;b++){
					if(a!=b)str.insert(num[a]+num[b]);
				}
			}
		}
		if(k==3){
			for(a=0;a<n;a++){
				for(b=0;b<n;b++){
					if(a!=b)for(c=0;c<n;c++){
						if(a!=c && b!=c)str.insert(num[a]+num[b]+num[c]);
					}
				}
			}
		}
		if(k==4){
			for(a=0;a<n;a++){
				for(b=0;b<n;b++){
					if(a!=b)for(c=0;c<n;c++){
						if(a!=c && b!=c)for(d=0;d<n;d++){
							if(a!=d && b!=d && c!=d)str.insert(num[a]+num[b]+num[c]+num[d]);
						}
					}
				}
			}
		}
		cout << str.size() << endl;
	}
	return 0;
}

C++: #include<vector>
#include<map>
#include<string>
#include<iostream>
#include<numeric>
#include<algorithm>
using namespace std;
int main(){for(int n,k;cin>>n>>k,n;){
	vector<string> v;string s;
	for(;n;n--)cin>>s,v.push_back(s);
	map<string,int>m;
	sort(v.begin(),v.end());
	do{m[accumulate(v.begin(),v.begin()+k,string(),[](string x,string y){return x+y;})]=1;}while(next_permutation(v.begin(),v.end()));
	cout<<m.size()<<endl;
}}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(int a[],int b[],int d[],int p,int m,int n){
  int i,j,k;
  int t;
  if(p==m){
    t=a[b[0]];
    for(i=1;i<m;i++){
      k=0;
      for(j=0;j<i;j++){
	if(a[b[j]]>9)
	  k+=2;
	else
	  k++;
      }
      t+=a[b[i]]*pow(10,k);
    }
    for(i=0;d[i]!=0&&d[i]!=t;i++);
    if(d[i]==0)
      d[i]=t;
  }else{
    for(i=0;i<n;i++){
      for(j=0;j<p&&i!=b[j];j++);
      if(j==p){
	b[p]=i;
	f(a,b,d,p+1,m,n);
      }
    }
  }
  return;
}
int main(){
  int i;
  int a[10],b[4],d[10000],m,n;
  while(cin>>n>>m&&n+m){
    for(i=0;i<n;i++)
      cin>>a[i];
    memset(b,0,sizeof(b));
    memset(d,0,sizeof(d));
    f(a,b,d,0,m,n);
    for(i=0;d[i]!=0;i++);
    cout<<i<<endl;
  }
  return 0;
}

C++: /* AOJ 0546
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0546
 *
 */


//include
#include<bits/stdc++.h>

//using
using namespace std;

//define static const typedef
#define FOR(i, s, n) for(int (i) = (s); (i) < (n); (i)++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\n";
#define DEBUG(x) cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;

//global
static set<string> a;
static set<pair<string, VS> > e;
static int n, k, ans;
static VS str_array;
static char b[2];
//function
void solve(int many_used, string now_string, VS can_use){
  if(e.count(pair<string, VS>(now_string, can_use))){
      return;
  }
  e.insert(pair<string, VS>(now_string, can_use));
  if(k == many_used && !a.count(now_string)){
    ans++;
    a.insert(now_string);
    //DEBUG(now_string)
    return;
  }
  REP(i, can_use.size()){
    string str = can_use[i];
    can_use.erase(can_use.begin() + i);
    solve(many_used + 1, now_string + str, can_use);
    can_use.PB(str);
    SORT(can_use);
  }
}
int main(){
  while(scanf("%d %d", &n, &k) && n && k){
    REP(i, n){
      scanf("%s", b);
      str_array.PB((string)b);
    }
    ans = 0;
    SORT(str_array);
    solve(0, "", str_array);
    printf("%d\n", ans);
    str_array = VS();
    a = set<string>();
  }
  return 0;
}