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> // only for GCC
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

vs v;
set<string> a;
bool used[10];
int n, m;
void bfs(string s, int num){
    if(m == num){
        a.insert(s);
    }
    else {
        rep(i, n){
            if(!used[i]){
                used[i] = true;
                bfs(s+v[i], num+1);
                used[i] = false;
            }
        }
    }
}

int main(){
    while(cin >> n >> m, n+m){
        v.clear(); a.clear();
        rep(i, n){
            string s;
            cin >> s;
            v.pb(s);
        }
        fill(used, used+10, false);
        bfs("", 0);
        cout << a.size() << endl;
    }
}

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

using namespace std;

#define INF ( 1 << 15 )

string array[10];
int n, k;
static set < string > ret;

void rec( string s, int bit, int cnt){
  if(cnt == k){
    ret.insert( s);
    return;
  }
  for(int i = 0 ; i < n ; i++ ){
    if((bit >> i) & 1) rec( s + array[i], bit&~(1<<i), cnt + 1);
  }
}
int main(){
  while(cin >> n >> k , n){
    for(int i = 0 ; i < n ; i++ ){
      cin >> array[i];
    }
    rec( "", (1 << n) - 1, 0);
    cout << ret.size() << endl;
    ret.clear();
  }
}

C++: #include <cstdio>
#include <math.h>
#include <set>
using namespace std;

int n, k, c[11], a[4];
bool dp[11] = { false };
set<int> s;

inline int make(int* a) {
	int r = 0, f = 1;
	for (int i = 0; i < k; i++) {
		r += a[i] * f;
		f *= pow(10, (int)log10(a[i]) + 1);
	}
	return r;
}

int rec(int d = 0) {
	if (d == k) {
		int r = make(a);
		if (s.find(r) == s.end()) {
			s.insert(r);
		}
	}
	else {
		for (int i = 0; i < n; i++) {
			if (!dp[i]) {
				a[d] = c[i];
				dp[i] = true;
				rec(d + 1);
				dp[i] = false;
			}
		}
	}
	return 0;
}

int main(){
	while (scanf("%d",&n), scanf("%d",&k), n != 0) {
		s = set<int>();
		for (int i = 0; i < n; i++) {
			scanf("%d", &c[i]);
		}
		rec();
		printf("%d\n", s.size());
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
  
using namespace std;
  
int n,k,ans;
vector<string> V;
map<string,vector<int> > M;
map<string,int> USED;
void init(){
  V.clear();
  M.clear();
  USED.clear();
  ans = 0;
}
 
bool find(string s,int num){
  for(int i = 0; i < M[s].size(); i++)
    if(M[s][i] == num) return true;
 
  return false;
}
  
void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  M[s].push_back(rev);

  if(rev == k){
    if(USED[s] == 0){
      USED[s] = 1;
      ans++;
    }
    return;
  }
  
  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0){
      if(!find(s+V[i],rev+1))
	solve(s+V[i],rev+1,used+(1<<i));
      
    }
}
  
int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

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

int numofbits5(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

int main(){
	int n,k,ret,sum;
	string b;
	while(cin >> n >> k && n || k){
		ret = 0;
		set<string> ret;
		vector<string> v,item(n);
		
		for(int i=0;i < n ; i++){
			cin >> item[i];
		}
		sort(item.begin(),item.end());
		for(int i=0;i < (1<<n);i++){
			sum = 0;v.clear();
			if(numofbits5(i) != k)continue;
			for(int j=0;j<n;j++){
				if( (1<<j) & i ){
					v.push_back(item[j]);
				}
			}
			do{
				b.clear();
				for(size_t i=0; i<v.size(); ++i){
					b += v[i];
				}
				ret.insert(b);
								
			}while(next_permutation(v.begin(),v.end()));
		}
		cout << ret.size() << endl;
	}
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, k;
int a[10];
int v[10000] = {};
int s = 0;

void set_vec(int t, int f, int sum) {
	if(t>=k) {
		v[s] = sum;
		s++;
	}else {
		for(int i=0;i<n;i++) {
			if((1<<i) & f)continue;
			set_vec(t+1, f+(1<<i), sum*(a[i]<10?10:100)+a[i]);
		}
	}
}

int main() {
	scanf("%d", &n);
	scanf("%d", &k);
	
	for(int i=0;i<n;i++) {
		scanf("%d", &a[i]);
	}
	
	set_vec(0, 0, 0);
	
	sort(v, v+s);
	
	int t = 0;
	int sum = 0;
	
	for(int i=0;i<s;i++) {
		if(t==v[i])continue;
		sum++;
		t=v[i];
	}
	
	printf("%d\n", sum);
	
	return  0;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define EACH(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
using namespace std;

typedef vector<int> vint;

inline string to_s(int n){static char v[32];sprintf(v,"%d",n);return string(v);}

set<string> st;
int u[4];

void act(vint &v,int n){
  sort(u,u+n);
  do{
    string s;
    REP(i,n) s+=to_s(v[u[i]]);
    st.insert(s);
  }while(next_permutation(u,u+n));
}

int main(){
  int n,k;
  while(cin >> n >> k && n|k){
    vint v(n);
    REP(i,n) cin >> v[i];

    st.clear();

    if(k==2){
      REP(i,n) REP(j,i){
	u[0]=i, u[1]=j; act(v,2);
      }
    }
    if(k==3){
      REP(i,n) REP(j,i) REP(l,j){
	u[0]=i, u[1]=j; u[2]=l; act(v,3);
      } 
    }
    if(k==4){
      REP(i,n) REP(j,i) REP(l,j) REP(m,l){
	u[0]=i, u[1]=j; u[2]=l, u[3]=m; act(v,4);
      }
    }

    // EACH(it,st) cout << *it << ' '; cout << endl;
    
    cout << st.size() << endl;
  }
}