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<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 1e+8
#define EPS 1e-8
#define rep(i,j) for(int i = 0; i < (j); i++)
#define reps(i,j,k) for(int i = (j); i < (k); i++)
typedef long long ll;
typedef pair < int , int > Pii;
class P{
    public:
    int num;
    int cnt;
    bool used[10];
    P(){}
    P(int num,int cnt,bool a[10]):num(num),cnt(cnt){
        for(int i = 0; i < 10; i++){
            used[i] = a[i];
        }
    }
};
int bond(int x,int y){
    stringstream ss1;
    ss1 << x;
    stringstream ss2;
    ss2 << y;
    string ans = ss1.str() + ss2.str();
    stringstream Ans;
    Ans << ans;
    int res;
    Ans >> res;
    return res;
}
int main(){
    int n;
    int m;
    while(scanf("%d%d",&n,&m),(n|m)){
        int card[10];
        set<int> S;
        queue<P> Q;
        for(short i = 0; i < n; i++){
            bool x[10]={0};
            x[i] = 1;
            scanf("%d",&card[i]);
            Q.push(P(card[i],1,x));
        }
        short ans = 0;
        while( Q.size() ){
            P p = Q.front();Q.pop();
            if(p.cnt == m){
                if(S.find(p.num) == S.end())ans++,S.insert(p.num);
            }
            else{
                for(int i = 0; i < n; i++){
                    if(p.used[i] == 0){
                        p.used[i] = 1;
                        Q.push(P(bond(p.num,card[i]),p.cnt+1,p.used));
                        p.used[i] = 0;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

C++: #include <stdio.h>
#include <limits.h>
#include <math.h>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem0546 : Lining up the cards **/

vector<string> card;
bool used[10];
set<string> ans;
int N, K;

void solve(int n, string s)
{
	if (n==K) ans.insert(s);
	else {
		rep(i, N) {
			if (!used[i]) {
				used[i] = true;
				solve(n+1, s+card[i]);
				used[i] = false;
			}
		}
	}
}

int main()
{
	while (cin>>N, N) {
		ans.clear();
		card.clear();
		fill(used, used+10, false);
		
		cin>>K;
		rep(i, N) {
			string s; cin>>s;
			card.push_back(s);
		}
		solve(0, "");
		cout << ans.size() << endl;
	}
}

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

vector<int> ary;

int myp(int x,int y){
	int a;
	a=1;
	for(int i=0;i<y;i++)a*=x;
	return a;
}

int main(){
	int N,K,tmp,ket,hog;
	set<int> s;
	set<int>::iterator it;
	while(1){
		cin>>N>>K;
		if(N==0&&K==0) break;
		for(int i=0;i<N;i++){
			cin>>tmp;
			ary.push_back(tmp);
		}
		s.clear();

		sort(ary.begin(),ary.end());
		do{
			ket=hog=0;
			for(int i=0;i<K;i++){
				hog+=ary[i]*myp(10,ket);
				//?????????
				if(ary[i]>9){
					ket+=2;
				}
				else{
					ket++;
				}
			}
			s.insert(hog);

		}while(next_permutation(ary.begin(),ary.end()));
		cout<<s.size()<<endl;
	}
	return 0;
}

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

using namespace std;

int n, k;
string data[10];
set<int> used;

int solve(){
  used.clear();
  string tmp;
  for(int i1=0;i1<n;++i1){
    tmp = data[i1];
    for(int i2=0;i2<n;++i2){
      if(i2 == i1) continue;
      tmp = data[i1] + data[i2];
      if(k>2){
	for(int i3=0;i3<n;++i3){
	  if(i3 == i1 || i3 == i2) continue;
	  tmp = data[i1] + data[i2] + data[i3];
	  if(k>3){
	    for(int i4=0;i4<n;++i4){
	      if(i4 == i1 || i4 == i2 || i4 == i3) continue;
	      tmp = data[i1] + data[i2] + data[i3] + data[i4];
	      used.insert(atoi(tmp.c_str()));
	    }
	  }else used.insert(atoi(tmp.c_str()));
	}
      }else used.insert(atoi(tmp.c_str()));
    }
  }
  return (int)used.size();
}

main(){
  while(cin >> n >> k && (n || k)){
    for(int i=0;i<n;++i){
      cin >> data[i];
    }
    cout << solve() << endl;
  }
}

C++: #include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<string>

using namespace std;

set<string>mo;
int N,k;
bool use[15];
map<string,int>memo;
string n[10];
void g(int m,string a){
    if(m==k){
        memo[a]=1;
    return;
    }
        for(int i=0;i<N;i++){
        if(!use[i]){
                string _a=a+n[i];
            use[i]=1;
            g(m+1,_a);
            use[i]=0;
        }
    }
}
//??¨??????????????????(????????????)
int main(){
while(1){
        int ans=0;
        cin >> N >> k;
    if(N==0&&k==0)break;
    for(int y=0;y<N;y++){
        cin >> n[y];
    }
    fill(use,use+15,0);
    memo.clear();
    g(0,"");
    cout << memo.size() << endl;
}

return 0;
}

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

using namespace std;
int n,k;
int d[11];
int ans;
set<int> s;
void ch(int ca[11]){
	int t[11];
	for(int i = 0;i < k;i++)t[i] = ca[i];
	int kazo = 0;
	sort(t,t+k);
do{
	kazo++;
	int keta[5]={0};
	for(int i = 0;i < k-1;i++){
		keta[i+1] = keta[i];
		keta[i+1]++;
		if(t[i] >= 10)keta[i+1]++;
	}
	int co=0;
	for(int i = 0;i < k;i++){
		int p = t[i];
		while(keta[i]--)p*=10;
		co += p;
	}
	set<int>::iterator it = s.find(co);
	if(it == s.end()){
		ans++;
		s.insert(co);
	}



}while(next_permutation(t,t+k));
return;

}

void dfs(int i,int j,int o[11]){
	int h[11];
	for(int l = 0;l < k;l++) h[l] = o[l];
	if(i == k){
		ch(h);
		return;
	}
	for(int u = j;u < n;u++){
		h[i] = d[u];
		dfs(i+1,u+1,h);
	}
	return;
}



int main(){
	while(1){
	ans = 0;
	s.clear();
	int q[11];
	cin >> n >> k;
	if(n == 0 && k == 0)break;
	for(int i = 0;i < n;i++)cin >> d[i];
	dfs(0,0,q);
	cout << ans << endl;
	}
	return 0;
}