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<iostream>
#include<string>
#include<set>
using namespace std;
int main() {
	int n, k; while (cin >> n >> k) {
		if (!n && !k)return 0;
		string s[11];
		set<string>st;
		for (int i = 0; i < n; i++)cin >> s[i];
		if (k == 2) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (i == j)continue;
					st.insert(s[i] + s[j]);
				}
			}
		}
		else if (k == 3) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (i == j)continue;
					for (int p = 0; p < n; p++) {
						if (i == p || j == p)continue;
						st.insert(s[i] + s[j] + s[p]);
					}
				}
			}
		}
		else if (k == 4) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (i == j)continue;
					for (int p = 0; p < n; p++) {
						if (i == p || j == p)continue;
						for (int q = 0; q < n; q++) {
							if (i == q || j == q || p == q)continue;
							st.insert(s[i] + s[j] + s[p] + s[q]);
						}
					}
				}
			}
		}
		cout << st.size() << endl;
	}
}

C++: #include<vector>
#include<map>
#include<string>
#include<iostream>
//#include<numeric>
#include<sstream>
#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;
		stringstream ss;
		for(int i=0;i<k;i++)ss<<v[i];
		m[ss.str()]=1;
	}while(next_permutation(v.begin(),v.end()));
	cout<<m.size()<<endl;
}}

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

using namespace std;

int count = 0;

struct Card
{

  bool flag;
  string number;

};

int LiningUpTheCards(int k, Card card, vector<Card> &bunch_card , vector<Card> &kind_num)
{

  int dummy;
  int start;

  if(k == 0)
    {

      bool flag = true;

      for(int i = 0; i < kind_num.size(); ++i )
          if(!kind_num[i].number.compare(card.number))
            {
              flag = false;
              break;
            }

      if(flag)
        {
          kind_num.push_back(card);
          count++;
        }

      return 0;

    }

  else{

    for(int j = 0; j < bunch_card.size(); ++j)
      {

        if(bunch_card[j].flag)
          {

            bunch_card[j].flag = false;
            start = card.number.length();
            card.number += bunch_card[j].number;
            dummy = LiningUpTheCards(k-1,card,bunch_card,kind_num);
            bunch_card[j].flag = true;
            card.number.erase(start);

          }

      }

  }

  return 0;

}

int main()
{

  int k;
  int n;
  int dummy;
  Card input;
  vector<Card> kind_num;
  vector<Card> bunch_card;

  input.flag = true;

  cin >> n;
  cin >> k;

  while(n != 0 || k != 0)
    {

      for(int i = 0; i < n; ++i)
        {
          cin >> input.number;
          bunch_card.push_back(input);
        }

      input.number.erase();

      dummy = LiningUpTheCards(k,input,bunch_card,kind_num);

      kind_num.clear();
      bunch_card.clear();

      cout << count << endl;

      cin >> n;
      cin >> k;

      count = 0;

    }

}

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

int main() {
	int a, b;
	while (cin >> a >> b, a | b) {
		vector<string>c(a);
		set<string>s;
		for (int d = 0; d < a; d++)cin >> c[d];
		switch (b) {
		case 2:
			for (int e = 0; e < a; e++) {
				for (int f = 0; f < a; f++) {
					if (e != f)s.insert(c[e] + c[f]);
				}
			}break;
		case 3:
			for (int e = 0; e < a; e++) {
				for (int f = 0; f < a; f++) {
					for (int g = 0; g < a; g++) {
						if (e != f&&f != g&&g != e)s.insert(c[e] + c[f] + c[g]);
					}
				}
			}break;
		case 4:for (int e = 0; e < a; e++) {
			for (int f = 0; f < a; f++) {
				for (int g =0; g < a; g++) {
					for (int h = 0; h < a; h++) {
						if (e != f&&f != g&&g != h&&h != e&&e != g&&f != h)s.insert(c[e] + c[f] + c[g] + c[h]);
					}
				}
			}
		}break;
		}
		cout << s.size() << endl;
	}
}

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

using namespace std;

int main(){
  int n,k;
  while(scanf("%d%d",&n,&k),n+k){
    int card[n];
    set<int> s;
    for(int i=0; i<n; i++)
      scanf("%d",&card[i]);
    vector<int> v(n);
    for(int i=0; i<k; i++)
      v[n-k+i] = i+1;
    do{
      int a[k];
      for(int i=0; i<n; i++){
	if(card[i]!=0)
	  a[v[i]-1]=card[i];
      }
      int nn = 0;
      for(int i=0; i<k; i++){
	int tmp = a[i];
	int keta = 1;
	while(tmp!=0){
	  keta*=10;
	  tmp/=10;
	}
	nn*=keta;
	nn+=a[i];
      }
      s.insert(nn);
    }while(next_permutation(v.begin(), v.end()));
    cout << s.size() << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, save4[100000], roop2;
bool save[10000000];
string card[10], res, save2, save5[10000000];
void solve(int a){
	roop--;
	for (int i = 0; i<n; i++){
		if(i==a) continue;
		if (save3 != i){
			if (roop == 0){
				save2 = res;
				res += card[i];
				num = stoi(res);
				if (save[num] == 0){
					ans++;
				}
				save[num] = 1;
				save[roop2] = num;
				roop2++;
				res = save2;
			}
			else{
				save5[i]=res;
				res += card[i];
				solve(i);
				roop++;
				res = save5[i];
			}
		}
	}
}
int main() {
	while (true){
		cin >> n >> k;
		roop2 = 0;
		if (n == 0 && k == 0){
			return 0;
		}
		for (int i = 0; i < n; i++){
			cin >> card[i];
		}
		for (int i = 0; i < n; i++){
			roop = k - 1;
			res = card[i];
			save3 = i;
			solve(i);
		}
		cout << ans << endl;
		for (int i = 0; i < 10000000; i++){
			if (save[save4[i]] == 0)
				break;
			save[save4[i]] = 0;
		}
	}
}

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

int count(int n)
{
int ret = 1;
for(int mul = 10; mul <= n; mul *= 10) ++ret; 
return ret;
}

void add(int k, int sum, vector<int> &c, vector<int> &result)
{
if(k == 0) {
result.push_back(sum);
return;
}
for(int i = 0; i < c.size(); ++i) {
if(c[i] == 0)
continue;
int tmp = c[i];
c[i] = 0;
add(k - 1, sum * count(tmp) + tmp, c, result);
c[i] = tmp;
}
return;
}

int main()
{
while(true) {
int n, k;
vector<int> c;
vector<int> result;
scanf("%d%d", &n, &k);
if(n == 0)
break;
c.resize(n);
for(int i = 0; i < n; ++i) 
scanf("%d", &c[i]);
add(k, 0, c, result);
sort(result.begin(), result.end());
result.remove(unique(result.begin(), result.end()), result.end());
printf("%d\n", result.size());
}
return 0;
}