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 <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
 
int n, k;
set<string> st;
string s;
vi v;
 
void calc(int c, vi u){
    if(u.size() == k){
    	// rep(i, u.size()){
    	// 	cout<<" "<<u[i];
    	// }
    	// cout<<endl;
    	sort(all(u));
        do{
            string t;
            for(int i = 0; i < k; i++){
                t += to_string(u[i]);
            }
            st.insert(t);
        }while(next_permutation(all(u)));
        return;
    }
    for(int i = c; i<n; i++){
        u.pb(v[i]);
        calc(i+1, u);
        u.pop_back();
    }
}
 
 
int main(){
    while(cin>>n>>k){
        if(n == 0 && k == 0) break;
        v = vi(n);
        rep(i, n) cin>>v[i];
        s = "";
        st.clear();
        vi tmp;
        calc(0,tmp);
        // for(auto it = st.begin(); it != st.end(); it++){
        // 	cout<<"  "<<*it<<endl;
        // }
        cout<<st.size()<<endl;
    }
}

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

void dfs(string now,int cou,int before);
int n,k;
vector<string> data;
map<string,int> used;
bool flg[15];


int main(){
  while(cin >> n >> k && n && k){
    data.clear();
    used.clear();
    for(int i=0;i<n;i++){
      string str; cin >> str;
      data.push_back(str);
    }
    for(int i=0;i<n;i++){
      memset(flg,true,n+1);
      dfs(data[i],0,i);
    }
    cout << used.size() << endl;
  }
}

void dfs(string now,int cou,int before){
  if(cou == k-1) {
    if(used[now]==0){
      used[now] = 1;
    }
  }
  else {
    flg[before] = false;
    for(int i=0;i<n;i++){
      if(flg[i]){
	string tmp=now;
	tmp += data[i];
	dfs(tmp,cou+1,i);
	tmp = data[i];
	tmp += now;
	dfs(tmp,cou+1,i);
      }
    }
  }
  flg[before] = true;
  return;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>

using namespace std;


int N, K;
int card[10];
bool used[10];
set<int> s;

int keta(int n) {
	int tmp = 1;
	while (n != 0)
	{
		n /= 10;
		tmp *= 10;
	}
	return tmp;
}

void solve(int m, int l) {
	if (m == K) {
		s.insert(l);

		return;
	}

	for (int i = 0; i < N; i++) {
		if (used[i] == false) {
			used[i] = true;
			solve(m + 1, l*keta(card[i]) + card[i]);
			used[i] = false;
		}
	}
}

int main() {

	while (cin >> N >> K, N)
	{
		s.clear();

		for (int i = 0; i < N; i++) {
			cin >> card[i];
			used[i] = false;
		}

		for (int i = 0; i < N; i++) {
			used[i] = true;
			solve( 1, card[i]);
			used[i] = false;
		}


		cout << s.size() << endl;
	}


	return 0;
}

C++: #include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;
ll n,k,a[10];
set<ll> s;
vector<ll> v;
ll num1,num2,num3,num4;
int main()
{while(1){
	scanf("%lld%lld",&n,&k);
	s.clear();
	if(!n) return 0;
	for(ll i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	if(k==2){
		for(ll i=0;i<n;i++){
		num1=a[i];
			for(ll j=0;j<n;j++){
				if(i==j) continue;
				if(a[j]<10){
					num2=num1*10+a[j];
				}
				else{
					num2=num1*100+a[j];
				}
				s.insert(num2);
			}
			
		}
	}
	else if(k==3){
		for(ll i=0;i<n;i++){
		num1=a[i];
			for(ll j=0;j<n;j++){
				if(i==j) continue;
				if(a[j]<10){
					num2=num1*10+a[j];
				}
				else{
					num2=num1*100+a[j];
				}
				for(ll l=0;l<n;l++){
					if(i==l||j==l) continue;
					if(a[l]<10){
						num3=num2*10+a[l];
					}
					else{
						num3=num2*100+a[l];
					}
					s.insert(num3);
				}
			}
		}
	}
	else{
		for(ll i=0;i<n;i++){
		num1=a[i];
			for(ll j=0;j<n;j++){
				if(i==j) continue;
				if(a[j]<10){
					num2=num1*10+a[j];
				}
				else{
					num2=num1*100+a[j];
				}
				for(ll l=0;l<n;l++){
					if(i==l||j==l) continue;
					if(a[l]<10){
						num3=num2*10+a[l];
					}
					else{
						num3=num2*100+a[l];
					}
					for(ll t=0;t<n;t++){
						if(t==i||t==j||t==l) continue;
						if(a[t]<10){
							num4=num3*10+a[t];
						}
						else{
							num4=num3*100+a[t];
						}
						s.insert(num4);
					}
				}
			}
		}
	}
	cout<<s.size()<<endl;
}}

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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXN = 11;
string s[MAXN];
//bool used[MAXN];

int main()
{
    int n, k;
    while(cin >> n >> k && n)
    {
        REP(n) cin >> s[i];
        set<string> ans;

        if(k == 2)rep(i,n)rep(j,n)if(i != j) ans.insert(s[i] + s[j]);
        if(k == 3)rep(i,n)rep(j,n)rep(l,n) if(i != j && j != l && l != i)ans.insert(s[i]+s[j]+s[l]);

        if(k == 4)rep(i,n)rep(j,n)rep(l,n)rep(m,n)
            if(i != j && i != l && i != m && j != l && j != m && l != m)
                ans.insert(s[i]+s[j]+s[l]+s[m]);

        cout << ans.size() << endl;

    }
    
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <cmath>
using namespace std;

int n,k;
string a[11];
string b[4];
char t[5];
int main()
{
	int x,sum;
	while(1){
		scanf(" %d%d",&n,&k);
		if(n == 0 && k==0)break;
		set<string> ss;
		for(int i = 0; i < n; ++i){
			scanf(" %s ",t);
			a[i] = t;
		}
		sort(a,a+n);
		do{
			string s;
			for (int i = 0; i < k; ++i){
				s += a[i];
			}
			ss.insert(s);
		}while(next_permutation(a,a+n));
		printf("%d\n",(int)ss.size());
	}
}

C++: #include<iostream>
#include<string>
#include<set>
using namespace std;
int f[10],n,k;
string v[10];
set<string>u;
void F(int d,string s)
{
	if(d==k)u.insert(s);
	else for(int i=0;i<n;++i)
		if(!f[i])
		{
			f[i]=1;
			F(d+1,s+v[i]);
			f[i]=0;
		}
}
int main()
{
	int i;
	string s;
	while(cin>>n>>k,n)
	{
		for(i=0;i<n;++i)cin>>s,v[i]=s;
		u.clear();
		F(0,"");
		cout<<u.size()<<endl;
	}
	return 0;
}