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 n, k, ans = 0, roop, save3[10], roop2, card[10], res, save2;
bool save[100000000];
void solve(){
	roop--;
	for (int i = 0; i<n; i++){
		if(save3[i]==1) continue;
			if (roop == 0){
				save2 = res;
				if(card[i]/10>=1){
					res = res*100;
				}
				else{
					res = res*10;
				}
				res += card[i];
				if (save[res] == 0){
					ans++;
					save[res]=1;
				}
				res = save2;
			}
			else{
				if(card[i]/10>=1){
					res = res*100;
				}
				else{
					res = res*10;
				}
				res += card[i];
				save3[i]=1;
				solve();
				save3[i]=0;
				roop++;
				res -= card[i];
				if(res%100==0)
					res=res/100;
				else if(res%10==0)
					res=res/10;
			}
		}
}
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] = 1;
			solve();
			save3[i]=0;
		}
		cout << ans << endl;
		ans=0;
		for (int i = 0; i < 10000000; i++){
			save[i] = 0;
		}
	}
}

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

int n, k;
std::string cards[11];
std::set<std::string>s;

void makecard(int p, std::string res, bool used[11]){
	if (p == k){
		s.insert(res);
		return;
	}

	for (int i = 0; i < n; i++){
		if (!used[i]){
			used[i] = true;
			makecard(p + 1, res + cards[i], used);
			used[i] = false;
		}
	}
}

int main(){

	while (std::cin >> n >> k, n || k){
		s.clear();
		for (int i = 0; i < n; i++){
			std::cin >> cards[i];
		}
		bool used[11];
		for (int j = 0; j < 11; j++)used[j] = false;

		makecard(0, "", used);

		std::cout << s.size() << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>

using namespace std;

vector<string> nums;
set<string> a;
int n, k;
int main()
{
    while(cin >> n && n)
    {
        cin >> k;
        for(int i=0;i<n;++i)
        {
            string t;
            cin >> t;
            nums.push_back(t);
        }
        for(int w=0;w<n;++w){
            for(int x=0;x<n;++x){
                if(w==x) continue;
                if(k>2){
                    for(int y=0;y<n;++y){
                        if(y==x || y==w) continue;
                        if(k>3){
                            for(int z=0;z<n;++z){
                            if(z==y || z==x || z==w) continue;
                            a.insert(nums[w]+nums[x]+nums[y]+nums[z]);
                            }
                        }
                        else a.insert(nums[w]+nums[x]+nums[y]);
                    }
                }
                else a.insert(nums[w]+nums[x]);
            }
        }
        cout << a.size() << endl;
        nums.clear();
        a.clear();
    }
}

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int n, k, a[10];
set< string > num_set;

void calc(vector< int >& vec) {
	do {
		stringstream ss;
		for_(i,0,k) ss << vec[i];
		num_set.insert(ss.str());
	} while (next_permutation(vec.begin(), vec.end()));
}

void dfs(int i, vector< int >& vec, int K) {
	if (i == n) {
		if (K == 0) calc(vec);
		return;
	}
	
	dfs(i + 1, vec, K);
	
	vec.push_back(a[i]);
	dfs(i + 1, vec, K - 1);
	vec.pop_back();
}

int main() {
	while (cin >> n >> k, n) {
		for_(i,0,n) cin >> a[i];
	
		sort(a, a + n);
		
		vector< int > vec;
		
		num_set.clear();
		dfs(0, vec, k);
		cout << num_set.size() << endl;
	}
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<sstream>
using namespace std;
bool memo[1000000];
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[128];
        queue<P> Q;
        memset(memo,0,sizeof(memo));
        for(int i = 0; i < n; i++){
            bool x[10]={0};
            x[i] = 1;
            scanf("%d",&card[i]);
            Q.push(P(card[i],1,x));
        }
        int ans = 0;
        while( Q.size() ){
            P p = Q.front();Q.pop();
            if(p.cnt == m){
                if( memo[p.num] )continue;
                ans++;
                memo[p.num] = true;
            }
            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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

int main(){
  int n, k;
  while(cin >> n >> k && (n|k)){
    string card[10];
    string t;
    for(int i = 0 ; i < n ; i++)  cin >> card[i];
    
    sort(card, card+n);

    set<int> res;
    
    do{
      int a = 0;
      for(int i = 0 ; i < k ; i++){
        for(int j=0;j<card[i].size();j++){
          a *= 10;
          a += card[i][j] - '0';
        }
      }
      
      res.insert(a);
    }while(next_permutation(card, card+n));
      
    cout << res.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){
    if(depth==k){
        int num;
        num=atoi(sum.data());
        s[t]=num;
        t++;
        return;
    }
    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;
    }
}