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 <map>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
map<string,bool> bmap;
short card[10];
short select[4],temp[4];
bool used[10];
int n,k,num;
void solve(int m) {
	if(m==k) {
		for(int i=0;i<k;i++)temp[i]=select[i];
		do {
			stringstream str;
			for(int i=0;i<k;i++)str<<(int)select[i];
			if(bmap.find(str.str())==bmap.end())num++;
			bmap[str.str()]=true;
		}while(next_permutation(select,select+k));
		for(int i=0;i<k;i++)select[i]=temp[i];
	}else {
		for(int i=0;i<n;i++) {
			if(used[i]==false) {
				used[i]=true;
				select[m]=card[i];
				solve(m+1); 
				used[i]=false;
			}
		}
	}
}
int main() {
	while(scanf("%d %d",&n,&k),n) {
		for(int i=0;i<n;i++)scanf("%d",&card[i]);
		fill(used,used+10,0);
		bmap.clear();
		num=0;
		solve(0);
		printf("%d\n",num);
	}
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int n,k;
	cin >> n >> k;
	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[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[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<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 <set>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

typedef long long ll;

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
	while (cin >> n >> m, n) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) cin >> a[i];
		vector<set<ll> > dp(1 << n);
		dp[0] = set<ll>({ 0 });
		for (int i = 1; i < 1 << n; i++) {
			for (int j = 0; j < n; j++) {
				if (i & (1 << j)) {
					for (int k : dp[i - (1 << j)]) {
						dp[i].insert(stoll(to_string(k) + to_string(a[j])));
					}
				}
			}
		}
		set<ll> ret;
		for (int i = 0; i < 1 << n; i++) {
			if (__builtin_popcount(i) == m) {
				ret.insert(dp[i].begin(), dp[i].end());
			}
		}
		cout << ret.size() << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string.h>
#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;
			}
		}
	}
}