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 "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

//population count
//?????£?????????bit?????°?????°??????
int popcount(int x) {
	int ret = 0;
	//?????????????????£?????????bit????????????
	while (x) {
		x &= x - 1;
		ret++;
	}
	return ret;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	for (int N; cin >> N&&N;) {
		int K; cin >> K;
		unordered_set<string> st;
		vector<int> v(N); rep(i, 0, N) { cin >> v[i]; }
		for (int mask = 1; mask < (1 << N); mask++) {
			if (popcount(mask) != K)continue;
			vector<int> base;
			rep(i, 0, N) {
				if ((mask >> i) & 1) {
					base.emplace_back(v[i]);
				}
			}
			sort(all(base));
			do {
				string tmp = "";
				rep(i, 0, K) {
					tmp += to_string(base[i]);
				}
				st.insert(tmp);
			} while (next_permutation(all(base)));
		}
		cout << st.size() << endl;
	}
	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)
{
//	cout << "loop:" << loop << endl;
	if(loop==m)
	{
//		cout << "came:" ;
//		for(int i=0;i<came.size();i++)
//			cout <<  came[i] << " " ;
//		cout << endl;
//		cout << sans << endl;;
		ans.insert(sans);
		return ;
	}
	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<came.size();j++)
		{
//				cout << "j:" << j << " now:" << came[j] << " size:" << came.size() << " " << i << endl;
			if(i==came[j])
			{
				i++;
				j=-1;
			}
		}
		if(i==n)
			break;
//		cout << "next:" << i << endl;
		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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const double EPS = 1e-12;
const double INF = 1e12;

int N,M;
int ar[11];
set<string>ch;

void re(string str,vector<bool>used,int cnt){
	if(cnt==0){
		ch.insert(str);
	}else{
		REP(i,SZ(used)){
			if(!used[i]){
				used[i]=true;
				re(str+toString(ar[i]),used,cnt-1);
				used[i]=false;
			}
		}
	}
}
int main(){
	
	while(cin>>N>>M){
		if(N==0&&M==0)break;
		ch.clear();
		REP(i,N)cin>>ar[i];
		vector<bool>used(N,false);
		re("",used,M);
		cout<<SZ(ch)<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
set<unsigned int> s;
int n,m;
int cards[100];
bool used[100];
void solve(int cnt,string a){
  string tmp=a;
  if(cnt==m){
    s.insert(atoi(a.c_str()));
    return;
  }
  for(int i=0;i<n;i++){
    if(used[i]==true)continue;
    used[i]=true;
    a+=to_string(cards[i]);
    solve(cnt+1,a);
    a=tmp;
    used[i]=false;
  }
}

int main(){
  while(cin>>n,n){
    s.clear();
    cin>>m;
    for(int i=0;i<n;i++){
      used[i]=false;
      cin>>cards[i];
    }
    solve(0,"");
    // for(auto i:s)
    // cout << i << endl;

    cout << s.size() << endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 11
#define SIZE 400

int N, K;
char A[MAXN][10];
int used[MAXN];
char num[100];
int size;
int arr[SIZE];

void rec(int k) {
  int i;
  int length = strlen(num);
  if(k == K) {
    arr[size++] = atoi(num);
    return;
  }
  for(i = 0; i < N; ++i) {
    if(!used[i]) {
      used[i] = 1;
      strcat(num, A[i]);
      rec(k+1);
      used[i] = 0;
      num[length] = '\0';
    }
  }
}

void bubbleSort() {
  int i, j, t;
  for(i = 0; i < size; ++i) {
    for(j = 1; j < size-i; ++j) {
      if(arr[j-1] > arr[j]) {
	t = arr[j-1];
	arr[j-1] = arr[j];
	arr[j] = t;
      }
    }
  }
}

int main() {
  int i, ans;
  while(1) {
    scanf("%d", &N);
    scanf("%d", &K);
    if(N == 0 && K == 0) break;
    for(i = 0; i < N; ++i) {
      scanf("%s", A[i]);
    }

    for(i = 0; i < N; ++i) {
      used[i] = 0;
    }
    strcpy(num, "");
    size = 0;
    rec(0);

    bubbleSort();

    ans = 0;
    for(i = 0; i < size; ++i) {
      if(i == 0 || arr[i-1] != arr[i]) {
	++ans;
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}