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>
#define INF 100000
int min(int a,int b){return a<b?a:b;}
int getk(int a){
	int res=0;
	while(a>0){
		res++;
		a/=10;
	}
	return res;
}
int pow(int a,int b){
	int l;
	int rt=1;
	for(l=0;l<b;l++)rt*=10;
	return rt;
}
int card[10];
int pos[10000];
int cpos;
int main(){
	int i,j;
	int n,k;
	while(1){
	scanf("%d",&n);
	scanf("%d",&k);
	if(n==0&&k==0)return 0;
	for(i=0;i<n;i++)scanf("%d",&card[i]);
	cpos=0;
	int max=1;
	int ket;
	int next;
	int ni;
	int num;
	int ans=0;
	bool used[10];
	for(i=0;i<k;i++)max*=n;
	for(i=0;i<max;i++){
cont:		
		for(j=0;j<n;j++)used[j]=false;
		ket=1;
		ni=i;
		num=0;
		for(j=0;j<k;j++){
			next=ni%n;
			ni/=n;
			if(used[next]){
				i++;
				if(i==max)goto end;
				goto cont;
			}
			else used[next]=true;
			num+=card[next]*ket;
			ket*=pow(10,getk(card[next]));
		}
		for(j=0;j<cpos;j++){
			if(num==pos[j])break;
		}
		if(j==cpos){
			pos[cpos++]=num;
			ans++;
		}
	}
end:
	printf("%d\n",ans);
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
map<string,bool> bmap;
int card[10];
int selected[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]=selected[i];
		do {
			stringstream str;
			for(int i=0;i<k;i++)str<<selected[i];
			if(bmap.find(str.str())==bmap.end())num++;
			bmap[str.str()]=true;
		}while(next_permutation(selected,selected+k));
		for(int i=0;i<k;i++)selected[i]=temp[i];
	}else {
		for(int i=0;i<n;i++) {
			if(used[i]==false) {
				used[i]=true;
				selected[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 <vector>
#include <set>
#include <string>

using namespace std;

void solve()
{
	int n;
	cin >> n;
	int sheet;
	cin >> sheet;
	set<string> Set;
	vector<string> Vec(n);
	for(int i = 0; i < n; ++i)
	{
		cin >> Vec[i];
	}
	if(sheet == 2)
	{
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				if(i == j)
				{
					continue;
				}
				Set.insert(Vec[i] + Vec[j]);
			}
		}
	}
	if(sheet == 3)
	{
		for(int i = 0; i < n; ++i)
		{
			for(int j = 0; j < n; ++j)
			{
				for(int k = 0; k < n; ++k)
				{
					if(i == j || j == k || i == k)
					{
						continue;
					}
					Set.insert(Vec[i] + Vec[j] + Vec[k]);
				}
			}
		}
	}
	if(sheet == 4)
	{
		for(int i = 0; i < n; ++i)
		{
			for(int j = i + 1; j < n; ++j)
			{
				for(int k = j + 1; k < n; ++k)
				{
					for(int l = k + 1; l < n; ++l)
					{
						if(i == j || i == k || i == l || j == k || j == l || k == l)
						{
							continue;
						}
						Set.insert(Vec[i] + Vec[j] + Vec[k] + Vec[l]);
					}
				}
			}
		}
	}
	cout << Set.size() << endl;
}

int main()
{
	solve();
	return(0);
}

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

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

ll n,k;
ll a[20];
bool used[20];

ll ans;
map<ll,bool> mp;

void dfs(ll now, ll num){
	if(now == k){
		if(!mp[num]){
			mp[num] = true;
			ans++;
			//cout << num << "num" << endl;
		}
	}
	for (int i = 0;i < n;i++){
		if(!used[i]){
			ll nn;
			if(a[i] >= 10) nn = num * 100;
			else nn = num * 10;
			nn += a[i];
			used[i] = true;
			dfs(now+1,nn);
			used[i] = false;
		}
	}
	return;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(true){
		cin >> n;
		if(n == 0) break;
		cin >> k;

		for (int i = 0;i < n;i++){
			cin >> a[i];
		}

		memset(used,0,sizeof(used));
		mp = map<ll,bool>();
		ans = 0;
		
		dfs(0,0);

		cout << ans << endl;

	}

	return 0;

}

C++: #include <cstdio>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
using namespace std;

int main(){
	while(1){
		int n, k;
		int p[10];
		set<long> s;
		
		scanf(" %d", &n);
		if(n==0) break;
		scanf(" %d", &k);	
		for(int i=0; i<n; ++i) scanf(" %d", &p[i]);
		
		if(k==2){
			for(int a=0; a<n; ++a){
				for(int b=0; b<n; ++b){
					if(b==a) continue;
					stringstream ss;
					ss<<p[a];
					ss<<p[b];
					
					istringstream iss(ss.str());
					long t;
					iss>>t;
					s.insert(t);
					iss.clear();
					printf(" %ld\n", t);
				}
			}	
		}
		else if(k==3){
			for(int a=0; a<n; ++a){
				for(int b=0; b<n; ++b){
					if(b==a) continue;
					for(int c=0; c<n; ++c){
						if(c==a || c==b) continue;
						stringstream ss;
						ss<<p[a];
						ss<<p[b];
						ss<<p[c];
						
						istringstream iss(ss.str());
						long t;
						iss>>t;
						s.insert(t);
						iss.clear();	
					}
				}
			}
		}
		else if(k==4){
			for(int a=0; a<n; ++a){
				for(int b=0; b<n; ++b){
					if(b==a) continue;
					for(int c=0; c<n; ++c){
						if(c==a || c==b) continue;
						for(int d=0; d<n; ++d){
							if(d==a || d==b || d==c) continue;
							stringstream ss;
							ss<<p[a];
							ss<<p[b];
							ss<<p[c];
							ss<<p[d];
							
							istringstream iss(ss.str());
							long t;
							iss>>t;
							s.insert(t);
							iss.clear();	
						}
					}
				}
			}
		}
		
		printf("%ld\n", s.size());
	}
}

C++: #include <cstdio>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
using namespace std;

int main(){
	while(1){
		int n, k;
		int p[10];
		set<long> s;
		
		scanf(" %d", &n);
		if(n==0) break;
		scanf(" %d", &k);	
		for(int i=0; i<n; ++i) scanf(" %d", &p[i]);
		
		if(k==2){
			for(int a=0; a<n; ++a){
				for(int b=0; b<n; ++b){
					if(b==a) continue;
					stringstream ss;
					ss<<p[a];
					ss<<p[b];
					
					istringstream iss(ss.str());
					long t;
					iss>>t;
					s.insert(t);
					iss.clear();
				}
			}	
		}
		else if(k==3){
			for(int a=0; a<n; ++a){
				for(int b=0; b<n; ++b){
					if(b==a) continue;
					for(int c=0; c<n; ++c){
						if(c==a || c==b) continue;
						stringstream ss;
						ss<<p[a];
						ss<<p[b];
						ss<<p[c];
						
						istringstream iss(ss.str());
						long t;
						iss>>t;
						s.insert(t);
						iss.clear();	
					}
				}
			}
		}
		else if(k==4){
			for(int a=0; a<n; ++a){
				for(int b=0; b<n; ++b){
					if(b==a) continue;
					for(int c=0; c<n; ++c){
						if(c==a || c==b) continue;
						for(int d=0; d<n; ++d){
							if(d==a || d==b || d==c) continue;
							stringstream ss;
							ss<<p[a];
							ss<<p[b];
							ss<<p[c];
							ss<<p[d];
							
							istringstream iss(ss.str());
							long t;
							iss>>t;
							s.insert(t);
							iss.clear();	
						}
					}
				}
			}
		}
		
		printf("%ld\n", s.size());
	}
}