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>
using namespace std;
#define INF (1<<29)//INF
#define F first
#define S second
#define VS vector<string>
#define VI vector<int>
#define REPI(i,s,n) for(int i=s;i<n;i++)//sからn未満の間iを回す
#define REPJ(j,s,n) for(int j=s;j<n;j++)//sからn未満の間iを回す
#define RREPI(i,s,n) for(int i=s;i>=n;i--)//sからn以下の間iを回す
#define RREPJ(j,s,n) for(int j=s;j>=n;j--)//sからn以下の間jを回す
#define ALL(a) a.begin(),a.end()//ソートとかに使える sort(ALL(vec))のように
#define SET(a,c) memset(a,c,sizeof(a));//aをc（0or1）で初期化する
#define DEBUGONE(x) cout<<#x<<" : "<<x<<endl;//デバックで一つの変数を表示
#define DEBUGTWO(x,y) cout<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl;//デバックで二つの変数を表示
#define DEBUGTHREE(x,y,z) cout<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl;//デバックで三つの変数を表示
#define OUTONE(x) cout<<x<<endl;//一つの変数を表示
#define OUTTWO(x,y) cout<<x<<" "<<y<<endl;//二つの変数を表示
#define OUTTHREE(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;//三つの変数を表示
typedef long long int64;
typedef pair<string,int> Psi;
typedef pair<int,int> Pii;
map<Psi,int64> mem;
string num[15];
int n,k;
int64 ans=0;
int64 bitdfs(int now,string nows,int bits);
main(){
  cin>>n>>k;
  REPI(i,0,n){
    cin>>num[i];
  }
  cout<<bitdfs(0,"",0)<<endl;
}
int64 bitdfs(int now,string nows,int bits){
  if(now>=k){
    if(mem[Psi(nows,now)]==0){
      mem[Psi(nows,now)]=-1;
      return 1;
    }
    else{
      return 0;
    }
  }
  else{
    int64 cnt=0;
    int bitst=1;
    REPI(i,0,n){
      int b=bitst&bits;
      if(b==0){
	cnt+=bitdfs(now+1,nows+num[i],bits|bitst);
      }
      bitst=bitst<<1;
    }
    return mem[Psi(nows,now)]=cnt;
  }
}

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

#define rep(i,n) for(int i=0; i<(n); i++)

using namespace std;

set<string> s;
int trace[4];
bool used[10];

void dfs(int depth, int n, int k, vector<int>& num) {
    if (depth == k) {
        stringstream ss;
        rep(i,k)ss << num[trace[i]];
        s.insert(ss.str());
    } else {
        rep(i,n) {
            if (used[i])continue;
            used[i] = true;
            trace[depth] = i;
            dfs(depth + 1, n, k, num);
            used[i] = false;
        }
    }
}

int main() {
    int n,k;
    while(cin >> n >> k, n) {
        s.clear();
        vector<int> num(n);
        rep(i,n) cin >> num[i];
        dfs(0, n, k, num);
        cout << s.size() << endl;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-8;


int tx[] = {-1,0,1,0};
int ty[] = {0,1,0,-1};

int path[501][501];


int main(){
	int n,k;
	while(~scanf("%d %d",&n,&k)){
		if(n==k && k==0) break;

		vector<string> nums;
		for(int i=0;i<n;i++){
			string num;
			cin >> num;
			nums.push_back(num);
		}

		sort(nums.begin(),nums.end());
		map<string,int> visited;


		vector<int> choice;
		for(int j=0;j<n-k;j++) choice.push_back(0);
		while(choice.size()!=n) choice.push_back(1);

		int res=0;
		do{
			vector<string> str;

			for(int j=0;j<choice.size();j++){
				if(choice[j]==1) str.push_back(nums[j]);
			}

			sort(str.begin(),str.end());

			do{
				string num;
				for(int j=0;j<str.size();j++) num.append(str[j]);
				if(visited.find(num) == visited.end()){
					visited[num]=1;
					res++;
				}
			}while(next_permutation(str.begin(),str.end()));
		}while(next_permutation(choice.begin(),choice.end()));

		printf("%d\n",res);
	}
}

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

int keta(int x){
	if(10>x)return 10;
	return 100;
}

int main(){
while(1){
	vector<int> ans;
	int n,k,a[10+2];
	scanf("%d%d",&n,&k);
if(n==0&&k==0)break;
	for(int i=0;i<n;i++)scanf("%d",&a[i]);
	if(k==2){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(j!=i){
					int c=a[i]*keta(a[j])+a[j];
					vector<int>::iterator ite;
					ite=find(ans.begin(),ans.end(),c);
					if(ite==ans.end())ans.push_back(c);
				}
			}
		}
	}
	if(k==3){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(j!=i){
					for(int x=0;x<n;x++){
						if(x!=i&&x!=j){
							int c=(a[i]*keta(a[j])+a[j])*keta(a[x])+a[x];
							vector<int>::iterator ite;
							ite=find(ans.begin(),ans.end(),c);
							if(ite==ans.end())ans.push_back(c);
						}
					}
				}
			}
		}
	}
	if(k==4){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(j!=i){
					for(int x=0;x<n;x++){
						if(x!=i&&x!=j){
							for(int y=0;y<n;y++){
								if(y!=i&&y!=j&&y!=x){
									int c=((a[i]*keta(a[j])+a[j])*keta(a[x])+a[x])*keta(a[y])+a[y];
									vector<int>::iterator ite;
									ite=find(ans.begin(),ans.end(),c);
									if(ite==ans.end())ans.push_back(c);
								}
							}
						}
					}
				}
			}
		}
	}
	printf("%d\n",ans.size());
}
}

C++: #include<bits/stdc++.h>
#define LL long long
static const LL INF = 1<<50;

using namespace std;

int n,k,ans=1;
vector<string> s;
string num[11];
int t=0;
void cal(string sum,int depth,int bit){
    if(depth==k){
        s.push_back(sum);
        return;
    }
    for(int i=1;i<=n;++i){
        int b=1<<i;
        if(bit&b){
            int k=bit-b;
            cal(sum+num[i],depth+1,k);
        }
    }
}

int main(){
    while(1){
        int p=1;
        t=0;
        cin>>n>>k;
        if(n==0&&k==0)break;
        for(int i=1;i<=n;++i){
            p+=pow(2,i);
            cin>>num[i];
        }
        cal("",0,p);
        sort(s.begin(),s.end());
        for(int i=1;i<s.size();++i){
            if(s[i]!=s[i-1]){
                ans++;
            }
        }
        cout<<ans<<endl;
        ans=0;
        s.clear();
    }
}