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<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define EPS 1e-8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = (j); i < (k); i++)
typedef long long ll;
typedef pair < int , int > Pii;
vector < string > vs;
map <string , int > memo;
int n,k;
void solve(int dep,string str,int F){
	if(dep == n){
		int cnt = 0;
		rep(i,n)if(F>>i&1)cnt++;
		if(cnt==k)memo[str] = 1;
		return ;
	}
	int cnt = 0;
	rep(i,n){
		if(!(F>>i&1)){
			string sub=str;
			sub += vs[i];
			F = F|1<<i;
			solve(dep+1,sub,F);
			F = F&~(1<<i);
			solve(dep+1,str,F);
		}
		
	}
	return ;
}
int main(){
	while(scanf("%d%d",&n,&k),n){
		memo.clear();
		vs.clear();
		rep(i,n){
			string tmp;
			cin >> tmp;
			vs.push_back(tmp);
		}
		rep(i,n){
			int F = 0;
			F = 1<<i;
			solve(1,vs[i],F);
		}
		printf("%d\n",memo.size());
	}
	return 0;
}

C++: #include<iostream>
#include <stdlib.h>

using namespace std;

int main()
{
	int n, k, result[100], i, p, a, b, c, d, e;
	char card[100][3];
	char number[10000][9];

	for (i = 0;; i++)
	{
		cin >> n;
		cin >> k;

		a = 0;
		b = 0;
		c = 0;
		d = 0;
		e = 0;
		p = 0;

		if (n == 0 && k == 0)
		{
			break;
		}

		for (int a = 0; a < n; a++)
		{
			cin >> card[a];
		}

		if (k == 2)
		{
			result[i] = n*(n - 1);
			for (a; a < n*n; a++)
			{
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				c++;
			}
			for (int a = 0; a < n*n - 1; a++)
			{
				if (atoi(number[a]) != 0)
				{
					for (int b = a + 1; b < n*n; b++)
					{
						if (atoi(number[a]) == atoi(number[b]))
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}

		else if (k == 3)
		{
			result[i] = n*(n - 1)*(n - 2);
			for (int a = 0; a < n*n*n; a++)
			{
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && c != d && d != b)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				d++;
			}
			for (int a = 0; a < n*n*n - 1; a++)
			{
				if (atoi(number[a]) != 0)
				{
					for (int b = a + 1; b < n*n*n; b++)
					{
						if (atoi(number[a]) == atoi(number[b]))
						{
							number[a][0] = *"0";
							number[a][1] = *"";
							result[i]--;
							break;
						}
					}
				}
			}
		}

		else if (k == 4)
		{
			result[i] = n*(n - 1)*(n - 2)*(n - 3);
			for (int a = 0; a < n*n*n*n; a++)
			{
				if (e >= n)
				{
					e = 0;
					d++;
				}
				if (d >= n)
				{
					d = 0;
					c++;
				}
				if (c >= n)
				{
					c = 0;
					b++;
				}
				if (b >= n)
				{
					break;
				}
				else
				{
					if (b != c && b != d && b != e && c != d && c != e && d != e)
					{
						for (int f = 0; f < 3; f++)
						{
							if (card[b][f] != *"")
							{
								number[a][p] = card[b][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[c][f] != *"")
							{
								number[a][p] = card[c][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[d][f] != *"")
							{
								number[a][p] = card[d][f];
								p++;
							}
							else
							{
								break;
							}
						}
						for (int f = 0; f < 3; f++)
						{
							if (card[e][f] != *"")
							{
								number[a][p] = card[e][f];
								p++;
							}
							else
							{
								number[a][p] = *"";
								p = 0;
								break;
							}
						}
					}
					else
					{
						number[a][0] = *"0";
						number[a][1] = *"";
					}
				}
				e++;
			}
		}
		for (int a = 0; a < n*n*n*n - 1; a++)
		{
			if (atoi(number[a]) != 0)
			{
				for (int b = a + 1; b < n*n*n*n; b++)
				{
					if (atoi(number[a]) == atoi(number[b]))
					{
						number[a][0] = *"0";
						number[a][1] = *"";
						result[i]--;
						break;
					}
				}
			}
		}
	}

	for (int a = 0; a < i; a++)
	{
		cout << result[a] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<set>
#define MAX 30
#include<string>
#include<vector>
using namespace std;
int main(){
	int n;
	while(cin>>n,n){
		int k;
		cin>>k;
		vector<string> vec;
		set<string> st;
		for(int i=0;i<n;i++){
			string s;
			cin>>s;
			vec.push_back(s);
		}
		sort(vec.begin(),vec.end());
//		cout<<"---"<<endl;
		do{
			string t="";
			for(int i=0;i<k;i++){
				t+=vec[i];
			}
			st.insert(t);
//			cout<<"------"<<endl;
		}while(next_permutation(vec.begin(),vec.end()));
		cout<<st.size()<<endl;
	}
	return 0;
}