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 <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int n, k;
  while(cin >> n >> k, n||k) {
    string a[n];
    REP(i,n) {
      cin >> a[i];
    }
    set<string> se;
    long long comb = (1<<k) - 1;
    while( comb < 1 << n ) {
      vector<int> hoge;
      REP(i,n)
        if (comb >> i & 1)
          hoge.push_back(i);
      sort(ALL(hoge));
      do {
        string s;
        REP(i,k)
          s += a[hoge[i]];
        //    cout << s << endl;
        se.insert(s);
      } while(next_permutation(ALL(hoge)));
      int x = comb&-comb, y = comb+x;
      comb = ((comb & ~y) / x >> 1) | y;
    }
    cout << se.size() << endl;
  }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int n={},k={};
string integer[11]={};
vector<string>num={};
int chousen[10]={};
string complete_integer[1000]={};
string answer={};
int integer_count=1;
bool check[100000000]={};
int counter=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];
                }
                complete_integer[integer_count]=answer;
                integer_count+=1;
                answer={};
            }else{
                operation();
            }
            num.pop_back();
            chousen[i]=0;
        }
        
    }

}



void compare(){
    for(int i=1;i<=integer_count-1;i++){
        //cout << complete_integer[i] << endl;
        if(check[stoi(complete_integer[i])]){
        }else{
            check[stoi(complete_integer[i])]=1;
            counter+=1;
        }
    }
}



int main(){
    
    while(true){


        
    cin >> n >> k;
    if (n==0&&k==0){
        break;
    }
    for(int i=1;i<=n;i++){
        cin >> integer[i];
    }
        
    operation();
    compare();
    cout<< counter << endl;
    n={};
    k={};
        for(int i=0;i<=9;i++){
            
            integer[i]={};
            chousen[i]={};
        }
    num={};
        for(int i=0;i<=999;i++){
            complete_integer[i]={};
        }
    answer={};
    integer_count=1;
        for(int i=0;i<=99999999;i++){
            check[i]={};
        }
    counter=0;
    
        
    }
    return 0;
}

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

int n, k;
int a[10];

bool used[10] = {false};
int b[10];
set<int> dict;

void dfs(int id) {
	if (id == k) {
		int num = 0;
		for (int i = 0; i < k; i++) {
			if (b[i] >= 10) num *= 100;
			else num *= 10;
			num += b[i];
		}
		dict.insert(num);
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			used[i] = true;
			b[id] = a[i];
			dfs(id + 1);
			used[i] = false;
		}
	}
}

int main() {
	while (cin >> n >> k) {
		if (!n) break;
		dict.clear();
		for (int i = 0; i < n; i++) cin >> a[i];
		dfs(0);
		cout << dict.size() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
int n, k;
int main(){
    while(cin >> n >> k, n||k){
        vector<string> v;
        for(int i=0; i<n; ++i){
            string st; cin >> st;
            v.push_back(st);
        }
        sort(v.begin(), v.end());
        vector<string> nums;
        do{
            string num="";
            for(int i=0; i<k; ++i){
                num += v[i];
            }
            nums.push_back(num);
        }while(next_permutation(v.begin(), v.end()) );
        sort(nums.begin(), nums.end());
        nums.erase(unique(nums.begin(), nums.end()), nums.end());
        cout << nums.size() << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <sstream>
using namespace std;
int main(){
    int n,k;
    map<string,bool> ret;
    while(cin >> n >> k){
        vector<int>data(n);
        for(int i=0;i<n;i++)
            cin >> data[i];
        sort(data.begin(),data.end());
        do{
            stringstream ss;
            for(int i=0;i<k;i++)
                ss << data[i];
            ret[ss.str()];
        }while(next_permutation(data.begin(), data.end()));
        cout << ret.size() << endl;
    }
}

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

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

vs v;
set<string> a;
bool used[10];
int n, m;
void bfs(string s, int num){
    if(m == num){
        a.insert(s);
    }
    else {
        rep(i, n){
            if(!used[i]){
                used[i] = true;
                bfs(s+v[i], num+1);
                used[i] = false;
            }
        }
    }
}

int main(){
    cin >> n >> m;
    rep(i, n){
        string s;
        cin >> s;
        v.pb(s);
    }
    fill(used, used+10, false);
    rep(i, n){
        used[i] = true;
        bfs(v[i], 1);
        used[i] = false;
    }
    cout << a.size() << endl;
}