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

using namespace std;

void solve()
{
	int n;
	int sheet;
	while(cin >> n >> sheet)
	{
		if(!n && !sheet)
		{
			break;
		}
		vector<string> Vec(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		set<string> Set;
		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 PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define INF 1000000001
#define MOD 100000
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const ull B=100000007;
string rec(int x){
      string res="";
      while(x>0){
            res+=x%10+'0';
//            cout<<res<<endl;
            x/=10;
      }
      reverse(ALL(res));
      return res;
}
int main(){
      int n,k;
      while(cin>>n>>k&&n&&k){
            int t[n];
            REP(i,n)cin>>t[i];
            sort(t,t+n);
            set<string> f;
            do{
                  string ans="";
                  REP(i,k)ans+=rec(t[i]);
                  f.insert(ans);
            }while(next_permutation(t,t+n));
            cout<<f.size()<<endl;
      }
}

C++: #include "bits/stdc++.h"

#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define all(co) co.begin(), co.end()
using namespace std;
//using P = pair<int, int>;

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

		set<string> sets;
		vector<bool> used(n);
		function<void(string, int)> dfs = [&](string str, int count) {
			if (count == k) {
				sets.insert(str);
				return;
			}
			rep(i, n) if (!used[i]) {
				used[i] = true;
				dfs(str + vec[i], count + 1);
				used[i] = false;
			}
		};
		dfs("", 0);
		cout << sets.size() << endl;
	}
	return 0;
}

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

using namespace std;

set<int> s;
vector<int> cards;
int n,k;

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


/*void solve(long long a, int cnt){
    if(cnt>=n)return a;
    r=(cards[cnt]/10)?100:10;
    s.insert(a*r+cards[cnt]);
    solve(a*r+cards[cnt], cnt+1);
    solve(a,cnt+1);
}*/


int main(void){
    // Here your code !
    while(cin>>n>>k,n||k){
        s.clear();cards.clear();
        rep(i,n){
            int t;
            cin>>t;
            cards.push_back(t);
        }
        sort(cards.begin(),cards.end());
        do{
            long long res=0;
            rep(i,k){
                int c=cards[i];
                long long r=c/10?100:10;
                res=res*r+c;
            }
            s.insert(res);
        }while(next_permutation(cards.begin(),cards.end()));
        /*for(auto i = s.begin(); i!=s.end(); i++)cout<<*i<<"\t";
        cout<<endl;*/
        cout<<s.size()<<endl;
    }
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s); sin>>v; return v;}
template<class T> inline string toString(T x) {ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

void dfs(vi &cards,int k,int pos,string cur,vi &used,set<string> &s){
	if(pos==k){
		s.insert(cur);
		return;
	}
	REP(i,cards.size()){
		if(!used[i]){
			string ncur=cur+toString(cards[i]);
			vi nused(used);
			nused[i]=1;
			dfs(cards,k,pos+1,ncur,nused,s);
		}
	}
}

int main(){
	int n;
	while(cin>>n,n){
		int k;
		cin>>k;
		vi cards(n);
		REP(i,n){
			cin>>cards[i];
		}
		string cur;
		vi used(n);
		set<string> s;
		dfs(cards,k,0,cur,used,s);
		cout<<s.size()<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  while(true) {
  int N,M;
  cin >> N;
  if(N == 0) break;
  cin >> M;
  vector<string> A(N);
  for(int i=0; i<N; i++) cin >> A.at(i);
  
  unordered_set<string> res;
  
  for(int i=0; i<N; i++){
    string a = A.at(i);
    
    for(int j=0; j<N; j++){
      if( i == j ) continue;
      string b = a + A.at(j);
      if( M==2 ) res.insert(b);
      
      for(int k=0; k<N; k++){
        if( i == k || j == k ) continue;
        string c = b + A.at(k);
        if( M==3 ) res.insert(c);
        
         for(int l=0; l<N; l++){
           if( i == l || j == l || k == l ) continue;
           string d = c + A.at(l);
           if( M==4 ) res.insert(d);
         }
       }
    }
  }
  
  cout << res.size() << endl;
  }
  return 0;
}