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>
#define ll long long
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
using namespace std;

int main(){
    while(true){
    ll n,k;
    cin>>n>>k;
    if(n==0&&k==0)return 0;
    vector<ll> a(n);
    rep(i,n)cin>>a[i];
    sort(a.begin(),a.end());
    set<string> ans;
    if(k==2){
        rep(i,n){
            rep(j,n){
                if(i==j)continue;
                string v;
                v+=to_string(a[i])+to_string(a[j]);
                ans.insert(v);
            }
        }
    }else if(k==3){
        rep(i,n){
            rep(j,n){
                if(i==j)continue;
                rep(l,n){
                    if(i==l || j==l)continue;
                    string v;
                    v+=to_string(a[i])+to_string(a[j])+to_string(a[l]);
                    ans.insert(v);
                }
            }
        }
    }else if(k==4){
        rep(i,n){
            rep(j,n){
                if(i==j)continue;
                rep(l,n){
                    if(i==l || j==l)continue;
                    rep(m,n){
                        if(i==m || j==m || l==m)continue;
                        string v;
                        v+=to_string(a[i])+to_string(a[j])+to_string(a[l])+to_string(a[m]);
                        ans.insert(v);
                    }
                }
            }
        }
    }
    cout<<ans.size()<<endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout<<(n)<<endl
#define Cout(n) cout<<(n)
#define rep(i,num,N) for(int(i)=(num);(i)<(N);++(i))
#define rrep(i,num,N) for(int (i)=(num);(i)>(N);--(i))
#define all(v) (v).begin() , (v).end()
#define rall(v) (v).rbegin() , (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,K;
    
    while(cin>>N>>K,N!=0&&K!=0){
        set<string> num;
        vector<string> card(N);
        rep(i,0,N) cin>>card[i];
        sort(all(card));

        do{
            string s = "";
            rep(i,0,K) s += card[i]; 
            num.insert(s);
        }while(next_permutation(all(card)));
        put(num.size());
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)

using ll=long long;
using ld=long double;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int, int>;
using vpii=vector<pii>;

template<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>p){os<<"("<<p.fst<<", "<<p.scd<<")";return os;}
template<class T>ostream&operator<<(ostream&os,const vector<T>v){rep(i,v.size()){if(i)os<<", ";os<<v[i];}return os;}
template<typename T>T&max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T&min(T&a,T&b){if(a < b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a < b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a > b){a=b;return true;}return false;}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

int n, k;
string a[11];
bool used[11];
set<string> se;

void dfs(int j, string s="")
{
    if (j == 0) {
        se.insert(s);
        return;
    }

    rep(i, n) {
        if (used[i]) continue;
        used[i] = true;

        dfs(j - 1, s + a[i]);

        used[i] = false;
    }
}

int main()
{
    while (cin >> n >> k, n) {
        se.clear();

        rep(i, n) cin >> a[i];

        dfs(k);

        cout << se.size() << endl;
    }
}

C++: #include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>

using namespace std;
int a, b;
int num[15];


int bind(int p,int q){
	if(q<10){
		return 10*p+q;
	}
	else return 100*p+q;
}


int main(void){
	while(1){
		cin >> a >> b;
		if(a == 0 && b == 0){
			return 0;
		}
		set <int> S;
		for(int i=0;i<a;i++){
			cin >> num[i];
		}
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				for(int k=0;k<a;k++){
					for(int l=0;l<a;l++){
						
						if(b==4){
							if(i != j && i != k && i != l && j != k && j != l && k != l){ 
							int c = bind(bind(bind(num[i],num[j]),num[k]),num[l]);
							S.insert(c);
							}
						}
						if(b==3){
							if(j != k && j != l && k != l){
							int c = bind(bind(num[j],num[k]),num[l]);
							S.insert(c);
							}
						}
						if(b==2){
							if(k != l){
							int c = bind(num[k],num[l]);
							S.insert(c);
							}
						}	
					}
				}
			}
		}
		cout << S.size() <<endl;
		S.clear();
	}
}

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

int main(){
    int n,k;
    while(1){
        cin>>n>>k;
        string S[11];
        if(n==0&&k==0) break;
        for(int i=0; i<n; i++) cin>>S[i];
        sort(S,S+n);
        set<string> SS;
        do{
            string str;
            for(int i=0; i<k; i++) str+=S[i];
            SS.insert(str);
        }while(next_permutation(S, S+n));
        cout<<SS.size()<<endl;
    }
}