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<bits/stdc++.h>
using namespace std;
signed main(){
  int n;
  while(cin>>n,n){
    int k;
    cin>>k;
    string s[n];
    for(int i=0;i<n;i++) cin>>s[i];
    set<string> ss;
    function<void(int,int,string)> dfs=[&](int c,int d,string t) {
      if(d==k){
	ss.insert(t);
	return;
      }
      for(int i=0;i<n;i++){
	if((c>>i)&1) continue;
	dfs(c+(1<<i),d+1,t+s[i]);
      }
    };
    dfs(0,0,"");
    cout<<ss.size()<<endl;
  }
  return 0;
}

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

int main(){
    int n,k;
    while(1){
        cin>>n>>k;
        string S[11];
        if(n==0&&k==0) break;
        for(int i=0; i<n; i++) cin>>S[i];
        sort(S,S+n);
        set<string> SS;
        do{
            string str;
            for(int i=0; i<k; i++) str+=S[i];
            SS.insert(str);
        }while(next_permutation(S, S+n));
        cout<<SS.size()<<endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<list>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<set>
using namespace std;
set<int>a;
void solve(int b[10],int i,string s,bool c[10],int n,int k) {
	for (int j = 0; j < n; j++) {
		if (c[j]) {
			if (i == k&&c[j]) {
				string u = s;
				s += to_string(b[j]);
				a.insert(stoi(s));
				s = u;
			}
			else {
				c[j] = false;
				solve(b, i+1, s+to_string(b[j]), c, n, k);
				c[j] = true;
			}
		}
	}
}
int main() {
	int n, k;
	while (cin >> n >> k&&n != 0 && k != 0) {
		a.clear();
		int b[10];
		bool c[10];
		for (int i = 0; i < n; i++) {
			cin >> b[i];
			c[i] = true;
		}
		k--;
		for (int i = 0; i < n; i++) {
			c[i] = false;
			solve(b, 1, to_string(b[i]),c,n,k);
			c[i] = true;
		}
		cout << a.size() << endl;
	}
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int N,K;
    string str[11];
    string lis[10021];
    cin>>N>>K;
    for(int i=0;i<N;i++)cin>>str[i];
    int ii=0;
    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&&j!=k&&k!=l&&i!=k&&j!=l&&i!=l){
                        if(K==2)lis[ii]=str[i]+str[j];
                        if(K==3)lis[ii]=str[i]+str[j]+str[k];
                        if(K==4)lis[ii]=str[i]+str[j]+str[k]+str[l];
                        ii++;
                    }
                }
            }
        }
    }
    sort(lis,lis+10012);
    int ans=0;
    for(int i=0;i<10010;i++)if(lis[i]!=lis[i+1])ans++;
    cout<<ans<<endl;
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
 
using namespace std;
 
int n,k,ans;
vector<string> V;
map<string,bool> M;
 
void init(){
  V.clear();
  M.clear();
  ans = 0;
}
 
void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  if(rev == k){
    if(M[s] == false){
      M[s] = true;
      ans++;
    }
    return;
  }
 
  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0)
      solve(s+V[i],rev+1,used+(1<<i));
   
}
 
int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

C++: #import<iostream>
#import<set>
#import<algorithm>
using namespace std;main(){int n,k,c[10],i,t;for(;cin>>n>>k,i=n;){for(;i--;)cin>>c[i];sort(c,c+n);set<int>s;do{for(i=t=0;i<k;++i)t=t*(c[i]>9?100:10)+c[i];s.insert(t);}while(next_permutation(c,c+n));cout<<s.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};
	scanf("%d%d", &n, &m);
	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;
}
/*
4 2
1 2 12 1
*/

C++: #include<iostream>
#include<set>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cstring>

#define MAX 128
using namespace std;
set<int> seed;

void perm(int cards[], int com[], int p[], int n)
{
  int *c,*pc,*q,k,t;
  
  c = new int[n];
  for(k=1,q=p,pc=c;k<=n;)*q++=*pc++=k++;k=1;pc=c;
  do{t=*(p+k);*(p+k)=*(q=p+((k&1)?*pc:0));*q=t;
    
    //TODO
    ostringstream oss;
    for(int i = 0; i < n; ++i){
      oss << cards[ com[ p[i]-1 ] ];
    }
    seed.insert( atoi(oss.str().c_str()) );
    
    k=1;pc = c;while(*pc==0)*pc++=k++;(*pc)--;}while(k<n);
  delete [] c;
}

void comb(int cards[], int n, int k)
{
  int *p;
  bool loop = false;
  
  p = new int[k];
  for(int i = 0; i < k; i++) p[i] = i;
  
  do{ loop = false;
    
    // TODO
    int tmp[MAX];
    perm(cards,p,tmp,k);
    
    for(int pk=k-1;pk>=0;pk--){p[pk]++;if(p[pk]<=n-(k-pk)){for(int j=pk+1;j<k;j++)p[j]=p[j-1]+1;pk++;loop=true;break;}}
  }while(loop);
  delete [] p;
}

int main(){
  while(true){
    int cards[MAX];
    int n,k;scanf("%d%d", &n, &k);
    if(n==0&&k==0)break;
    for(int i = 0; i < n; ++i){
      scanf("%d",&cards[i]);
    }
    comb( cards, n, k );
    cout << seed.size() << endl;
    seed.clear();
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define LL long long
static const LL INF = 1<<50;

using namespace std;

int n,k,ans;
LL s[10000000];
string num[11];
void cal(string sum,int depth,int bit){
    int res=0;
    if(depth==k){
        int num;
        num=atoi(sum.data());
        s[num]=num;
        //cout<<s[num]<<endl;
    }
    for(int i=1;i<=n;++i){
        int b=1<<i;
        if(bit&b){
            int k=bit-b;
            cal(sum+num[i],depth+1,k);
        }
    }
}

int main(){
    while(1){
        memset(s,0,sizeof(s));
        int p=1;
        cin>>n>>k;
        if(n==0&&k==0)break;
        string a;
        for(int i=1;i<=n;++i){
            p+=pow(2,i);
        }
        for(int i=1;i<=n;++i){
            cin>>num[i];
        }
        cal(a,0,p);
        sort(s,s+114514);
        for(int i=0;i<114514;++i){
            if(s[i]&&s[i]!=s[i+1]){
                ans++;
            }
        }
        cout<<ans<<endl;
        ans=0;
    }
}