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 <algorithm>
#include <string>
#include <vector>
using namespace std;

int n, k, ans;
string save, cards[11];
vector<string> v;

void solve(int a, int b, int c){
	for(int i = 1; i <= n; i++){
		if(a != b && a != c && a != i && b != c && b != i && c != i){
			if(k == 3){
				v.push_back(save + cards[i]);
			} else{
				if(c == -1){
					solve(a, b, i);
				} else{
					v.push_back(save + cards[c] + cards[i]);
				}
			}
		}
	}
}

int main() {
	while(1){
		cin >> n >> k;
		if(n == 0 && k == 0){
			break;
		}
		
		for(int i = 1; i <= n; i++){
			cin >> cards[i];
		}
		
		for(int a = 1; a <= n; a++){
			for(int b = 1; b <= n; b++){
				if(a != b){
					save = cards[a] + cards[b];
					if(k >= 3){
						solve(a, b, -1);
					} else{
						v.push_back(save);
					}
				}
			}
		}
		
		ans = 1;
		sort(v.begin(), v.end());
		for(int i = 1; i < v.size(); i++){
			if(v[i] != v[i - 1]){
				ans++;
			}
		}
		
		cout << ans << endl;
		
		int x = v.size();
		for(int i = 0; i < x; i++){
			v.pop_back();
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector <string> card,memo;
string suzi,m;
int n,k,ans,p,but;


int main(){

    while(1){

        cin >> n >> k;
        if(n==0&&k==0)break;

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

        if(k==2){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(i!=j){
                        suzi=card[i]+card[j];
                        but=0;
                        while(1){
                            if(p==ans)break;
                            if(memo[p]!=suzi);
                            else {
                                but++;
                                break;
                            }
                        p++;
                        }
                        p=0;
                        if(but==0){
                                ans++;
                               memo.push_back(suzi);
                    }
                }
                but=0;
            }
        }

    }

        if(k==3){
                 for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        for(int a=0;a<n;a++){
                    if(i!=j&&j!=a&&a!=i){
                        suzi=card[i]+card[j]+card[a];
                        but=0;
                        while(1){
                            if(p==ans)break;
                            if(memo[p]!=suzi);
                            else {
                                but++;
                                break;
                            }
                        p++;
                        }
                        p=0;
                        if(but==0){
                                ans++;
                               memo.push_back(suzi);
                    }
                }
                but=0;
                }
            }
        }

        }

        if(k==4){

                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        for(int a=0;a<n;a++){
                            for(int b=0;b<n;b++){
                    if(i!=j&&i!=a&&i!=b&&j!=a&&j!=b&&a!=b){
                        suzi=card[i]+card[j]+card[a]+card[b];
                        but=0;
                        while(1){
                            if(p==ans)break;
                            if(memo[p]!=suzi);
                            else {
                                but++;
                                break;
                            }
                        p++;
                        }
                        p=0;
                        if(but==0){
                                ans++;
                               memo.push_back(suzi);
                    }
                }
                but=0;
                    }
                }
            }
        }

        }

        cout << ans << endl;
        memo.clear();
        card.clear();
        ans=0;
    }


return 0;
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int n,k,a[10],power[6];
vector<string>vec;
int main(){
	while(true){
		cin>>n>>k;vec.clear();power[0]=1;for(int i=1;i<5;i++)power[i]=power[i-1]*n;
		if(n==0 && k==0)break;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<power[k];i++){
			int bit[4];for(int j=0;j<k;j++)bit[j]=(i/power[j])%n;
			bool OK=true;
			if(k==2 && (bit[0]==bit[1]))OK=false;
			if(k==3 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==4 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==4 && (bit[0]==bit[3] || bit[1]==bit[3] || bit[2]==bit[3]))OK=false;
			if(OK==true){
				string S="";
				for(int j=0;j<k;j++)S+=to_string(a[bit[j]]);
				vec.push_back(S);
			}
		}
		sort(vec.begin(),vec.end());int cnt=1;
		for(int i=1;i<vec.size();i++){
			if(vec[i-1]!=vec[i])cnt++;
		}
		cout<<cnt<<endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <set>
using namespace std;
int card[10],n,k;
bool used[10];
vector<int> V;
set<int> S;
void solve(int x) {
	if(x==k) {
		int p=0,q=1;
		for(int i=0;i<V.size();i++) {
			p+=(V[i]%10)*q; q*=10;
			if(V[i]/10) p+=(V[i]/10)*q,q*=10;
		}
		S.insert(p);
		return;
	}
	for(int i=0;i<n;i++) {
		if(!used[i]) {
			used[i]=true;
			V.push_back(card[i]);
			solve(x+1);
			V.pop_back();
			used[i]=false;
		}
	}
}
int main() {
	while(scanf("%d",&n),n) {
		scanf("%d",&k);
		for(int i=0;i<n;i++) scanf("%d",&card[i]);
		S.clear();
		solve(0);
		printf("%d\n",S.size());
	}
}

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

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

int n,k;
int card[16];
int check[16];
set<string> ss;

string tos(int a){
	char str[100];
	int i=0;
	while( a>0 ){
		str[i++] = a%10 + '0';
		a /= 10;
	}
	str[i] = 0;
	string s = string(str);
	return string(s.rbegin(), s.rend());
}
void solve(int cnt, string str){
	//printf("%d , %s\n",cnt,str.c_str());
	if( cnt==0 ){
		ss.insert(str);
		return;
	}
	for(int i=0; i<n; i++)if( !check[i] ){
		check[i] = 1;
		solve(cnt-1, str + tos(card[i]));
		check[i] = 0;
	}
	return;
}
int main(){
	while(scanf("%d%d",&n,&k),n|k){
		ss.clear();
		rep(i,n)scanf("%d",&card[i]);
		solve(k,"");
		cout << ss.size() << endl;
	}
	
	return 0;
}