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 <vector>
#include <algorithm>
using namespace std;

int n, k;
int a[10];
int v[10000] = {};
int s;

void set_vec(int t, int f, int sum) {
	if(t>=k) {
		v[s] = sum;
		s++;
	}else {
		for(int i=0;i<n;i++) {
			if((1<<i) & f)continue;
			set_vec(t+1, f+(1<<i), sum*(a[i]<10?10:100)+a[i]);
		}
	}
}

int main() {
	
	scanf("%d", &n);
	scanf("%d", &k);
	
	while(n==0 && k==0) {
		s = 0;
		for(int i=0;i<n;i++) {
			scanf("%d", &a[i]);
		}
		
		set_vec(0, 0, 0);
		
		sort(v, v+s);
		
		int t = 0;
		int sum = 0;
		
		for(int i=0;i<s;i++) {
			if(t==v[i])continue;
			sum++;
			t=v[i];
		}
		
		printf("%d\n", sum);
		
		scanf("%d", &n);
		scanf("%d", &k);
	}
	
	return  0;
}

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


int main()
{
	while(true) {
		int N, K;
		cin >> N >> K;

		if(N == 0 && K == 0) break;

		vector<string> vs(N);
		for(int i = 0; i < N; i++) cin >> vs[i];

		sort(vs.begin(), vs.end());

		set<string> ss;
		do ss.insert(accumulate(vs.begin(), vs.begin() + K, string()));
		while (next_permutation(vs.begin(), vs.end()));

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

C++: #include "stdio.h"
#include <vector>
#include <queue>
#include <set>

using namespace std;

int x[10];

int main()
{
int n,k;
scanf("%d",&n);
scanf("%d",&k);
for(int i = 0; i < n; i++)
{
scanf("%d",&x[i]);
}

int w = 1;
for(int i = 0; i < k; i++)
{
w *= n;
}

set<int> sw;
for(int i = 0; i < w; i++)
{
priority_queue<int> q1;
vector<int> q2;
int s = i;
for(int i = 0; i < k; i++)
{
int l = s % n;
q1.push(l);
q2.push_back(l);
s /= n;
}

int flgw = 0;
int mae = -1;
for(int ii = 0; ii < k; ii++)
{
if(mae == q1.top())
{
flgw = 1;
break;
}
mae = q1.top();
q1.pop();
}
if(flgw == 1)
{
continue;
}

int g = 0;
for(int ii = 0; ii < k; ii++)
{
if(x[q2[ii]] < 10)
{
g = g * 10 + x[q2[ii]];
}
else
{
g = g * 100 + x[q2[ii]];
}
}
sw.insert(g);
}

printf("%d\n",sw.size());
return 0;
}

C++: #include<iostream>

using namespace std;

int main()
{
	int n, k, result[100], i, p, a, b, c, d, e;
	char card[10][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++)
						{
							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] = *"";
					}
				}
			}
		}
		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<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,k;
string s[11];
int main(){
  while(cin>>n>>k,n){
    set<string>se;
    r(i,n)cin>>s[i];
    sort(s,s+n);
    do{
      string x;
      r(i,k)x+=s[i];
      se.insert(x);
    }while(next_permutation(s,s+n));
    cout<<se.size()<<endl;
  }
}