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 <queue>
#include <stack>
#include <set>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;

string cards[10];
set<string> s;
vector<int> vi, vi2;

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()
{
    int n, k;
    while(cin >> n >> k && n && k) {
        s.clear(); vi.clear();
        for(int i = 0; i < n; i++) {
            cin >> cards[i];
            vi.push_back(i);
        }

        int ret = 0;
        do {
            vi2.clear();
            for(int i = 0; i < k; i++) {
                vi2.push_back(vi[i]);
            }
            sort(vi2.begin(), vi2.end());
            do {
                string str = "";
                for(int i = 0; i < k; i++) {
                    str += cards[vi2[i]];
                }
                if(s.find(str) == s.end()) {
                    ret++;
                    s.insert(str);
                }
            } while(next_permutation(vi2.begin(), vi2.end()));
        } while(next_combination(vi.begin(), vi.begin() + k, vi.end()));
        cout << ret << endl;
        
    }
}

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

int main(void){
	while(1){
		int n, k;
		scanf("%d %d", &n, &k);
		if(n == 0 && k == 0) return 0;
		//??°?????????????????¨????????±???
		vector<string> data(n);//??\?????????????????\??????
		set<string> num;//k???????????§??§?????????????????\??????
		rep(i, n) cin >> data[i];
		
		sort(data.begin(), data.end());//?????????????????????
		//next_permutaion??§??¨???????????????
		do{
			string tmp;
			//??????????????????k???????????£?????????
			rep(i, k){
				tmp += data[i];
			}
			//set?????\???????????¨??§??????????????????????????????????????????????????????
			num.insert(tmp);
		}while(next_permutation(data.begin(), data.end()));
		printf("%lu\n", num.size());//?????°?????¨???
	}
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<set>
#include<string.h>
#include<ctype.h>

using namespace std;

int arr[10];
int sub[10];
int n,k;
int now = 0;
int cont;
int c = 0;

set<int> visit2;

void saiki(int[],int);

int main()
{
	for(;;)
	{
		visit2.clear();
		int i;
		c = 0;
		now = 0;
		
		scanf("%d",&n);
		scanf("%d",&k);
		if(n == 0 && k == 0){
			break;
		}
		
		for(i = 0; i < n; i++){
			scanf("%d",&arr[i]);
		}
		for(i = 0; i < n; i++){
			sub[0] = arr[i];
	//		printf("\næªJ[hF%d\n",arr[i]);
			saiki(sub,1);
			now++;
		}
		cout <<visit2.size()-c << endl;
	}
}

void saiki(int temp[], int kk)
{
	//k:TõK
	int used[50] = {0};
	int i;
	int t = 0;
	char str[256] = "";
	
	for(i = 0; i < kk; i++){
		sprintf(str+strlen(str),"%d",temp[i]);
	}
	
	t = atoi(str);
	
	if(kk == k){
		
		visit2.insert(t);
		return;
	}
	else{ 
		int j;
		
//		cout << "*t = " << t << endl;
		
		for(i = 0; i < kk; i++){
			for(j = 0; j < n; ++j ){     
				if( used[j] == 0 && arr[j] == temp[i] ){
//					printf("J[h%dÍgpÏÝ\n",arr[j]);
					used[j] = 1;
					break;
				}
			}
		}
		
		for(i = 0; i < n; i++){
			if(used[i] == 0){
				temp[kk] = arr[i];
				used[i] = 1;
				saiki(temp, kk+1);
				used[i] = 0;
			}
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int n, k;
string a[11];
set<string> all;
bool flag[11];

void solve(int num, string s) {
  if( num == k ) {
    all.insert(s);
  }
  for(int i=0; i<n; i++) {
    if( flag[i] ) continue;
    flag[i] = true;
    solve(num + 1, s + a[i]);
    flag[i] = false;
  }
}

int main() {
  cin >> n >> k;
  for(int i=0; i<n; i++) {
    cin >> a[i];
  }
  for(int i=0; i<11; i++) flag[i] = false;
  solve(0, "");
  cout << all.size() << endl;
}

C++: #include<stdio.h>
#include<algorithm>
#include<sstream>
#include<string.h>
#include<vector>
#include<set>
using namespace std;

int n, k, cnt = 0;
char card[10];
bool ans[100000000];
void solve(int a, vector<char>vec, vector<bool>b) {
	vec.push_back(card[a]);
	b[a] = true;
	if (vec.size() == k) {
		ostringstream oss;
		for (char i : vec)
			oss << (int)i;
		int n = stoi(oss.str());
		if (ans[n] != true)cnt++;
		ans[n] = true;
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!b[i])
			solve(i, vec, b);
	}
}
int main(){
	memset(ans, -1, sizeof(ans));
	scanf("%d%d", &n, &k);
	if (!(n | k))return 0;
	for (int i = 0; i < n; i++)
		scanf("%d", &card[i]);
	vector<char>vec;
	vector<bool>b(10);
	for (int i = 0; i < n; i++)
		solve(i, vec, b);
	printf("%d\n", cnt);
	main();
	return 0;
}

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

using namespace std;

set<string> st;

string getStr(int n)
{
  ostringstream ss;
  ss << n;
  return ss.str();
}

void func(int a[11], int n, string str, int k, bool b[11], int p)
{
  if(k == p){
    st.insert(str);
    return ;
  }

  for(int i = 0; i < n; i++){
    if(b[i]){
      b[i] = false;
      func(a, n, str+getStr(a[i]), k, b, p+1);
      b[i] = true;
    }
  }

  return ;
}

int main()
{
  int n, m, a[11];
  bool b[11];

  while(cin>>n && n){
    cin >> m;

    for(int i = 0; i < n; i++){
      cin >> a[i];
    }
    fill(b, b+11, true);
    func(a, n, "", m, b, 0);

    cout << st.size() << endl;
    st.clear();
  }

  return 0;
}