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<iostream>
#include<vector>
#include<list>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<set>
using namespace std;
set<int>a;
void solve(int b[10],int i,string s,bool c[10],int n,int k) {
	for (int j = 0; j < n; j++) {
		if (c[j]) {
			if (i == k&&c[j]) {
				string u = s;
				s += to_string(b[j]);
				a.insert(stoi(s));
				s = u;
			}
			else {
				c[j] = false;
				solve(b, i+1, s+to_string(b[j]), c, n, k);
				c[j] = true;
			}
		}
	}
}
int main() {
	int n, k;
	while (cin >> n >> k&&n != 0 && k != 0) {
		a.clear();
		int b[10];
		bool c[10];
		for (int i = 0; i < n; i++) {
			cin >> b[i];
			c[i] = true;
		}
		k--;
		for (int i = 0; i < n; i++) {
			c[i] = false;
			solve(b, 1, to_string(b[i]),c,n,k);
			c[i] = true;
		}
		cout << a.size() << endl;
	}
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
	int n,k,i,a[]={0,1,2,3,4,5,6,7,8,9};
	string s,v[10];
	while(cin>>n>>k,n)
	{
		set<string>u;
		for(i=0;i<n;++i)cin>>s,v[i]=s;
		do
		{
			for(s="",i=0;i<k;++i)s+=v[a[i]];
			u.insert(s);
		}while(next_permutation(a,a+n));
		cout<<u.size()<<endl;
	}
	return 0;
}

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

using namespace std;

vector< vector<int> > MakeCombination1(vector< vector<int> >);
void MakeCombination1Inner(int, vector<int>, vector< vector<int> >, vector< vector<int> >&);
vector< vector<int> > MakeCombination2(int, int);
void MakeCombination2Inner(int, vector<int>&);
int CardPair(vector<string>, vector< vector<int> >, vector< vector<int> >);

int main(){
  int i, n, k;
  string s;
  vector<string> str;
  vector< vector<int> > data1, data2[3];

  data2[0] = MakeCombination1(data2[0]);
  data2[1] = MakeCombination1(data2[0]);
  data2[2] = MakeCombination1(data2[1]);

  while(1){
    cin >> n >> k;
    if(n == 0 && k == 0) break;
    data1 = MakeCombination2(n, k);

    for(i=0; i<n; ++i){
      cin >> s;
      str.push_back(s);
    }
    cout << CardPair(str, data1, data2[k-2]) << endl;
    str.clear();
  }
  return 0;
}

vector< vector<int> > MakeCombination1(vector< vector<int> > original){
  int i;
  vector<int> _data;
  vector< vector<int> > data;

  if(original.empty()){
    _data.push_back(0);
    _data.push_back(1);
    data.push_back(_data);
    _data.clear();
    _data.push_back(1);
    _data.push_back(0);
    data.push_back(_data);
  }else{
    _data = original[0];
    _data.push_back(original[0].size());

    for(i=_data.size()-1; i>=0; --i){
      MakeCombination1Inner(i, _data, original, data);
    }
  }
  return data;
}

void MakeCombination1Inner(int m, vector<int> num, vector< vector<int> > original, vector< vector<int> >& data){
  int i, j, n;
  vector<int> _data;
  n = num[m];
  num.erase(num.begin() + m);

  for(i=0; i<original.size(); ++i){
    for(j=0; j<original[i].size(); ++j)
      _data.push_back(num[original[i][j]]);

    _data.push_back(m);
    data.push_back(_data);
    _data.clear();
  }
}

vector< vector<int> > MakeCombination2(int n, int m){
  int i;
  vector<int> _data;
  vector< vector<int> > data;

  for(i=0; i<m; ++i)
    _data.push_back(i);
  data.push_back(_data);

  while(1){
    MakeCombination2Inner(n, _data);
    data.push_back(_data);

    if(_data[0] == n-m && _data[_data.size()-1] == n-1)
      break;
  }
  return data;
}

void MakeCombination2Inner(int n, vector<int>& data){
  int i, j;
  for(i=data.size()-1, j=1; i>=0; --i, ++j){
    if(data[i] != n-j){
      ++data[i];
      for(j=i+1; j<data.size(); ++j)
        data[j] = data[j-1]+1;
      return;
    }
  }
}

int CardPair(vector<string> str, vector< vector<int> > data1, vector< vector<int> > data2){
  int i, j, k;
  string s;
  vector<string> str1;
  set<string> str2;
  for(i=0; i<data1.size(); ++i){
    for(j=0; j<data1[i].size(); ++j){
      str1.push_back(str[data1[i][j]]);
    }
    for(j=0; j<data2.size(); ++j){
      for(k=0; k<data2[j].size(); ++k){
	s += str1[data2[j][k]];
      }
      if(str2.find(s) == str2.end()) str2.insert(s);
      s.clear();
    }
    str1.clear();
  }
  return str2.size();
}

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);
              if(a==0) break;
	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());
	for(int i=0;i<vec.size();i++){
		printf("%d\n",vec[i]);
	}
	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;
}