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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<utility>
#include<string>
#include<algorithm>

using namespace std;
string c[15];
int ans;
bool t[100000005];

map<string,bool> memo;

int ku[4];
int n,k;

//void shokika();
void kumi();
void ku2();
void ku3();
void ku4();

main(){
  int i,j,l,t;
  while(1){
    //    shokika();
    memo.clear();
    ans=0;
    //    puts("W");
    cin >> n >> k;
    if(n==0 && k==0) break;

    for(i=0;i<n;i++){
      cin >> c[i];
    }
    /*
    for(i=0;i<n;i++){
      cout << c[i] <<endl;
    }
    puts("");
    */
    for(i=0;i<n-(k-1);i++){
      for(j=i+1;j<n-(k-2);j++){
	if(2<k){
	  for(l=j+1;l<n-(k-3);l++){
	    if(3<k){
	      for(t=l+1;t<n-(k-4);t++){
		ku[0]=i;
		ku[1]=j;
		ku[2]=l;
		ku[3]=t;
		kumi();
	      }
	    }
	    else{
	      ku[0]=i;
	      ku[1]=j;
	      ku[2]=l;
	      kumi();
	    }
	  }
	}
	else{
	  ku[0]=i;
	  ku[1]=j;
	  kumi();
	}
      }
    }

    /*
      for(i=0;i<n;i++){
      cout << c[i] <<endl;
    }
    puts("");
    */
    cout << ans << endl;
  }
}

void kumi(){
    if(k==2) ku2();
    else if(k==3) ku3();
    else ku4();
}

void ku2(){
  string temp;
  temp=c[ku[0]];
  temp+=c[ku[1]];
  if(memo[temp]!=true){
    memo[temp]=true;
    ans++;
    //    cout << "ku2..." << temp <<endl;
  }
  temp=c[ku[1]];
  temp+=c[ku[0]];
  if(memo[temp]!=true){
    memo[temp]=true;
    ans++;
    // cout << "ku2..." << temp <<endl;
  }
}

void ku3(){
  int i,j,l;
  string temp;
  for(i=0;i<3;i++){
    for(j=0;j<3;j++){
      if(i!=j){
	for(l=0;l<3;l++){
	  if(l!=i && l!=j){
	    temp=c[ku[i]];
	    temp+=c[ku[j]];
	    temp+=c[ku[l]];
	    if(memo[temp]!=true){
	      memo[temp]=true;
	      ans++;
	      //	      cout << "ku3..." << temp <<endl;
	    }
	  }
	}
      }
    }
  }
}

void ku4(){
  int i,j,l,t;
  string temp;
  for(i=0;i<4;i++){
    for(j=0;j<4;j++){
      if(i!=j){
	for(l=0;l<4;l++){
	  if(l!=i && l!=j){
	    for(t=0;t<4;t++){
	      if(t!=i && t!=j && t!=l){
		temp=c[ku[i]];
		temp+=c[ku[j]];
		temp+=c[ku[l]];
		temp+=c[ku[t]];
		if(memo[temp]!=true){
		  memo[temp]=true;
		  ans++;
		  //  cout << "ku4..." << temp <<endl;
		}
	      }
	    }
	  }
	}
      }
    }
  }
}

/*
  void shokika(){
  fill(t,t+100000005,false);
  }
*/

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

int n, k;
string a[10];
set<string> s;

void solve(vector<int> v) {
    do {
        string t = "";
        for (int i = 0; i < k; i++) {
            t.append(a[v[i]]);
        }
        s.insert(t);
    } while (next_permutation(v.begin(), v.end()));
}

int main() {
    cin >> n >> k;
    while (n != 0 && k != 0) {
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        s.clear();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (k == 2) {
                    solve(vector<int>{i, j});
                } else {
                    for (int l = j + 1; l < n; l++) {
                        if (k == 3) {
                            solve(vector<int>{i, j, l});
                        } else {
                            for  (int m = l + 1; m < n; m++) {
                                solve(vector<int>{i, j, l, m});
                            }
                        }
                    }
                }
            }
        }
        cout << s.size() << "\n";
        cin >> n >> k;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,k,c[100];
map<int,bool> ans;
int used[20];
void dfs(int s,int d,int p){
  if(d==k){ans[s]=1;return;}
  for(int i=0;i<n;i++){
    if(used[i])continue;
    int r=10;
    if(c[i]>9)r*=10;
    used[i]=1;
    dfs(s*r+c[i],d+1,i+1);
    used[i]=0;
  }
}

int main(){
  while(cin>>n>>k,n){
    for(int i=0;i<n;i++)cin>>c[i];
    memset(used,0,sizeof(used));
    ans.clear();
    dfs(0,0,0);
    cout<<ans.size()<<endl;
  }
  return 0;
}

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

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

using namespace std;

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

string func(vector<int> v, int k) {
	stringstream ss;
	rep(i, k) {
		ss << v[i];
	}

	return ss.str();
}

int main() {
	int n;
	while(cin >> n && n) {
		int k;
		cin >> k;
		
		vector<int> v(n);
		rep(i, n) cin >> v[i];

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

		set<string> ans;

		do {
			ans.insert(func(v, k));
		}while(next_permutation(v.begin(), v.end()));

		cout << ans.size() << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<stdlib.h>

int n,k,num,chk[101];
char str[256],card[10];

void kansu(int a[],int l){
	int i,j;
	if(l==k){
		for(i=0;i<k;i++){
			char tmp[256];
			sprintf(tmp,"%s%d",str,card[a[i]]);
			strcpy(str, tmp);
			
		}
		chk[num]=atoi(str);
		for(i=0;i<101;i++){
			if(chk[num]==chk[i]&&i!=num){
				chk[num]=0;
				num--;
			}
		}
		num++;
		
		str[0] = '\0';
	}else{
		int flag[10]={0};
		for(i=0;i<l;i++)flag[a[i]]=1;
		for(i=0;i<n;i++)if(flag[i]==0){
			a[l]=i;
			kansu(a,l+1);
		}
	}
}

int main(){
	int i,b[10];
	while(1){
		scanf("%d%d",&n,&k);
		if(n==0&&k==0)break;
		num=0;
		for(i=0;i<10;i++){
			card[i]=0;
			b[i]=0;
		}
		for(i=0;i<101;i++)chk[i]=0;
		for(i=0;i<n;i++)scanf("%d",&card[i]);
		kansu(b,0);
		printf("%d\n",num);
	}
}