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>
using namespace std;
int a[10001];
int c[11];
int n,k;
int top=0;
bool used[11];
void DFS(int x,int dept){
	if(dept==k){
		bool t=true;
		for(int i=0;i<top;i++){
			if(a[i]==x)t=false;
		}
		if(t){
			a[top]=x;
			top++;
		}
		return;
	}
	for(int i=0;i<n;i++){
		if(!used[i]){
			used[i]=true;
			if(c[i]>=10)DFS(x*100+c[i],dept+1);
			else DFS(x*10+c[i],dept+1);
			used[i]=false;
		}
	}
}
int main(){
	while(true){
	cin>>n>>k;
	if(n==0&&k==0)break;
	for(int i=0;i<n;i++){
		cin>>c[i];
		used[i]=false;
	}
	DFS(0,0);
	cout<<top<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;

int main(){
    int n, k, C[100];
    int checked_num[1000000]={};
    while(1){
        cin >> n >> k;
        if(n==0 && k==0) break;
        for(int i=0; i<n; i++)
            cin >> C[i];
        sort(begin(C), begin(C)+n);

        // 全ての組み合わせを考える
        do{
            string num="";
            for(int i=0; i<k; i++)
                num+=to_string(C[i]);
            checked_num[stoi(num)]=1;
        }while(next_permutation(C, C+n));

        int ans=0;
        for(int i=0; i<1000000; i++)
            ans+=checked_num[i];
        cout << ans << endl;

        memset(C, 0, 100*sizeof(int));
        memset(checked_num, 0, 1000000*sizeof(int));
    }
    return 0;
}

C++: #include <stdio.h>
#include <string.h>

int N, K;
char A[11][3];
char tmp[10], cards[211][10];
bool ans_flag = true;
int ans = 0;

int main(void) {
	while (true) {
		ans = 0;
		scanf("%d%d", &N, &K);
		if (N == 0 && K == 0) { break; }
		for (int i = 0; i < N; ++i) { scanf("%s", &A[i]); }

		if (K == 2) {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = i+1; j < N; ++j) {
					strcat(tmp, A[j]);
					for (int k = 0; k < ans; ++k) {
						if (strcmp(tmp, cards[k]) == 0) {
							ans_flag = false;
							break;
						}
					}
					if (ans_flag == true) {
						ans++;
						strcpy(cards[ans - 1], tmp);
					}
					ans_flag = true;
					memset(tmp, '\0', sizeof(tmp));
				}
			}
		}
		else if (K == 3) {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = i+1; j < N; ++j) {
					strcat(tmp, A[j]);
					for (int k = j+1; k < N; ++k) {
						strcat(tmp, A[k]);
						for (int l = 0; l < ans; ++l) {
							if (strcmp(tmp, cards[l]) == 0) { ans_flag = false; break; }
						}
						if (ans_flag == true) {
							ans++;
							strcpy(cards[ans - 1], tmp);
						}
						ans_flag = true;
						memset(tmp, '\0', sizeof(tmp));
					}
				}
			}
		}
		else {
			for (int i = 0; i < N; ++i) {
				strcat(tmp, A[i]);
				for (int j = i+1; j < N; ++j) {
					strcat(tmp, A[j]);
					for (int k = j+1; k < N; ++k) {
						strcat(tmp, A[k]);
						for (int l = k+1; l < N; ++l) {
							strcat(tmp, A[l]);
							for (int m = 0; m < ans; ++m) {
								if (strcmp(tmp, cards[m]) == 0) { ans_flag = false; break; }
							}
						}
						if (ans_flag == true) {
							ans++;
							strcpy(cards[ans - 1], tmp);
						}
						ans_flag = true;
						memset(tmp, '\0', sizeof(tmp));
					}
				}
			}
		}

		printf("%d\n", ans);
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, p;
	while (cin >> n >> p) {
		if (n == 0 && p == 0) {
			break;
		}
		vector<string> hyou(n);
		for (int i = 0; i < n; i++) {
			cin >> hyou[i];
		}
		set<string> t;
		for (int i = 0; i < n; i++) {
			string b = hyou[i];
			for (int j = 0; j < n; j++) {
				if (i == j) {
					continue;
				}
				string c = b+hyou[j];
				if (p > 2) {
					for (int k = 0; k < n; k++) {
						if (i == k || j == k) {
							continue;
						}
						string d = c+hyou[k];
						if (p > 3) {
							for (int l = 0; l < n; l++) {
								if (i == l || j == l || k == l) {
									continue;
								}
								string e = d+hyou[l];
								if (t.find(e) == t.end()) {
									t.insert(e);
								}
							}
						} else {
							if (t.find(d) == t.end()) {
								t.insert(d);
							}
						}
					}
				} else {
					if (t.find(c) == t.end()) {
						t.insert(c);
					}
				}
			}
		}
		cout << t.size() << endl;
	}
	return 0;
}

C++: #include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;

map<string,bool> m;
int n,k;
string a[20];

bool f[20];
void dfs(string str,int dep){
    if(dep == k){

    m[str] = true;
        return;
    }
    for(int i = 0;i < n;i++){
        if(f[i] == false){
            f[i] = true;
            dfs(str + a[i],dep+1);
            f[i] = false;
        }
    }
}
int main(void){

    while(cin>>n>>k,k){
        for(int i = 0;i < n;i++){
            cin>>a[i];
        }
        dfs("",0);
        int cnt = 0;
        map<string,bool>::iterator it = m.begin();
        while( it != m.end() )
        {
            //cout << (*it).first << ":" << (*it).second << endl;
            ++it;
            cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;
const int MOD = 1e9 + 7;
 


int n, k;
set<string> st;
string s;
vi v;

void calc(int c, vi u){
	if(c == k){
		do{
			string t;
			for(int i = 0; i < k; i++){
				t += to_string(u[i]);
			}
			st.insert(t);
		}while(next_permutation(all(u)));
		return;
	}
	for(int i = c; i + k - c <n; i++){
		u.pb(v[i]);
		calc(c+1, u);
		u.pop_back();
	}
}


int main(){
	while(cin>>n>>k){
		if(n == 0 && k == 0) break;
		v = vi(n);
		rep(i, n) cin>>v[i];
		s = "";
		st.clear();
		vi tmp;
		calc(0,tmp);
		cout<<st.size()<<endl;
	}
}