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>

using namespace std;

vector<string> v;

v_push(string str){
	bool checker;
	
	checker = true;
	for(int len = 0; len < v.size(); len++){
		if(v.at(len) == str){
			checker = false;
			break;
		}
	}
	if(checker){
		v.push_back(str);
	}
}

string str_int(int num){
	string str = "";
	if(num == 0){
		str = "0";
		return str;
	}
	while(true){
		if(num == 0){
			break;
		}
		string d = "D";
		d[0] = '0' + (num % 10);
		str = d + str;
		num /= 10;
	}
	return str;
}

int main(){
	
	int n, k;
	
	int num[20];
	
	while(true){
		
		cin >> n >> k;
		
		if(n == 0 && k == 0){
			break;
		}
		
		for(int i = 0; i < n; i++){
			cin >> num[i];
		}
		
		string str;
		
		if(k == 2){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j){
						continue;
					}else{
						str = str_int(num[i]) + str_int(num[j]);
						v_push(str);
					}
				}
			}
		}else if(k == 3){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					for(int k = 0; k < n; k++){
						if(i == j || i == k || j == k){
							continue;
						}else{
							str = str_int(num[i]) + str_int(num[j]) + str_int(num[k]);
							v_push(str);
						}
					}
				}
			}
		}else if(k == 4){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					for(int k = 0; k < n; k++){
						for(int l = 0; l < n; l++){
							if(i == j || i == k || j == k || i == l || j == l || k == l){
								continue;
							}else{
								str = str_int(num[i]) + str_int(num[j]) + str_int(num[k]) + str_int(num[l]);
								v_push(str);
							}
						}
					}
				}
			}
		}
		
		printf("%d\n", v.size());
		
		v.erase(v.begin(), v.end());
	}
	
	return 0;
}

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

using namespace std;

set<int> hash;
int card[10];

inline int digit(int n)
{
	if (n >= 10){
		return (100);
	}
	else {
		return (10);
	}
}

int sel_card(int cur, int n, int m)
{
	if (m == 0){
		hash.insert(cur);
		return (0);
	}
	
	for (int i = 0; i < n; i++){
		if (card[i] != -1){
			int tmp = card[i];
			card[i] = -1;
			sel_card(cur * digit(tmp) + tmp, n, m - 1);
			card[i] = tmp;
		}
	}
	
	return (0);
}


int main(void)
{
	int n, m;
	
	while (1){
		hash.clear();
		
		scanf("%d%d", &n, &m);
		
		if (n == 0 && m == 0){
			break;
		}
		
		for (int i = 0; i < n; i++){
			scanf("%d", &card[i]);
		}
		
		sel_card(0, n, m);
		
		printf("%d\n", hash.size());
	}
	
	return (0);
}

C++: #include <bits/stdc++.h>

using namespace std;

int N, K;
vector<int> CardsInfo;
vector<int> KOfCards;
set<int> Number;

void judge() {
	int num;
	stringstream ss;
	vector<int> temp(KOfCards);

	sort(temp.begin(), temp.end());
	do {
		for (int i = 0; i < K; ++i) {
			ss << temp[i];
		}
		ss >> num;
		ss.clear();
		//cout << "=" << num << endl;
		Number.insert(num);
	} while (next_permutation(temp.begin(), temp.end()));
	//cout << "curTotal:" << Number.size() << endl;
}

void findCombination(int index, int k) {
	
	if (k == K) {
	    /*for (auto p : KOfCards) {
			cout << p << " ";
		}
		cout << endl;*/
		judge();
		return;
	}
	if (index >= N) {
		return;
	}

	KOfCards.push_back(CardsInfo[index]);
	findCombination(index + 1, k + 1);
	KOfCards.pop_back();
	findCombination(index + 1, k);
}


int main() {
	int num;

	while (cin >> N >> K, N) {
		CardsInfo.clear();
		for (int i = 0; i < N; ++i) {
			cin >> num;
			CardsInfo.push_back(num);
		}
		Number.clear();
		findCombination(0, 0);
		cout << Number.size() << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<set>
using namespace std;
int C[10],n;
set<int>V;
void S(int k,int b){
	int i,c,v;
	for(i=0;i<n;i++){
		if(C[i]){
			v=b*(C[i]>9?100:10)+(c=C[i]);
			if(k>1){
				C[i]=0;
				S(k-1,v);
				C[i]=c;
			}else{
				V.insert(v);
			}
		}
	}
}
int main(){
	int k,i;
	for(;cin>>n>>k,n;){
		for(i=n;i--;)
			cin>>C[i];
		S(k,0);
		cout<<V.size()<<endl;
		V.clear();
	}
	cout<<endl;
}

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

bool solve() {
    int n, k;
    std::cin >> n >> k;
    if (n == 0) return false;

    std::vector<std::string> ss(n);
    for (auto& s : ss) std::cin >> s;

    std::set<std::string> ans;
    std::function<void(int, int, std::string)>
        dfs = [&](int rem, int b, std::string s) -> void {
        if (rem == 0) {
            ans.insert(s);
            return;
        }

        for (int i = 0; i < n; ++i) {
            if ((b >> i) & 1) continue;
            dfs(rem - 1, b | (1 << i), s + ss[i]);
        }
    };
    dfs(k, 0, "");

    std::cout << ans.size() << "\n";
    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while (solve()) {}

    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;
const int INF=1000000;

int main(){
    int n, k, C[1000];
    int checked_num[INF]={};
    while(1){
        cin >> n >> k;
        if(n==0 && k==0) break;
        for(int i=0; i<n; i++)
            cin >> C[i];
        sort(C, 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<INF; i++)
            ans+=checked_num[i];
        cout << ans << endl;

        memset(C, 0, 1000*sizeof(int));
        memset(checked_num, 0, INF*sizeof(int));
    }
    return 0;
}