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"

int N;
int K;
int num[10];
int fig[5040] = {};
int box;
int work;
int now;
int ans;

int main() {
	scanf("%d %d", &N, &K);
	while (N != 0) {
		ans = 0;
		for (int i = 0; i < N; i++) {
			scanf("%d", &num[i]);
		}
		switch (K) {
		case 2:
			box = 0;
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					if (i != j) {
						fig[box] = num[i];
						if (num[j] >= 10)fig[box] = fig[box++] * 100 + num[j];
						else fig[box] = fig[box++]*10 + num[j];
					}
				}
			}
			break;
		case 3:
			box = 0;
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					for (int k = 0; k < N; k++) {
						if (i != j&&i != k&&j != k) {
							fig[box] = num[i];
							if (num[j] >= 10)fig[box] = fig[box] * 100 + num[j];
							else fig[box] = fig[box] * 10 + num[j];
							if (num[k] >= 10)fig[box] = fig[box++] * 100 + num[k];
							else fig[box] = fig[box++] * 10 + num[k];
						}
					}
				}
			}
			break;
		case 4:
			box = 0;
			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&&i != k&&i != l&&j != k&&j != l&&k != l) {
								fig[box] = num[i];
								if (num[j] >= 10)fig[box] = fig[box] * 100 + num[j];
								else fig[box] = fig[box] * 10 + num[j];
								if (num[k] >= 10)fig[box] = fig[box] * 100 + num[k];
								else fig[box] = fig[box] * 10 + num[k];
								if (num[l] >= 10)fig[box] = fig[box++] * 100 + num[l];
								else fig[box] = fig[box++] * 10 + num[l];
							}
						}
					}
				}
			}
			break;
		}
		for (int i = 0; i < box-1; i++) {
			for (int j = i + 1; j < box; j++) {
				if (fig[i] > fig[j]) {
					work = fig[i];
					fig[i] = fig[j];
					fig[j] = work;
				}
			}
		}
		now = fig[0];
		ans++;
		for (int i = 1; i < box; i++) {
			if (fig[i] != now) {
				ans++;
				now = fig[i];
			}
		}
		printf("%d\n", ans);
		scanf("%d %d", &N, &K);
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	for(;;){
	int n,k;
	cin >> n >> k;
	if(n==0&&k==0)return 0;
	string ss[10];
	vector<string> ans;
	for(int i=0;i<n;i++)cin >> ss[i];
	int a=0,b=1,c=2,d=3;

	
	if(k==2){
		for(;;){
			string x="";
			x+=ss[a]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[a];
			ans.push_back(x);
			b++;
			if(b==n){
				if(a==n-2)goto next;
				a++;
				b=a+1;
			}
		}
	}
	else if(k==3){
		for(;;){
			string x="";
			x+=ss[a]+ss[b]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[a]+ss[c]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[a]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[c]+ss[a];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[a]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[b]+ss[a];
			ans.push_back(x);
			x="";
			c++;
			if(c==n){
				if(b==n-2){
					if(a==n-3){
						goto next;
					}
					a++;
					b=a+1;
					c=a+2;
					goto f;
				}
				b++;
				c=b+1;
			}
			f:;
		}
	}
	else {
		for(;;){
			string x="";
			x+=ss[a]+ss[b]+ss[c]+ss[d];
			ans.push_back(x);
			x="";
			x+=ss[a]+ss[b]+ss[d]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[a]+ss[c]+ss[b]+ss[d];
			ans.push_back(x);
			x="";
			x+=ss[a]+ss[c]+ss[d]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[a]+ss[d]+ss[b]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[a]+ss[d]+ss[c]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[a]+ss[c]+ss[d];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[a]+ss[d]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[c]+ss[d]+ss[a];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[c]+ss[a]+ss[d];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[d]+ss[a]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[b]+ss[d]+ss[c]+ss[a];
			ans.push_back(x);
			x="";
			
			x+=ss[c]+ss[a]+ss[b]+ss[d];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[a]+ss[d]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[b]+ss[a]+ss[d];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[b]+ss[d]+ss[a];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[d]+ss[a]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[c]+ss[d]+ss[b]+ss[a];
			ans.push_back(x);
			x="";
			x+=ss[d]+ss[a]+ss[b]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[d]+ss[a]+ss[c]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[d]+ss[b]+ss[a]+ss[c];
			ans.push_back(x);
			x="";
			x+=ss[d]+ss[b]+ss[c]+ss[a];
			ans.push_back(x);
			x="";
			x+=ss[d]+ss[c]+ss[a]+ss[b];
			ans.push_back(x);
			x="";
			x+=ss[d]+ss[c]+ss[b]+ss[a];
			ans.push_back(x);
			x="";
			d++;
			if(d==n&&c==n-2&&b==n-3&&a==n-4)goto next;
			else if(d==n&&c==n-2&&b==n-3){
				a++;
				b=a+1;
				c=a+2;
				d=a+3;
			}
			else if(d==n&&c==n-2){
				b++;
				c=b+1;
				d=c+1;
			}
			else if(d==n){
				c++;
				d=c+1;
			}
			else ;
		}
	}
next:;
	sort(ans.begin(),ans.end());
	/*for(int i=0;i<ans.size();i++){
		cout << ans[i] << "\n";
	}*/
	int ret=0;
	for(int i=0;i<ans.size();i++){
		ret++;
		if(i==ans.size()-1)goto xx;
		while(ans[i]==ans[i+1]){
			i++;
			if(i==ans.size()-1)goto xx;
		}
	}
	xx:;
	printf("%d\n",ret);
	}
	return 0;
}

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

using namespace std;

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

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

#define rep(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define rept(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )

int N, K;
std::string num[11];
std::set<std::string> data;

void f( int used, int cnt, std::string d ){
	if( cnt == 0 ){
		data.insert(d);
		return;
	}

	rep( i, N ){
		if( used & (1 << i) ) continue;

		f( used | 1 << i, cnt - 1, d + num[i] );
	}
}

int main(){
	while( scanf("%d%d", &N, &K), N * K ){
		data.clear();

		rep( i, N ) std::cin >> num[i];

		f( 0, K, "" );
		
		std::cout << data.size() << std::endl;
	}
}