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<iostream>
#include<string>

using namespace std;
int main()
{

	bool p[1000001];
	int n, k;
	string m[1000];
	string a;
	while (1){
		
		cin >> n >> k;
		if (n == 0)return 0;
		for (int i = 0; i < n; i++){
			cin >> m[i];
		}
		if (k == 4){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){
					for (int i3 = 0; i3 < n; i3++){
						for (int i4 = 0; i4 < n; i4++){
							if (i != i2&&i3 != i4&&i != i3&&i != i4&&i2 != i3&&i2 != i4){
								a = m[i] + m[i2] + m[i3] + m[i4];
								p[atol(a.c_str())] = false;
								a.clear();
							}
						}
					}
				}
			}

		}
		long u = 0;
		if (k == 3){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){
					for (int i3 = 0; i3 < n; i3++){
						if (i != i2&&i != i3&&i2 != i3){
							a = m[i];
							a += m[i2];
							a += m[i3];

							u = atol(a.c_str());
							p[u] = false;
							a.clear();
						}
					}
				}
			}
		}
		int o;
		if (k == 2){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){

					if (i != i2){
						a = m[i] + m[i2];
						o = atoi(a.c_str());


						p[o] = false;
						a.clear();
					}
				}
			}
		}
		int count = 0;
		for (int i = 0; i < 100000; i++){
			if (!p[i])count++;
		}
		cout << count << endl;
	}
}

C++: #include<stdio.h>
#define INF 100000
int min(int a,int b){return a<b?a:b;}
int getk(int a){
	int res=0;
	while(a>0){
		res++;
		a/=10;
	}
	return res;
}
int pow(int a,int b){
	int l;
	int rt=1;
	for(l=0;l<b;l++)rt*=10;
	return rt;
}
int card[10];
bool pos[100000000];
int main(){
	int i,j;
	int n,k;
	while(1){
	scanf("%d",&n);
	scanf("%d",&k);
		if(n==0)return 0;
	for(i=0;i<n;i++)scanf("%d",&card[i]);
	for(i=0;i<20000000;i++)pos[i]=false;
	int max=1;
	int ket;
	int next;
	int ni;
	int num;
	int ans=0;
	bool used[10];
	for(i=0;i<k;i++)max*=n;
	for(i=0;i<max;i++){
cont:		
		for(j=0;j<n;j++)used[j]=false;
		ket=1;
		ni=i;
		num=0;
		for(j=0;j<k;j++){
			next=ni%n;
			ni/=n;
			if(used[next]){
				i++;
				if(i==max)goto end;
				goto cont;
			}
			else used[next]=true;
			num+=card[next]*ket;
			ket*=pow(10,getk(card[next]));
		}
		if(!pos[num]){
			ans++;
			pos[num]=true;
		}
	}
end:
	printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <iomanip>
using namespace std;
int n, k;
int main(){
    while(cin >> n >> k, n||k){
        vector<string> v;
        for(int i=0; i<n; ++i){
            string st; cin >> st;
            v.push_back(st);
        }
        sort(v.begin(), v.end());
        
        set<string> st;
        do{
            string num="";
            for(int i=0; i<k; ++i){
                num += v[i];
            }
            st.insert(num);
        }while(next_permutation(v.begin(), v.end()) );
        cout << st.size() << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
set<int> s;
int n,k;
vector<int> num;
void dfs(int t,int ret,vector<int> use){
	if(t==k)	s.insert(ret);
	else{
		for(int i=0;i<n;i++){
			if(use[i])	continue;
			use[i]=1;
			if(num[i]<10)	dfs(t+1,ret*10+num[i],use);
			else 	dfs(t+1,ret*100+num[i],use);
			use[i]=0;
		}
	}
}
int main(){
	while(true){
		cin>>n>>k;
		if(n==0)	return 0;
		s.clear();
		num.clear();
		for(int i=0;i<n;i++){
			int a;	cin>>a;
			num.push_back(a);
		}
		dfs(0,0,vector<int>(n,0));
		cout<<s.size()<<endl;
	}
}

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

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

void add(int depth,string str){
	if(depth==k){
		s.insert(str);
		return;
	}
	for(int i=0;i<n;i++){
		if(!used[i]){
			used[i]=true;
			string t;
			if(9<a[i])t += a[i]/10+'0';
			t += a[i]%10+'0';
			add(depth+1,str+t);
			used[i]=false;
		}
	}
}

int main(){
	while(cin>>n>>k,n||k){
		for(int i=0;i<n;i++)cin>>a[i];
		memset(used,false,sizeof(used));
		s.clear();
		add(0,"");
		cout<<s.size()<<endl;
	}
	return 0;
}

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

int main(){
	int n,k;
	while(cin>>n>>k,n,k){
		string sa[10];
		for(int i=0;i<n;i++){cin>>sa[i];}
		
		vector<string>ans;
		
		if(k==2){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(i!=j){
						string s=sa[i];
						s+=sa[j];
						
						int flg=0;
						for(int i=0;i<ans.size();i++){
							if(ans[i]==s){
								flg=1;
								break;
								}
							}
							
						if(flg==0)ans.push_back(s);	
						}
					}
				}
			}
			
		if(k==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){
							string s=sa[i];
						s+=sa[j];
						s+=sa[k];
						
						int flg=0;
						for(int i=0;i<ans.size();i++){
							if(ans[i]==s){
								flg=1;
								break;
								}
							}
							
						if(flg==0)ans.push_back(s);	
							}
						}
					}
				}
			}	
			
		if(k==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&&j!=k&&k!=l&&i!=k&&i!=l&&j!=l){
								string s=sa[i];
						s+=sa[j];
						s+=sa[k];
						s+=sa[l];
						
						int flg=0;
						for(int i=0;i<ans.size();i++){
							if(ans[i]==s){
								flg=1;
								break;
								}
							}
							
						if(flg==0)ans.push_back(s);	
								}
							}
						}
					}
				}
			}	
			
			cout<<ans.size()<<endl;
		}
		return 0;
	}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <fstream>	// require freopen

using namespace std;

typedef long long ll;

int main()
{
//	cut here before submit 
//	freopen ("testcase.lining_up_card", "r", stdin );
	int n, k;

	while (cin >> n >> k && n && k ) {
//		vector <string> card (n );
		vector <int> card (n, 0 );
		for (int i = 0; i < n; ++i ) {
			cin >> card[i];
		} // end for
		sort (card.begin(), card.end() );
		set <ll> res;

		do{
			ll sum = 0LL;
			for (int i = 0; i < k; ++i ){
				if (card[i] < 10 ){
					sum *= 10LL;
				}else{ 
					sum *= 100LL;
				} // end if
				sum += (ll)card[i];
//				cout << card[i] << ' ';
			} // end for

//			if (!res.count (sum ) ){
//				cout << "sum: " << sum << endl;
			res.insert (sum );
//			}else{
//				cout << endl;
//			} // end if
		}while ( next_permutation (card.begin(), card.end() ) );

		cout << res.size() << endl;
		
	} // end loop
		
	return 0;
}