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 <string>
#include <vector>
#include <set>
#include <algorithm>





using namespace std;



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,k; cin >> n >> k;
    if(n==0 && k==0) break;
    vector<string> ss;
    set<string> hoge;
    for(int i=0; i<n; i++){
      string x; cin >> x;
      ss.push_back(x);
    }
    sort(ss.begin(),ss.end());
    
    do{
      vector<string> maki;
      for(int i=0; i<k; i++) maki.push_back(ss[i]);
      do{
	string tmp="";
	for(int i=0; i<maki.size(); i++)
	  tmp += maki[i];
	hoge.insert(tmp);
      }while(next_permutation(maki.begin(),maki.end()));
    }while(next_combination(ss.begin(), ss.begin()+k, ss.end()));
    cout << hoge.size() << "\n";
  }
}

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

int n,k;
vector<string>cards;
set<string>result;

void rec(int used,int depth)
{
  if(depth==k){
    vector<int>v;
    for(int i=0;i<cards.size();i++)if(used&(1<<i))v.push_back(i);
    do{
      string s="";
      for(int i=0;i<k;i++)s+=cards[v[i]];
      result.insert(s);
    }while(next_permutation(v.begin(),v.end()));
    return;
  }

  for(int i=0;i<cards.size();i++){
    if(used&(1<<i))continue;
    rec(used^(1<<i),depth+1);
  }

  return;
}

int main()
{
  while(cin>>n>>k,n|k){
    cards.resize(n);
    result.clear();
    for(int i=0;i<n;i++)cin>>cards[i];
    rec(0,0);
    cout<<result.size()<<endl;
  }
}

C++: #include <stdio.h>
int n,k,i,j,p,num[5040],c[4],card[10],ans;
void set(int x){
    int i,j;
    if (x==k) {
        for (i=0; i<k; i++) {
            num[p]=num[p]*10;
            if (card[c[i]]>=10) {
                num[p]=num[p]*10;
            }
            num[p]+=card[c[i]];
        }
        p++;
        return;
    }
    for (i=0; i<n; i++) {
        int flag=1;
        for (j=0; j<x; j++) {
            if (c[j]==i) {
                flag=0;
                break;
            }
        }
        if (flag) {
            c[x]=i;
            set(x+1);
        }
    }
    return;
}
int main(){
    while (1) {
        scanf("%d%d",&n,&k);
        if (n==0&&k==0) {
            return 0;
        }
        for (i=0; i<n; i++) {
            scanf("%d",&card[i]);
        }
        for (i=0; i<5040; i++) {
            num[i]=0;
        }
        p=0;
        set(0);
        ans=0;
        k=p;
        while (k>0) {
            for (i=0; i<p; i++) {
                if (num[i]!=-1) {
                    ans++;
                    k--;
                    for (j=i+1; j<p; j++) {
                        if (num[i]==num[j]) {
                            k--;
                            num[j]=-1;
                        }
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
}

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

int count(int n)
{
int ret = 1;
for(int mul = 10; mul <= n; mul *= 10) ++ret; 
return ret;
}

void add(int k, int sum, vector<int> &c, vector<int> &result)
{
if(k == 0) {
result.push_back(sum);
return;
}
for(int i = 0; i < c.size(); ++i) {
if(c[i] == 0)
continue;
int tmp = c[i];
c[i] = 0;
add(k - 1, sum * count(tmp) + tmp, c, result);
c[i] = tmp;
}
return;
}

int main()
{
while(true) {
int n, k;
vector<int> c;
vector<int> result;
scanf("%d%d", &n, &k);
if(n == 0)
break;
c.resize(n);
for(int i = 0; i < n; ++i) 
scanf("%d", &c[i]);
add(k, 0, c, result);
sort(result.begin(), result.end());
result.erase(unique(result.begin(), result.end()), result.end());
printf("%d\n", result.size());
}
return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1<<29
#define MAX 11
using namespace std;

int a[MAX],s[MAX];

void solve(){
 int n,k;
 for(;;){
  cin>>n;
  cin>>k;
  if(!n) break;
  set<string> S;
  FOR(i,k) cin>>a[i];
  FOR(i,1<<n){
   if(__builtin_popcount(i)!=k) continue;
   int p=0;
   FOR(j,n) if((1<<j)&i) s[p++]=j;
   vector<int> ord(k);
   FOR(j,k) ord.pb(j);
   string s="";
   do{
    FOR(j,k) s+=to_string(s[ord[j]]);
    S.insert(s);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

int main(){
 solve();
 return 0;
}