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 <algorithm>
#include <functional>

int main() {
  for(int tci = 0; ; ++tci) {
    int n, k; scanf("%d%d", &n, &k);
    if(n == 0) break;
    int cards[10];
    for(int i = 0; i < n; ++i) {
      scanf("%d", &cards[i]);
    }
    bool selected[10];
    std::fill_n(selected, n, false);
    int nums[10000], nums_size = 0;
    std::function<void(int, int)> search = [&](int depth, int num) {
      if(depth <= 0) {
        nums[nums_size++] = num;
      } else {
        for(int i = 0; i < n; ++i) {
          if(selected[i]) continue;
          selected[i] = true;
          search(depth - 1, num * (cards[i] < 10 ? 10 : 100) + cards[i]);
          selected[i] = false;
        }
      }
    };
    search(k, 0);
    std::sort(nums, nums + nums_size);
    nums_size = std::unique(nums, nums + nums_size) - nums;
    printf("%d\n", nums_size);
  }
  return 0;
}

C++: #include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int n,k,card[10],digit[10];
bool use[10];
set<ll> S;

void rec(ll num,int depth)
{
	if(depth==0)
	{
		S.insert(num); return;
	}
	rep(i,n)if(!use[i])
	{
		use[i]=1;
		rec(num*digit[i]+card[i],depth-1);
		use[i]=0;
	}
}

int main()
{
	while(cin>>n>>k,n)
	{
		rep(i,n)
		{
			cin>>card[i];
			digit[i]=card[i]>9?100:10;
		}
		fill_n(use,n,0); S.clear();
		rec(0,k);
		cout<<S.size()<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
//#include <fstream>	// require freopen

using namespace std;

string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.lining_up_card", "r", stdin );
	int n, k;

	while (cin >> n >> k && n && k){
		vector <string> card (n);
		for (int i = 0; i < n; ++i){
			int m;
			cin >> m;
			card[i] = itos (m );
		} // end for
		sort (card.begin(), card.end() );

		set <string> res;
		do{
			vector <int> perm (k, 0 );
			string sum = "";
			for (int i = 0; i < k; ++i){
				sum += card[i];
			} // end for
			res.insert (sum );
		}while ( next_permutation (card.begin(), card.end() ) );

		cout << res.size() << endl;
		
	} // end loop
		
	return 0;
}

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

using namespace std;

int n,m;
vector<int> came;
string str[11];
set<string> ans;

void serch(int loop,string sans)
{
	if(loop==m)
	{
		ans.insert(sans);
		return ;
	}
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<came.size();j++)
		{
			if(i==came[j])
			{
				i++;
				j=-1;
			}
		}
		if(i==n)
			break;
		came.push_back(i);
		serch(loop+1,sans+str[i]);
		came.pop_back();
	}
	return ;
}

int main()
{
	int i,j;
	for(;;)
	{
		cin >> n >> m ;
		if(n==0 && m==0)
			break;
		
		ans.clear();
		
		for(i=0;i<n;i++)
			cin >> str[i] ;
		
		serch(0,"");
		
		cout << ans.size() << endl;
	}
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
# define ALL(x)     (x).begin(),(x).end()
# define UNIQ(c)    (c).erase(unique(ALL((c))), end((c)))
# define mp         make_pair
# define eb         emplace_back
# define FOR(i,a,b) for(int i=(a);i<(b);++i)
# define REP(i,n)   FOR(i,0,n)
# define INIT       std::ios::sync_with_stdio(false);std::cin.tie(0);


bool ans[10000001];
int a[11];
int n, k;

void calc(int i, int j, int qqq, int l) {
	int num = a[i];
	if (a[j] >= 10)num *= 100;
	else num *= 10;
	num += a[j];
	if (3 <= k) {
		if (a[qqq] >= 10)num *= 100;
		else num *= 10;
		num += a[qqq];
		if (4 <= k) {
			if (a[l] >= 10)num *= 100;
			else num *= 10;
			num += a[l];
		}
	}
	//cout << num << endl;
	//system("pause");
	ans[num] = true;
}

int main() {
	while (cin >> n >> k&&n&&k) {
		for (int i = 0; i < 10000001; i++)ans[i] = false;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				for (int qqq = 0; qqq < n; qqq++) {
					for (int l = 0; l < n; l++) {
						if (i == j)continue;
						if (i == qqq)continue;
						if (i == l)continue;
						if (j == qqq)continue;
						if (j == l)continue;
						if (qqq == l)continue;
						calc(i, j, qqq, l);
					}
				}
			}
		}
		int s = 0;
		for (int i = 0; i < 10000000; i++) {
			if (ans[i])s++;
		}
		cout << s << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <fstream>	// require freopen

using namespace std;

string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit 
//	freopen ("testcase.lining_up_card", "r", stdin );
	int n, k;

	while (cin >> n >> k && n && k ) {
		vector <string> card (n );
		for (int i = 0; i < n; ++i ) {
			int m;
			cin >> m;
			card[i] = itos (m );
		} // end for
		sort (card.begin(), card.end() );
		set <string> res;

		do{
			string sum = "";
			for (int i = 0; i < k; ++i ){ 
				sum += card[i];
			} // end for
			res.insert (sum );
		}while ( next_permutation (card.begin(), card.end() ) );

		cout << res.size() << endl;
		
	} // end loop
		
	return 0;
}