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++: /* AOJ 0546
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0546
 *
 */


//include
#include<bits/stdc++.h>

//using
using namespace std;

//define static const typedef
#define FOR(i, s, n) for(int (i) = (s); (i) < (n); (i)++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\n";
#define DEBUG(x) cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;

//global
static set<pair<string, VS> > e;
static int n, k, ans;
static VS str_array;
static VS ans_string;
static char b[2];
//function
static void solve(int many_used, string now_string, VS can_use){
  if(e.count(pair<string, VS>(now_string, can_use))){
      return;
  }
  e.insert(pair<string, VS>(now_string, can_use));
  if(k == many_used && !binary_search(ALL(ans_string), now_string)){
    ans++;
    ans_string.PB(now_string);
    SORT(ans_string);
    //DEBUG(now_string)
    return;
  }
  REP(i, can_use.size()){
    string str = can_use[i];
    can_use.erase(can_use.begin() + i);
    solve(many_used + 1, now_string + str, can_use);
    can_use.PB(str);
    SORT(can_use);
  }
}
int main(){
  while(scanf("%d %d", &n, &k) && n && k){
    REP(i, n){
      scanf("%s", b);
      str_array.PB((string)b);
    }
    ans = 0;
    SORT(str_array);
    solve(0, "", str_array);
    printf("%d\n", ans);
    str_array = VS();
    e = set<pair<string,VS> >();
    ans_string = VS();
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<sstream>
#include<vector>
#include<set>
using namespace std;

int n, k;
char card[10];
set<int>ans;
void solve(int a, vector<char>vec, vector<bool>b) {
	vec.push_back(card[a]);
	b[a] = true;
	if (vec.size() == k) {
		ostringstream oss;
		for (char i : vec)
			oss << (int)i;
		ans.insert(stoi(oss.str()));
		return;
	}
	for (int i = 0; i < n; i++) {
		if (!b[i])
			solve(i, vec, b);
	}
}
int main(){
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; i++)
		scanf("%d", &card[i]);
	vector<char>vec;
	vector<bool>b(10);
	for (int i = 0; i < n; i++)
		solve(i, vec, b);
	printf("%d\n", ans.size());
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
set<string> st;
int n,k;
int flag[10];
string v[10];
void dfs(int maisu,string s){
  if(maisu==k){
    st.insert(s);
    return;
  }
  for(int i=0;i<n;i++){
    if(flag[i]==0){
      flag[i]=1;
      dfs(maisu+1,s+v[i]);
      flag[i]=0;
    }
  }
  return;
}
  
int main(){
  cin>>n>>k;
  for(int i=0;i<n;i++){
    cin>>v[i];
    flag[i]=0;
  }
  
  st.clear();
  dfs(0,"");
  cout<<st.size()<<endl;
}

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;
int main(){
      int n,k;
      while(cin>>n>>k&&n&&k){
            string t[n];
            set<string> f;
            REP(i,n)cin>>t[i];
            sort(t,t+n);
            do{
                  string res="";
                  REP(i,k)res+=t[i];
                  f.insert(res);
            }while(next_permutation(t,t+n));
            cout<<f.size()<<endl;
      }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<functional>
#include<queue>
#define INF 999999

using namespace std;

int bind(int a[], int k)
{
	int i = 1;
	int j = 0;
	int b=0;
	while (j < k) {
		b += a[j] * i;
		if(a[j]<10)i *= 10;
		else i *= 100;
		j++;
	}
	return b;
}

int main()
{
	int p = 0;
	int b[5];
	while (1) {
		int n, k;
		cin >> n >> k;
		if (n == 0 && k == 0) {
			break;
		}

		vector<int> card;
		for (int i = 0; i < n; i++) {
			int a;
			cin >> a;
			card.push_back(a);
		}

		set<int> s;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int l = 0; l < n; l++) {
					for (int m = 0; m < n; m++) {
						if (i != j&&i != l&&i != m&&j != l&&j != m&&l != m) {
							int b[4] = { card[i],card[j],card[l],card[m] };
							int c = bind(b, k);
							s.insert(c);
						}
					}
				}
			}
		}
		b[p] = s.size();
		p++;
	}
	for (int i = 0; i < p; i++) {
		cout << b[i] << endl;
	}
	return 0;

}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<sstream>
#include<cstdlib>

using namespace std;

int main(){
	while(1){
		int n,k;
		scanf("%d%d",&n,&k);
		
		if(n==0)
			break;
		
		vector<int> card(n);
		for(int i=0;i<n;i++){
			scanf("%d",&card[i]);
		}
		
		sort(card.begin(),card.end());
		
		vector<int> data;
		do{
			
			stringstream sst;
			for(int i=0;i<k;i++){
				sst<<card[i];
			}
			data.push_back(atoi(sst.str().begin()));
			
			
			reverse( card.begin()+k,card.end());
		}while( next_permutation(card.begin(),card.end()) );
		
		sort(data.begin(),data.end());
		
		
		printf("%d\n",distance(data.begin(),unique(data.begin(),data.end())) );
	}
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;
const int INF=1000000;

int main(){
    int n, k, C[1000];
    int checked_num[INF]={};
    while(1){
        cin >> n >> k;
        if(n==0 && k==0) break;
        for(int i=0; i<n; i++)
            cin >> C[i];
        sort(C, C+n);

        // 全ての組み合わせを考える
        do{
            string num="";
            for(int i=0; i<k; i++)
                num+=to_string(C[i]);
            checked_num[stoi(num)]=1;
        }while(next_permutation(C, C+n));

        int ans=0;
        for(int i=0; i<INF; i++)
            ans+=checked_num[i];
        cout << ans << endl;

        memset(C, 0, 1000*sizeof(int));
        memset(checked_num, 0, INF*sizeof(int));
    }
    return 0;
}