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 <bits/stdc++.h>
using namespace std;

int main()
{
	int n, k;
	while (cin >> n >> k, 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;
}

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

void solve(int n, int k) {
    vector<string> deck(n);
    for (int i = 0; i < n; ++i) {
        cin >> deck[i];
    }
    set<string> save;
    for (int bit = 0; bit < (1 << n); ++bit) {
        vector<string> p;
        for (int j = 0; j < n; ++j) {
            if (bit >> j & 1) p.push_back(deck[j]);
        }
        if (p.size() != k) continue;
        sort(p.begin(), p.end());
        do {
            string ans;
            for (int i = 0; i < k; ++i) {
                ans += p[i];
            }
            save.insert(ans);
        } while(next_permutation(p.begin(), p.end()));
    }
    cout << save.size() << endl;
}

int main() {
    while(true) {
        int n, k;
        cin >> n >> k;
        if (n == 0 && k == 0) break;
        else solve(n, k);
    }
    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main()
{
  int n, k;
  while(cin >> n, n) {
    cin >> k;
    set<string> SET;
    vector<string> vs(n); FOR(i,0,n) cin >> vs[i];
    FOR(i,0,1<<n) {
      vector<int> v;
      int cnt = 0;
      FOR(j,0,n) {
        if((i>>j)&1) {
          cnt++;
          v.push_back(j);
        }
      }
      if(cnt != k) continue;
      do {
        string s = "";
        FOR(l,0,k) s = s + vs[v[l]];
        SET.insert(s);
      } while(next_permutation(v.begin(), v.end()));
    }
    cout << SET.size() << endl;
  } 
  
  return 0;
}

C++: /* 駄実装の典型 皆さんはstackではなくてn進法を使いましょう */
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <deque>
#include <algorithm>

using namespace std;

int main() {
    while (1) {
        int n;
        int k;
        vector<string> element_strings;
        set<string> generated_strings;
        deque<int> stack;
    
        scanf("%d", &n);
        scanf("%d", &k);
    
        if (n == 0 && k == 0) return 0;
 
        for (int i=0; i<n; i++) {
            char number[3];
            scanf("%s", number);
            element_strings.push_back(string(number));
        }
    
        sort(element_strings.begin(), element_strings.end());
    
        while (1) {
            char has_appeared[11];
            char not_count = 0;
            string new_string = "";
            int next_ind;
    
            while (stack.size() != k) {
                stack.push_back(0);
            }

            memset(has_appeared, 0, sizeof(has_appeared));

            for (deque<int>::iterator itr=stack.begin(); itr != stack.end(); itr++) {
                if (has_appeared[*itr]) {
                    not_count = 1;
                    break;
                }
                has_appeared[*itr] = 1;
                new_string += element_strings[*itr];
            }

            if (!not_count && generated_strings.count(new_string) == 0) { 
                generated_strings.insert(new_string);
            }
    
            while (!stack.empty() && stack.back() == element_strings.size()-1) {
                stack.pop_back();
            }
    
            if (stack.empty()) {
                break;
            }
    
            next_ind = stack.back() + 1;
            stack.pop_back();
            stack.push_back(next_ind);
        }
    
        printf("%lu\n", generated_strings.size());
    }
}

C++: #include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<numeric>

#define MOD 1000000007

using namespace std;
typedef long long ll;

int A[20];
set<int> s;

int check(int a,int n ,int c) {
	if (c == 0) {
		s.insert(a);
		return 0;
	}
	for (int i = 0; i < n; i++) {
		if (A[i] != -1) {
			int tmp = A[i];
			A[i] = -1;
			int d;
			if (tmp < 10)
				d = 10;
			else
				d = 100;
			check(a *d+tmp,n,c-1);
			A[i] = tmp;
		}
	}
	return 0;
}

int main()
{
	int N, K;
	while (1) {
		s.clear();
		cin >> N >> K;
		if (N == 0 && K== 0)break;

		for (int i = 0; i < N; i++) {
			cin >> A[i];
		}
		check(0,N,K);
		cout << s.size() << endl;
	}
	return 0;
}