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 <string>
using namespace std;
int save[10000000], n, k, ans = 0, roop, num, save3, save4[10000000], roop2;
string card[10], res, save2, save5[10000000];
void solve(int a){
	roop--;
	for (int i = 0; i<n; i++){
		if(i==a) continue;
		if (save3 != i){
			if (roop == 0){
				save2 = res;
				res += card[i];
				num = stoi(res);
				if (save[num] == 0){
					ans++;
				}
				save[num] = 1;
				save[roop2] = num;
				roop2++;
				res = save2;
			}
			else{
				save5[i]=res;
				res += card[i];
				solve(i);
				roop++;
				res = save5[i];
			}
		}
	}
}
int main() {
	while (true){
		cin >> n >> k;
		roop2 = 0;
		if (n == 0 && k == 0){
			return 0;
		}
		for (int i = 0; i < n; i++){
			cin >> card[i];
		}
		for (int i = 0; i < n; i++){
			roop = k - 1;
			res = card[i];
			save3 = i;
			solve(i);
		}
		cout << ans << endl;
		for (int i = 0; i < 10000000; i++){
			if (save[save4[i]] == 0)
				break;
			save[save4[i]] = 0;
		}
	}
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<set>
#include<ctype.h>
#include<cstdlib>

using namespace std;

int arr[10];
int sub[10];
int n,k;
int now = 0;
int cont;
int c = 0;

set<int> visit2;

void saiki(int[],int);

int main()
{
	for(;;)
	{
		visit2.clear();
		int i;
		c = 0;
		now = 0;
		
		scanf("%d",&n);
		scanf("%d",&k);
		if(n == 0 && k == 0){
			break;
		}
		
		for(i = 0; i < n; i++){
			scanf("%d",&arr[i]);
		}
		for(i = 0; i < n; i++){
			sub[0] = arr[i];
			saiki(sub,1);
			now++;
		}
		cout <<visit2.size()-c << endl;
	}
}

void saiki(int temp[], int kk)
{
	//k:TõK
	int used[50] = {0};
	int i,j;
	int t = 0;
	char str[256] = "";
	
	for(i = 0; i < kk; i++){
		sprintf(str+strlen(str),"%d",temp[i]);
	}
	
	t = atoi(str);
	
	if(kk == k){
		visit2.insert(t);
		return;
	}
	else{
		for(i = 0; i < kk; i++){
			for(j = 0; j < n; ++j ){     
				if( used[j] == 0 && arr[j] == temp[i] ){
					used[j] = 1;
					break;
				}
			}
		}
		
		for(i = 0; i < n; i++){
			if(used[i] == 0){
				temp[kk] = arr[i];
				used[i] = 1;
				saiki(temp, kk+1);
				used[i] = 0;
			}
		}
	}
}

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

	vector<string>kst;//n枚分のカードの情報:kst
	vector<string>card;//並べ終わった後のカードを入れるvector:card
	int n,k;
	int flag[10]={1};
void solve(string now,int i){//カードのすべてのパターンを列挙する
		if(i==k){
			bool f=true;
			for(int j=0;j<card.size();j++){
			if(now==card[j])f=false;
			}
			
			if(f)card.push_back(now);
		}
		else 
		for(int j=0;j<n;j++){
		
			if(flag[j]!=-3){
				flag[j]=-3;
			solve(now+kst[j],i+1);
			flag[j]=1;
			
			}
		}
}

int main(){
	
	while(cin>>n>>k,n&&k){
	kst.clear();
	card.clear();
	for(int i=0;i<n;i++){
		string a;
		cin>>a;
		kst.push_back(a);
	}
	solve("",0);
	int count=0;
	cout<<card.size()<<endl;
}

}

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

using namespace std;

int n,k,ans;
LL s[10001];
string num[11];
int t=0;
void cal(string sum,int depth,int bit){
    int res=0;
    if(depth==k){
        int num;
        num=atoi(sum.data());
        s[t]=num;
        t++;
    }
    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){
        t=0;
        for(int i=0;i<11;++i)num[i]='0';
        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+t);
        for(int i=0;i<t;++i){
            if(s[i]&&s[i]!=s[i+1]){
                ans++;
            }
        }
        cout<<ans<<endl;
        ans=0;
    }
}

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

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

string card[15];

int main(void){
	int n,k;
	while(cin >> n >> k){
		if(n==0&&k==0) break;
		for(int i=0;i<n;++i) cin >> card[i];
		set<int> ans;
		sort(card,card+n);
		do{
			string cur="";
			for(int i=0;i<k;++i) cur+=card[i];
			ans.insert(stoi(cur));
		}while(next_permutation(card,card+n));
		cout << ans.size() << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define Rep(i,N) for(int i = 0; i < N; i++)

int N, K;
string s[15];

set<string>st;

int dfs(string w, int bit, int k)
{
  if(k == K) {
    if(st.find(w) == st.end()) {
      st.insert(w);
      return 1;
    }else {
      return 0;
    }
  }
  int ret = 0;
  Rep(i,N) {
    if(!((bit >> i) & 1)) {
      ret += dfs(w + s[i], bit | (1 << i), k + 1);
    }
  }
  return ret;
}
  

int main()
{
  cin >> N >> K;
  Rep(i,N) cin >> s[i];
  cout << dfs("",0,0) << endl;
  return 0;
}

C++: #include<set>
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int main()
{
    for(int n,k;cin>>n>>k,n;){
        int card[10];
        for(int i=0;i<n;i++) cin>>card[i];
        sort(card,card+n);
 
        set<int> st;
        bool mask[10]={};
        for(int i=0;i<k;i++) mask[i]=true;
        do{
            int chos[10];
            for(int i=0,j=0;i<n;i++) if(mask[i]) chos[j++]=card[i];
            do{
                int res=0;
                for(int i=0;i<k;i++){
                    if(chos[i]<10)   res=res*10+chos[i];
                    else            res=res*100+chos[i];
                }
                st.insert(res);
            }while(next_permutation(chos,chos+k));
        }while(prev_permutation(mask,mask+n));
 
        cout<<st.size()<<endl;
    }
 
    return 0;
}