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<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<set>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
set<string>se;
vector<string>str(15);
int n,K;
void dfs2(int a,int b,int done,string s){
  if(done==(1<<2)-1){
    se.insert(s);
    return;
  }
  for(int i=0;i<2;i++){
    if(!(done&(1<<i))){
      switch(i){
      case 0:dfs2(a,b,done|(1<<i),s+str[a]);break;
      case 1:dfs2(a,b,done|(1<<i),s+str[b]);break;
      }
    }
  }
}
void dfs3(int a,int b,int c,int done,string s){
  if(done==(1<<3)-1){
    se.insert(s);
    return;
  }
  for(int i=0;i<3;i++){
    if(!(done&(1<<i))){
      switch(i){
      case 0:dfs3(a,b,c,done|(1<<i),s+str[a]);break;
      case 1:dfs3(a,b,c,done|(1<<i),s+str[b]);break;
      case 2:dfs3(a,b,c,done|(1<<i),s+str[c]);break;
      }
    }
  }
}
void dfs(int a,int b,int c,int d,int done,string s){
  if(done==(1<<4)-1){
    se.insert(s);
    return;
  }
  for(int i=0;i<4;i++){
    if(!(done&(1<<i))){
      switch(i){
      case 0:dfs(a,b,c,d,done|(1<<i),s+str[a]);break;
      case 1:dfs(a,b,c,d,done|(1<<i),s+str[b]);break;
      case 2:dfs(a,b,c,d,done|(1<<i),s+str[c]);break;
      case 3:dfs(a,b,c,d,done|(1<<i),s+str[d]);break;
      }
    }
  }
}
int main(){
  while(cin>>n>>K,n){
    se.clear();
    for(int i=0;i<n;i++)
      cin>>str[i];
    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	if(K==2){dfs2(i,j,0,"");continue;}
	for(int k=j+1;k<n;k++){
	  if(K==3){dfs3(i,j,k,0,"");continue;}
	  for(int l=k+1;l<n;l++){
	    dfs(i,j,k,l,0,"");
	  }
	}
      }
    }
    cout<<se.size()<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int main()
{
	while(1)
	{
		int n,k;
		cin >> n >> k;
		if(n==0&&k==0)break;
		int ans=0;
		set<string> st;
		vector<string> vec;
		for(int i=0;i<n;i++)
		{
			string s;
			cin >> s;
			vec.pb(s);
		}
		int comb=(1<<k)-1;
		while(comb<(1<<n))
		{
			string str;
			vector<int> num;
			for(int i=0;i<n;i++)
			{
				if((comb>>i)&1)
				{
					num.pb(i);
				}
			}
			do
			{
				str.clear();
				for(int i=0;i<num.size();i++)
				{
					str+=vec[num[i]];
				}
				if(st.find(str)==st.end())
				{
					st.insert(str);
					ans++;
				}
			}while(next_permutation(num.begin(),num.end()));
			int x=comb&-comb,y=comb+x;
			comb=((comb&~y)/x>>1)|y;
		}
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

void solve()
{
	int n;
	cin >> n;
	int sheet;
	cin >> sheet;
	set<string> Set;
	vector<string> Vec(n);
	for(int i = 0; i < n; ++i)
	{
		cin >> Vec[i];
	}
	if(sheet == 2)
	{
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				if(i == j)
				{
					continue;
				}
				Set.insert(Vec[i] + Vec[j]);
			}
		}
	}
	if(sheet == 3)
	{
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				for(int k = 0; k < n; ++k)
				{
					if(i == j || j == k || i == k)
					{
						continue;
					}
					Set.insert(Vec[i] + Vec[j] + Vec[k]);
				}
			}
		}
	}
	if(sheet == 4)
	{
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				for(int k = 0; k < n; ++k)
				{
					for(int l = 0; l < n; ++l)
					{
						if(i == j || i == k || i == l || j == k || j == l || k == l)
						{
							continue;
						}
						Set.insert(Vec[i] + Vec[j] + Vec[k] + Vec[l]);
					}
				}
			}
		}
	}
	cout << Set.size() << endl;
}

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

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

signed main() {
	int n, k;
	while(1){
		cin >> n >> k;
		if(n + k == 0) break;
		vector<string> s(n);
		set<string> st;
		rep(i, 0, n){
			cin >> s[i];
		}
		sort(all(s));
		do{
			string tmp = "";
			rep(i, 0, k){
				tmp += s[i];
			}
			//cout << tmp <<endl;
			if(st.count(tmp)) continue;
			st.insert(tmp);
		}while(next_permutation(all(s)));
		cout << st.size() <<endl;
	}
}

C++: #include<iostream>
#include<map>
#include<string>
using namespace std;
int n, k;
string x[128];
map<string, bool> memo;
void solve(int cnt, string s, bool used[]){
  if(cnt == k){
    memo[s] = true;
    return;
  }
  for(int i = 0;i < n; i++){
    if(!used[i]){
      used[i] = true;
      solve(cnt + 1, s + x[i], used);
      used[i] = false;
    }
  }
}
int main(void){
  for( ; ; ){
    bool used[128] = {false};
    cin >> n >> k;
    if(!(n + k)) break;
    memo.clear();
    for(int i = 0;i < n; i++) cin >> x[i];
    solve(0, "", used);
    cout << memo.size() << endl;
  }
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    while(1){
        int N,K;
        string str[11];
        string lis[10021];
        cin>>N>>K;
        if(N==0&&K==0)break;
        for(int i=0;i<N;i++)cin>>str[i];
        int ii=0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                for(int k=0;k<N;k++){
                    for(int l=0;l<N;l++){
                        if(i!=j&&j!=k&&k!=l&&i!=k&&j!=l&&i!=l){
                            if(K==2)lis[ii]=str[i]+str[j];
                            if(K==3)lis[ii]=str[i]+str[j]+str[k];
                            if(K==4)lis[ii]=str[i]+str[j]+str[k]+str[l];
                            ii++;
                        }
                    }
                }
            }
        }
        sort(lis,lis+10012);
        int ans=0;
        for(int i=0;i<10015;i++){
            if(lis[i]!=lis[i+1])ans++;
            cout<<lis[i]<<endl;
        }
        cout<<'['<<ans<<']'<<endl;
    }
    return 0;
}