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<stdio.h>
#include<string.h>
#include<stdlib.h>
//atoi(str)

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);
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

set<string> ans;
vi in;
vi selected;

void dfs(int k,int pos){
  //cout<<"dfs "<<k<<" "<<pos<<endl;
  if(k==0){
    sort(ALL(selected));
    do{
      string str;
      rep(i,selected.size()){
        stringstream ss;
        ss<<selected[i];
        str+=ss.str();
      }
      ans.insert(str);
    }while(next_permutation(ALL(selected)));
    sort(ALL(selected));
    reverse(ALL(selected));
  }else{
    for(int i=pos;i<in.size();i++){
      selected[k-1]=in[i];
      dfs(k-1,i+1);
    }
  }
}

main(){
  int n,k;
  while(cin>>n>>k,k|n){
    in.clear();
    ans.clear();
    selected.clear();
    selected.resize(k);
    rep(i,n){
      int t;
      cin>>t;
      in.pb(t);
    }
    sort(ALL(in));

    
    dfs(k,0);
    cout<<ans.size()<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int n, m, a[10], ans = 0;
map<int,int> mp;

int solve(int tmp[4], int k, int sum, int p){
	int t, u[4];
	rep(i,4)u[i] = tmp[i];
	u[k++] = p;
	if(a[p] >= 10)sum = sum*100+a[p];
	else sum = sum*10+a[p];
	
	if(m == k){
		if(mp[sum]==0)ans++;
		mp[sum] = 1;
		return 0;
	}
	
	for(int i = 0;i < n;i++){
		t = 1;
		rep(j,k)if(u[j] == i)t = 0;
		if(t)solve(u, k, sum, i);
	}
	return 0;
}

int main(){
	int u[4] = {0,0,0,0};
	while(1){
		scanf("%d%d", &n, &m);
		if(!n)break;
		rep(i,n)scanf("%d", &a[i]);
		for(int i = 0;i < n;i++){
			solve(u, 0, 0, i);
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

vector<string> card;
set<int> used;
set<int> ans;
int s[4];
int n,k;

void saiki(int t){
    if(t == k){
	string ss = "";
	REP(i,k) ss += card[s[i]];
	ans.insert(stoi(ss));
	return;
    }
    REP(i,n){
	if(used.find(i) == used.end()){
	    used.insert(i);
	    s[t] = i;
	    saiki(t+1);
	    used.erase(i);
	}
    }
}

int main(){
    while(true){
	cin >> n >> k;
	if(n==0&&k==0) break;
	card = vector<string>(n);
	REP(i,n){
	    cin >> card[i];
	}
	saiki(0);
	cout << ans.size() << endl;
	ans.clear();
	used.clear();
    }
    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#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;

int main(){
    int n, k, C[100];
    int checked_num[1000000]={};
    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<1000000; i++)
            ans+=checked_num[i];
        cout << ans << endl;

        memset(C, 0, 100*sizeof(int));
        memset(checked_num, 0, 1000000*sizeof(int));
    }
    return 0;
}

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

int bind(int a, int b){
  int x = 1;
  while(b >= x)x *= 10;
  return a * x + b;
}

int a[1080];

void solve(int n, int K){

  set<int> s;
  
  for(int i = 0;i < n;i++){
    cin >> a[i];
  }
  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;
	  int ans = a[i];
	  if(K >= 2)ans = bind(a[j], ans);
	  if(K >= 3)ans = bind(a[k], ans);
	  if(K >= 4)ans = bind(a[l], ans);
	  s.insert(ans);
	}
      }
    }
  }

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

int main(){
  int n, k;
  while(true){
    cin >> n >> k;
    if(n == 0 && k == 0)return 0;
    solve(n, k);		       
  }
}