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 <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>

#define REP(i,k,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int func(vector<int> v, int k) {
	stringstream ss;
	rep(i, k) {
		ss << v[i];
	}

	int ret;
	ss >> ret;

	return ret;
}

int main() {
	int n;
	while(cin >> n) {
		if(n == 0) break;

		int k;
		cin >> k;
		
		vector<int> v(n);
		rep(i, n) cin >> v[i];

		sort(v.begin(), v.end());

		set<int> ans;
		set<vector<int> > st;


		do {
			vector<int> ret(k);
			rep(i, k)  ret[i] = v[i];

			if(st.find(ret) == st.end()) {
				st.insert(ret);
				ans.insert(func(v, k));
			} else continue;
		}while(next_permutation(v.begin(), v.end()));

		cout << ans.size() << endl;
	}
	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[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<bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(),(v).end()

using namespace std;

using ll=long long;
using pii=pair<int, int>;
using vi=vector<int>;
using vii=vector<vector<int>>;

const ll LINF=1LL<<60;
const int INF=1<<29;
const int MOD=1e9+7;

vector<string> num, ord;
int n, k;
set<string> st;
vector<bool> used;

void rec(int pos=0){
    if(pos == k){
        string s="";
        for(int i=0; i<k; i++) s=s+ord[i];
        st.insert(s);
        return;
    }
    for(int i=0; i<n; i++){
        if(used[i]) continue;
        used[i]=true;
        ord[pos]=num[i];
        rec(pos+1);
        used[i]=false;
    }
    return;
}

int main(){
    while(cin >> n >> k){
        if(n == 0 && k == 0) break;
        st.clear();
        num.resize(n), ord.resize(k);
        used.assign(n, 0);
        for(auto &x : num) cin >> x;
        rec();
        cout << st.size() << endl;
    }
    return 0;
}