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

int bit(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

int main()
{
	int n,k;
	string str;
	while( cin >> n >> k && n || k ){
		set<string> ret;
		vector<string> num(n);
		for(int i=0;i<n;i++){
			cin >> num[i];
		}
		sort(num.begin(),num.end());
		for(int mask=0;mask < (1<<n); mask++){
			if(bit(mask)!=k)continue;
			vector<string> uuu;
			for(int j=0;j<n;j++){
				if( mask & (1<<j) )uuu.push_back(num[j]);
			}

			do{
				str="";
				for(int i=0;i<k;i++){
					str+=uuu[i];
				}
				ret.insert(str);
			}while( next_permutation(uuu.begin(),uuu.end()) );
		}
		cout << ret.size() << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a*b/gcd(a,b);
}

signed main(void) {
    int n,k;
    while(cin >> n >> k, n){
        vi a(n);
        rep(i,n) cin >> a[i];
        set<string> st;
        if(k == 2){
            rep(i,n)rep(j,n)if(i-j){
                st.insert(to_string(a[i]) + to_string(a[j]));
            }
        }
        if(k == 3){
            rep(i,n)rep(j,n)rep(k,n)if(i-j && j-k && i-k){
                st.insert(to_string(a[i]) + to_string(a[j]) + to_string(a[k]));
            }
        }
        if(k == 4){
            rep(i,n)rep(j,n)rep(k,n)rep(l,n)if(i-j && j-k && i-k && i-l && j-l && k-l){
                st.insert(to_string(a[i]) + to_string(a[j]) + to_string(a[k]) + to_string(a[l]));
            }
        }
        cout << st.size() << endl;
    }
}

C++: #include<stdio.h>
#include<algorithm>
#include<sstream>
#include<vector>
#include<set>
using namespace std;

int n, k;
char card[10];
set<int>ans;
void solve(int a, vector<char>vec, vector<bool>b) {
	vec.push_back(card[a]);
	b[a] = true;
	if (vec.size() == k) {
		ostringstream oss;
		for (char i : vec)
			oss << (int)i;
		ans.insert(stoi(oss.str()));
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!b[i])
			solve(i, vec, b);
	}
}
int main(){
	ans.clear();
	scanf("%d%d", &n, &k);
	if (!(n | k))return 0;
	for (int i = 0; i < n; i++)
		scanf("%d", &card[i]);
	vector<char>vec;
	vector<bool>b(10);
	for (int i = 0; i < n; i++)
		solve(i, vec, b);
	printf("%d\n", ans.size());
	main();
	return 0;
}

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

int toInt(string s){
  int ret = 0;
  for(int i = 0 ; i < s.size() ; i++){
    ret *= 10;
    ret += s[i] -'0';
  }
  return ret;
}

int main(){
  int n, k;
  while(cin >> n >> k){
    if(n == 0 && k == 0) break;
    string s;
    vector<string> data;
    for(int i = 0 ; i < n ; i++){
      cin >> s;
      data.push_back(s);
    }
    sort(data.begin(), data.end());
    set<int> res;
    do{
      string t = "";
      for(int i = 0 ; i < k ; i++){
	t += data[i];
      }
      res.insert(toInt(t));
    }while(next_permutation(data.begin(), data.end()));
     
    cout << res.size() << endl;
  }
  return 0;
}

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

int main()
{
	int n, k;
	while (cin >> n >> k) {
		vector<string> ss(n);
		for (int i = 0; i < n; i++) {
			cin >> ss[i];
		}
		set<int> st;
		if (k == 2) {
			for (int i = 0; i < n; i++) {
				for (int j = i + 1; j < n; j++) {
					st.insert(stoi(ss[i] + ss[j]));
					st.insert(stoi(ss[j] + ss[i]));
				}
			}
		}
		else if (k == 3) {
			for (int i = 0; i < n; i++) {
				for (int j = i + 1; j < n; j++) {
					for (int k = j + 1; k < n; k++) {
						st.insert(stoi(ss[i] + ss[j] + ss[k]));
						st.insert(stoi(ss[i] + ss[k] + ss[j]));
						st.insert(stoi(ss[j] + ss[i] + ss[k]));
						st.insert(stoi(ss[j] + ss[k] + ss[i]));
						st.insert(stoi(ss[k] + ss[i] + ss[j]));
						st.insert(stoi(ss[k] + ss[j] + ss[i]));
					}
				}
			}
		}
		else {
			for (int i = 0; i < n; i++) {
				for (int j = i + 1; j < n; j++) {
					for (int k = j + 1; k < n; k++) {
						for (int l = k + 1; l < n; l++) {
							vector<int> val = { i, j, k, l };
							do {
								st.insert(stoi(ss[val[0]] + ss[val[1]] + ss[val[2]] + ss[val[3]]));
							} while (next_permutation(val.begin(), val.end()));
						}
					}
				}
			}
		}
		cout << st.size() << endl;
	}
	return 0;
}