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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
	while(1){
	int a,b,c,x[11];
vector<int>vec;
	scanf("%d",&a);
	scanf("%d",&b);
	for(int i=0;i<a;i++){
		scanf("%d",&x[i]);
	}
	if(b==2){
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				if(j!=i){
				if(x[j]>=10){
					vec.push_back(x[i]*100+x[j]);
				}else{
					vec.push_back(x[i]*10+x[j]);
				}
				}
			}
		}
	}else if(b==3){
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				if(j!=i){
				for(int k=0;k<a;k++){
					if(k!=i){
						if(k!=j){
					if(x[k]>=10){
						if(x[j]>=10){
							vec.push_back(x[i]*10000+x[j]*100+x[k]);
						}else{
							vec.push_back(x[i]*1000+x[j]*100+x[k]);
						}
					}
					else{
						if(x[j]>=10){
							vec.push_back(x[i]*1000+x[j]*10+x[k]);
						}else{
							vec.push_back(x[i]*100+x[j]*10+x[k]);
						}
					}
				}
			}
		}
				}
			}
		}
	}else if(b==4){
		for(int i=0;i<a;i++){
			for(int j=0;j<a;j++){
				if(j!=i){
				for(int k=0;k<a;k++){
					if(k!=i){
						if(k!=j){
					for(int h=0;h<a;h++){
						if(h!=i){
							if(h!=j){
								if(h!=k){
						if(x[h]>=10){
							if(x[k]>=10){
								if(x[j]>=10){
									vec.push_back(x[i]*1000000+x[j]*10000+x[k]*100+x[h]);
								}else{
									vec.push_back(x[i]*100000+x[j]*10000+x[k]*100+x[h]);
								}
							}else{
								if(x[j]>=10){
									vec.push_back(x[i]*100000+x[j]*1000+x[k]*100+x[h]);
								}else{
									vec.push_back(x[i]*10000+x[j]*1000+x[k]*100+x[h]);
								}
							}
						}else{
							if(x[k]>=10){
								if(x[j]>=10){
									vec.push_back(x[i]*100000+x[j]*1000+x[k]*10+x[h]);
								}else{
									vec.push_back(x[i]*10000+x[j]*1000+x[k]*10+x[h]);
								}
							}else{
								if(x[j]>=10){
									vec.push_back(x[i]*10000+x[j]*100+x[k]*10+x[h]);
								}else{
									vec.push_back(x[i]*1000+x[j]*100+x[k]*10+x[h]);
								}
							}
						}
					}
				}
					}
					}
						}
						}
				}
				}
			}
		}
	}
	sort(vec.begin(),vec.end());
	int ans=1;
	for(int i=0;i<vec.size()-1;i++){
		if(vec[i]!=vec[i+1]){
			ans++;
		}
	}
	printf("%d\n",ans);
	}
	return 0;
}

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


int bit_s(int x){
	int re=0;
	for(int i=0;i<31;i++){
		if(x&1<<i)re++;
	}
	
	return re;
}	

int main(){
	int n,k;
	while(cin>>n>>k && n && k){
		set<int> ans;
		int data[50];
		for(int i=0;i<n;i++)cin>>data[i];
		
		for(int i=0;i<2<<(n-1);i++){
			if(bit_s(i)==k){
				vector<int> tmp;
				for(int j=0;j<n;j++){
					
					if(i&1<<j)tmp.push_back(data[j]);
				}
				vector<int> loop;
				for(int i=0;i<tmp.size();i++)loop.push_back(i);
				do{
					int a=0;
					for(int i=0;i<loop.size();i++){
						if(tmp[loop[i]]<10)a=a*10+tmp[loop[i]];
						else a=a*100+tmp[loop[i]];
					}
					ans.insert(a);
				}while(next_permutation(loop.begin(),loop.end()));
			}
		}
		cout<<ans.size()<<endl;
		//for(set<int>::iterator itr =ans.begin();itr!=ans.end();itr++)cout<<(*itr)<<endl;
	}
	
	
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

template < class BidirectionalIterator >
bool next_combination(BidirectionalIterator first1,
	BidirectionalIterator last1,
	BidirectionalIterator first2,
	BidirectionalIterator last2)
{
	if ((first1 == last1) || (first2 == last2)) {
		return false;
	}
	BidirectionalIterator m1 = last1;
	BidirectionalIterator m2 = last2; --m2;
	while (--m1 != first1 && !(*m1 < *m2)) {
	}
	bool result = (m1 == first1) && !(*first1 < *m2);
	if (!result) {
		// ???
		while (first2 != m2 && !(*m1 < *first2)) {
			++first2;
		}
		first1 = m1;
		std::iter_swap(first1, first2);
		++first1;
		++first2;
	}
	if ((first1 != last1) && (first2 != last2)) {
		// ???
		m1 = last1; m2 = first2;
		while ((m1 != first1) && (m2 != last2)) {
			std::iter_swap(--m1, m2);
			++m2;
		}
		// ??¢
		std::reverse(first1, m1);
		std::reverse(first1, last1);
		std::reverse(m2, last2);
		std::reverse(first2, last2);
	}
	return !result;
}

template < class BidirectionalIterator >
bool next_combination(BidirectionalIterator first,
	BidirectionalIterator middle,
	BidirectionalIterator last)
{
	return next_combination(first, middle, middle, last);
}

int main() {
	while (1) {
		int N; cin >> N; int K; cin >> K;
		if (!N)break;
		vector<string>sts;
		for (int i = 0; i < N; ++i) {
			string st; cin >> st;
			sts.emplace_back(st);
		}
		vector<int>nums(N);
		iota(nums.begin(), nums.end(), 0);
		set<string>anss;
		do {
			vector<int>anums(K);
			iota(anums.begin(), anums.end(), 0);
			do {
				string ans;
				for (int i = 0; i < K; ++i) {
					ans += sts[nums[anums[i]]];
				}
				anss.emplace(ans);
			} while (next_permutation(anums.begin(), anums.end()));
		} while (next_combination(nums.begin(),nums.begin()+K, nums.end()));
		cout << anss.size() << endl;
	}
	return 0;
}

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

int N, K;
set<int> result;
int card[16];
bool used[16];

void solve(int k, int ans) {
	if(k == K) {
		result.insert(ans);
		return;
	}
	for(int i = 0; i < N; i++) {
		if(used[i] == true) continue;
		used[i] = true;
		int t = card[i];
		int next;
		if(t < 10) {
			next = ans * 10;
		} else {
			next = ans * 100;
		}
		next += t;
		solve(k + 1, next);
		used[i] = false;
	}
}

int main() {
	for(;;) {
		result.clear();
		memset(used, 0, sizeof used);

		scanf("%d %d", &N, &K);
		if(N == 0 && K == 0)
			break;

		for(int i = 0; i < N; i++) {
			scanf(" %d", &card[i]);
		}

		solve(0, 0);
		printf("%d\n", result.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;
  if(rev == k && 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)){
	M[s+V[i]].push_back(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;
}