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 <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

#define str to_string
#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 11

int n, k;
int list[MAX_N];

int solve() {

  int ans = 0;
  vector<int> v;
  do {
    int score = 0;
    rep(i, k) {
      int x = list[i];
      if (x < 10) score = score*10 + x;
      else        score = score*100 + x;
    }
    if (!binary_search(v.begin(), v.end(), score)) {
      ans++;
      v.push_back(score);
      sort(v.begin(), v.end());
    }
  } while (next_permutation(list, list+n));
  return ans;
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0) break;
    rep(i, n) cin >> list[i];
    sort(list, list+n);

    cout << solve() << '\n';
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < n ; i++)
int main(){
  int n, k;
  while(cin >> n >> k , n){
    vector< string > vc(n);
    for(int i = 0 ; i < n ; i++ ) cin >> vc[i];
    set< string > ret;
    if(k == 1){
      rep(i,n) ret.insert(vc[i]);
    }else if(k == 2){
      rep(i,n) rep(j,n) if(i!=j) ret.insert(vc[i]+vc[j]);
    }else if(k == 3){
      rep(i,n) rep(j,n) if(i!=j) rep(k,n) if(i!=k&&j!=k) ret.insert(vc[i]+vc[j]+vc[k]);
    }else if(k == 4){
      rep(i,n) rep(j,n) if(i!=j) rep(k,n) if(i!=k&&j!=k) rep(l,n) if(i!=l&&j!=l&&k!=l) ret.insert(vc[i]+vc[j]+vc[k]+vc[l]);
    }
    cout << ret.size() << endl;
  }
}

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

int main() {

    int n,k;
    
    while(1) {
        cin >> n >> k;
        
        if(n==0 && k==0) break;

        string a[n];
        string s;

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

        set<string> se;

        if (k == 4) {
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    for (int x = j + 1; x < n; x++) {
                        for (int y = x + 1; y < n; y++) {
                            s = a[i] + a[j] + a[x] + a[y];
                            se.insert(s);
                            s = a[i] + a[j] + a[y] + a[x];
                            se.insert(s);
                            s = a[i] + a[x] + a[j] + a[y];
                            se.insert(s);
                            s = a[i] + a[x] + a[y] + a[j];
                            se.insert(s);
                            s = a[i] + a[y] + a[j] + a[x];
                            se.insert(s);
                            s = a[i] + a[y] + a[x] + a[j];
                            se.insert(s);

                            s = a[j] + a[i] + a[x] + a[y];
                            se.insert(s);
                            s = a[j] + a[i] + a[y] + a[x];
                            se.insert(s);
                            s = a[j] + a[x] + a[i] + a[y];
                            se.insert(s);
                            s = a[j] + a[x] + a[y] + a[i];
                            se.insert(s);
                            s = a[j] + a[y] + a[i] + a[x];
                            se.insert(s);
                            s = a[j] + a[y] + a[x] + a[i];
                            se.insert(s);

                            s = a[x] + a[j] + a[i] + a[y];
                            se.insert(s);
                            s = a[x] + a[j] + a[y] + a[i];
                            se.insert(s);
                            s = a[x] + a[i] + a[j] + a[y];
                            se.insert(s);
                            s = a[x] + a[i] + a[y] + a[j];
                            se.insert(s);
                            s = a[x] + a[y] + a[j] + a[i];
                            se.insert(s);
                            s = a[x] + a[y] + a[i] + a[j];
                            se.insert(s);

                            s = a[y] + a[j] + a[x] + a[i];
                            se.insert(s);
                            s = a[y] + a[j] + a[i] + a[x];
                            se.insert(s);
                            s = a[y] + a[x] + a[j] + a[i];
                            se.insert(s);
                            s = a[y] + a[x] + a[i] + a[j];
                            se.insert(s);
                            s = a[y] + a[i] + a[j] + a[x];
                            se.insert(s);
                            s = a[y] + a[i] + a[x] + a[j];
                            se.insert(s);
                        }
                    }
                }
            }
        } else if (k == 3) {
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    for (int x = j + 1; x < n; x++) {
                        s = a[i] + a[j] + a[x];
                        se.insert(s);
                        s = a[i] + a[x] + a[j];
                        se.insert(s);
                        s = a[j] + a[i] + a[x];
                        se.insert(s);
                        s = a[j] + a[x] + a[i];
                        se.insert(s);
                        s = a[x] + a[i] + a[j];
                        se.insert(s);
                        s = a[x] + a[j] + a[i];
                        se.insert(s);
                    }
                }
            }
        } else {
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    s = a[i] + a[j];
                    se.insert(s);
                    s = a[j] + a[i];
                    se.insert(s);
                }
            }
        }

        cout << se.size() << endl;

    }


    return 0;
}