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 "stdio.h"
#include <vector>
#include <queue>
#include <set>

using namespace std;

int x[10];

int main()
{
while(1)
{
int n,k;
scanf("%d",&n);
scanf("%d",&k);
if(n == 0 && k == 0)
{
break;
}
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 <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main() {
	int n, k;
	cin >> n >> k;
	vector<string> s(n);
	set<string> st;
	rep(i, 0, n){
		cin >> s[i];
	}
	sort(all(s));
	do{
		string tmp = "";
		rep(i, 0, k){
			tmp += s[i];
		}
		//cout << tmp <<endl;
		if(st.count(tmp)) continue;
		st.insert(tmp);
	}while(next_permutation(all(s)));
	cout << st.size() <<endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <numeric>
#include <climits>
#include <iterator>
#include <iomanip>
#include <stack>
#include <set>
#include <cstdio>
#include <bitset>
#include <functional>
using namespace std;
//typedef std::pair<std::string,double> P;

#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

typedef long long ll;
typedef vector<int> VI;

struct Less {
	bool operator()(const pair<int, int>& x, const pair<int, int>& y) const {
		return x.first > y.first;
	}
};
//グラフの隣接リスト
//VI g[200010];
//頂点の入次数を管理
int h[100010];

bool g[101][101];

//int dxy[5] = { -1, 0, 1, 0, -1 };

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, k; 
    while(cin >> n >> k, n||k){
	vector<string> v(n);
	REP(i, n) cin >> v[i];
	
	sort(v.begin(), v.end());
	set<string> se;
	do {
        string s="";
		for(int i=0; i<k; ++i) s += v[i];
        se.insert(s);
		
	} while (next_permutation(v.begin(), v.end()));
	
	cout << se.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;
        }
        
    }

}







int main(){
    
    while(true){

    cin >> n >> k;
    if (n==0&&k==0){
        break;
    }
    for(int i=1;i<=n;i++){
        cin >> integer[i];
    }

    operation();

    cout << integer_count << endl;
        
    n={};
    k={};
    for(int i=0;i<=10;i++){
        integer[i]={};
        chousen[i]={};
    }
    num={};
    for(int i=0;i<=9999;i++){
        complete_integer[i]={};
    }
    answer={};
    integer_count=0;
        
    }
    return 0;
}

C++: #include <cstdio>
#include <string>
#include <set>
#include <iostream>
using namespace std;
int main(){
  int n,type;
  string s[10];
  set<string>data;
  while(cin >> n >> type && n||type){
    for(int i = 0;i<n;i++) cin >> s[i];
    if(type==2) for(int i=0;i<n;i++)
               for(int j=0;j<n;j++)
                 if(i!=j) data.insert(data.end(),s[i]+s[j]);
    if(type==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) data.insert(data.end(),s[i]+s[j]+s[k]);
    if(type==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) data.insert(data.end(),s[i]+s[j]+s[k]+s[l]);
    cout << data.size() << endl;
    data.clear();
  }
}

C++: #include <iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int def(vector<int> s) {
	int si = s.size();
	if (si <= 1) {
		return 1;
	}
	else {
		int x = s[si - 1];
		int d = 0;
		for (int i = 0; i < si - 1; i++) {
			if (s[i] == x) {
				d = 1;
			}
		}
		if (d == 1) {
			return 0;
		}
		else {
			vector<int> t(si - 1);
			for (int i = 0; i < si - 1; i++) {
				t[i] = s[i];
			}
			return def(t);
		}
	}
}
int rui(int a, int b) {
	if (b == 0) {
		return 1;//aのb
	}
	else {
		return rui(a, b - 1)*a;
	}
}
int ke(int a, int b) {
	if (b < 10) {
		return a * 10 + b;
	}
	else {
		return a * 100 + b;
	}
}
int main()
{
	int az = 0;
	while (az == 0) {
		int n, k; int ans = 0;
		cin >> n >> k;
		if (n*n + k * k == 0) {
			az = 1;
		}
		else {
			vector<int> su(rui(n,k),0);
			int siz = 0;
			vector<int> ca(n);
			for (int i = 0; i < n; i++) {
				cin >> ca[i];
			}
			for (int i = 0; i < rui(n, k); i++) {
				vector<int> na(k);
				int j = i;
				for (int x = 0;  x< k; x++) {
					na[x] = j % n;
					j = j / n;
				}
				if (def(na) == 1) {
					int si = 0;
					for (int x = 0; x < k; x++) {
						si = ke(si, ca[na[x]]);
					}
					su[siz] = si;
					siz++;
				}
			}
			sort(su.begin(), su.end());
			reverse(su.begin(), su.end());
			int ba = -1;
			for (int i = 0; i < siz; i++) {
				if (ba != su[i]) {
					ans++;
					ba = su[i];
				}
			}
			cout << ans << endl;
		}
	}
}

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

int n, k;
set<string> po;
string s[10];

int main()
{
  while (cin >> n >> k, n) {
    po.clear();
    for (int i = 0; i < n; i++){
      cin >> s[i];
    }
    sort(s, s + n);
    do {
      string uyu = "";
      for (int i = 0; i < k; i++){
        uyu += s[i];
      }
      po.insert(uyu);
    } while (next_permutation(s, s + n));

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