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 rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
 
signed main() {
    int n, k;
    while(1){
        cin >> n >> k;
        if(n + k == 0) break;
        vector<string> s(n);
        set<string> st;
        rep(i, 0, n){
            cin >> s[i];
        }
        //sort(all(s));
        do{
            string tmp = "";
            rep(i, 0, k){
                tmp += s[i];
            }
            //cout << tmp <<endl;
            if(st.count(tmp)) continue;
            st.insert(tmp);
        }while(next_permutation(all(s)));
        cout << st.size() <<endl;
    }
}

C++: #include <iostream>
#include <queue>
#include <set>
#include <utility>

using namespace std;


class Solver {
  public:
    int n, k;
    int nums[10];
    int solve() {
      bool has_used[10];
      set<int> integers;
      for (int i = 0; i < n; ++i) {
        has_used[i] = false;
      }
      search(integers, has_used);
      return integers.size();
    }
  private:
    void search(set<int> &integers, bool has_used[], int x = 0, int ki = 0) {
      if (ki < k) {
        for (int ni = 0; ni < n; ++ni) {
          if (has_used[ni]) {
            continue;
          }
          has_used[ni] = true;
          if (nums[ni] < 10) {
            search(integers, has_used, 10 * x + nums[ni], ki + 1);
          } else {
            search(integers, has_used, 100 * x + nums[ni], ki + 1);
          }
          has_used[ni] = false;
        }
      } else {
        integers.insert(x);
      }
    }
};

int main() {
  Solver s;
  while (true) {
    cin >> s.n >> s.k;
    if (s.n == 0) {
      return 0;
    }
    for (int i = 0; i < s.n; ++i) {
      cin >> s.nums[i];
    }
    cout << s.solve() << endl;
  }
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int n;
	while(cin >> n, n) {
		int k;
		cin >> k;
		vs cards(n);
		REP(i, n) {
			cin >> cards[i];
		}

		vi num;
		REP(i, n) {
			if(i < k) {
				num.push_back(1);
			} else {
				num.push_back(0);
			}
		}
		sort(ALL(num));

		set<string> ans;
		do {
			vs choice;
			REP(i, n) {
				if(num[i]) {
					choice.push_back(cards[i]);
				}
			}

			sort(ALL(choice));
			do {
				string s;
				REP(i, k) {
					s += choice[i];
				}
				ans.insert(s);
			} while(next_permutation(ALL(choice)));
		} while(next_permutation(ALL(num)));

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

C++: #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;

int n, k;
string str[10];

int main(){
	while(scanf("%d", &n),n){
		set<string> s;
		scanf("%d", &k);
		rep(i,n) cin >> str[i];
		if(k == 2){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j) continue;
					s.insert(str[i]+str[j]);
				}
			}
		} else if(k == 3){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(j == i) continue;
					for(int u = 0; u < n; u++){
						if(u == i || u == j) continue;
						s.insert(str[i]+str[j]+str[u]);
					}
				}
			}
		} else{
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(i == j) continue;
					for(int u = 0; u < n; u++){
						if(u == i || u == j) continue;
						for(int v = 0; v < n; v++){
							if(v == i || v == j || v == u) continue;
							s.insert(str[i]+str[j]+str[u]+str[v]);
						}
					}
				}
			}
		}
		cout << s.size() << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<map>

using namespace std;

int n,k;
string s[1000];
char in[10000];
map<string,bool> Array;


int recroop(string ss,int counter,int index,bool used[]){
  if(counter > k)return 1;
  if(counter == k && !Array[ss]){
    cout << ss << endl;
    Array[ss]=true; 
  }
  string press = ss;
  bool preused[n];
  for(int i=0;i<n;i++)preused[i]=used[i];

  for(int i=0;i<n;i++){
    if(i != index && !used[i]){
      ss+=s[i];
      used[i]=true;
      recroop(ss,counter+1,i,used);
      used[i]=preused[i];
      ss.clear();
      ss = press;
    }
  }



}


int main(){
  bool used[100];

  while(true){
    cin >> n >> k;
    if(n+k == 0)break;
    for(int i=0;i<100;i++)used[i]=false;
    for(int i=0;i<n;i++)cin >> s[i]; 

    recroop("",0,-1,used); 
      cout << Array.size() << endl;    
      Array.clear();
  }

  return 0;
}

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

#define REP(i,k,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll func(vector<int> v, int k) {
	stringstream ss;
	rep(i, k) {
		ss << v[i];
	}

	ll ret;
	ss >> ret;
	return ret;
}

int main() {
	int n;
	while(cin >> n && n) {
		int k;
		cin >> k;
		
		vector<int> v(n);
		rep(i, n) cin >> v[i];

		sort(v.begin(), v.end());

		set<ll> ans;

		do {
			ans.insert(func(v, k));
		}while(next_permutation(v.begin(), v.end()));

		cout << ans.size() << endl;
	}
	return 0;
}