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<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;
		//http://origin.googlecode.com/svn-history/r366/trunk/core/include/origin/algorithm/boost_combination.hpp
		if(v.begin()==v.begin()+k)break;
		reverse(v.begin()+k,v.end());
	}while(next_permutation(v.begin(),v.end()));
	cout<<m.size()<<endl;
}}

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

#define INF (1<<29)

set<string> st;

void solve(int K, int S, vector<string> &s, string str)
{
    if (K == 0) {
        st.insert(str);
        return;
    }
    
    int N = s.size();
    for (int i = 0; i < N; i++) {
        if (S >> i & 1) continue;
        solve(K-1, S|(1<<i), s, str + s[i]);
    }
}

int main()
{
    int N, K;
    while (cin >> N, N) {
        vector<string> s(N);
        cin >> K;
        for (int i = 0; i < N; i++) {
            cin >> s[i];
        }
        st.clear();
        solve(K, 0, s, "");
        cout << st.size() << endl;
    }
    return 0;
}

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

int n={},k={};
string integer[11]={};
vector<string>num={};
int chousen[11]={};
string complete_integer[10000]={};
string answer={};
int integer_count=0;


void operation(){
    for(int i=1;i<=n;i++){
        if (chousen[i]==0){
            
            chousen[i]=1;
            num.push_back(integer[i]);
        
            if(num.size()==k){
                for(int i=1;i<=k;i++){
                    answer+=num[i-1];
                }
                for(int i=1;i<=integer_count;i++){
                    if(answer==complete_integer[i]){
                        goto none;
                    }
                }
                integer_count+=1;
                complete_integer[integer_count]=answer;

                none:;
                answer={};
            }else{
                operation();
            }
            num.pop_back();
            chousen[i]=0;
        }
        
    }

}

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

set<string> res;
string c[10];
bool use[10];
int n,k;
string ans;

void rec(int a){
  string tmp;
  if(!a){
    res.insert(ans);
    return;
  }
  for(int i=0;i<n;i++){
    if(!use[i]){
      use[i] = true;
      tmp = ans;
      ans += c[i];
      rec(a-1);
      ans = tmp;
      use[i] = false;
    }
  }
}

int main(){
  while(1){
    cin >> n;
    cin >> k;
    if(!n && !k)break;

    for(int i=0;i<n;i++)cin >> c[i];
    for(int i=0;i<n;i++)use[i] = false;

    res.clear();
    rec(k);

    cout << res.size() << endl;
  }
}

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

using namespace std;

int main()
{
	int n, k, a[11], b[11];
	while(1)
	{
		set<int> s;
		cin >> n >> k;
		if(n==0 && k==0) break;
		else
		{
			for(int i=0; i<n; i++)
			{
				cin >> a[i];
				if(a[i]>9) b[i]=2;
				else b[i]=1;
			}
			for(int i=0; i<n; i++)
			{
				int x=0;
				x=a[i];
				x*=10;
				for(int j=0; j<n; j++)
				{
					int y=x;
					if(j==i) continue;
					if(b[j]==2) y*=10;
					y+=a[j];
					if(k==2) s.insert(y);
					y*=10;
					for(int l=0; l<n; l++)
					{
						int z=y;
						if(l==i || l==j) continue;
						if(b[l]==2) z*=10;
						z+=a[l];
						if(k==3) s.insert(z);
						z*=10;
						for(int m=0; m<n; m++)
						{
							int w=z;
							if(m==i || m==j || m==l) continue;
							if(b[m]==2) w*=10;
							w+=a[m];
							if(k==4) s.insert(w);
						}
					}
				}
			}
			cout << s.size() << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
using namespace std;
int num;
int opt;
vector<string> data;
vector<string> combi;
void dfs(int deep,string smem,vector<int> mem)
{
	if(deep<opt){
		for(unsigned int f1=0;f1<data.size();f1++){
			for(unsigned int f2=0;f2<mem.size();f2++){
				if(mem[f2]==f1){
					goto Label;
				}
			}
			mem.push_back(f1);
			dfs(deep+1,smem+data[f1],mem);
			mem.pop_back();
			Label:;
		}
	}
	else{
		combi.push_back(smem);
	}
}
int main(void)
{
	while(1){
		data.clear();
		combi.clear();
		scanf("%d",&num);
		scanf("%d",&opt);
		if(num==0&&opt==0)return 0;
		for(int f1=0;f1<num;f1++){
			string sbuf;
			cin>>sbuf;
			data.push_back(sbuf);
		}
		//
		string smem;
		vector<int> dm;
		dfs(0,smem,dm);
		//
		for(unsigned int f1=0;f1<combi.size()-1;f1++){
			for(unsigned int f2=f1+1;f2<combi.size();f2++){
				if(combi[f1]==combi[f2]){
					combi.erase(combi.begin()+f2);
					f2--;
				}
			}
		}
		printf("%d\n",combi.size());	
	}
	return 0;
}

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

map<string,bool> mp;

int n,k;
string c[10];
bool used[10];

void dfs(int num,string s){
	if(num == k){
		mp[s] = true;
		return;
	}
	for(int i = 0;i < n;i++){
		if(!used[i]){
			used[i] = true;
			dfs(num + 1,s + c[i]);
			used[i] = false;
		}
	}
}

int main(){
	while(cin >> n >> k,n){
		mp.clear();
		for(int i = 0;i < n;i++) cin >> c[i];
		dfs(0,"");
		cout << mp.size() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
int main() {
	int n, k;
	while (scanf("%d%d",&n,&k)) {
		if (n==0&&k==0) break;
		vector<string> str(n);
		for (int i=0; i<n; i++) cin>>str[i];
		set<string> res;
		if (k==2) {
			for (int i=0; i<n; i++)
				for (int j=0; j<n; j++)
					if (i!=j) res.insert(str[i]+str[j]);
		}
		if (k==3) {
			for (int i=0; i<n; i++)
				for (int j=0; j<n; j++)
					for (int k=0; k<n; k++)
						if (i!=j&&i!=k&&j!=k) res.insert(str[i]+str[j]+str[k]);
		}
		if (k==4) {
			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&&i!=k&&i!=l&&
								j!=k&&j!=l&&k!=l) res.insert(str[i]+str[j]+str[k]+str[l]);
		}
		printf("%d\n",res.size());
	}
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <string>
#include <functional>
#include <math.h>
#include <utility>
#define INF 100000000
using namespace std;
typedef pair<int,int> P;
set<string> st;
bool used[10];
string ans,card[10];
int n,k;
void rec(int k){
	string tmp;
	if(k==0){st.insert(ans);return;}
	for(int i=0;i<n;i++){
		if(used[i])continue;
		tmp=ans;
		ans+=card[i];
		used[i]=true;
		rec(k-1);
		ans=tmp;
		used[i]=false;
	}
}
int main()
{
	while(cin>>n>>k&&(n!=0||k!=0)){
		for(int i=0;i<n;i++){
			cin>>card[i];
			used[i]=false;
		}
		st.clear();
		rec(k);
		cout<<st.size()<<endl;
	}
}