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;

#define int long long
#define LL long long

int n, k, c[12];
bool mem[100000000] = {false};

inline int LL_pow(int m, int n) {
    int ans = 1;
    for (int i = 0; i < n; ++i) {
        ans *= m;
    }
    return ans;
}

int cal(int depth, int num, vector<int> used) {
    if (depth == 0) {
        if (mem[num]) return 0;
        mem[num] = true;
        cout << "::" << num << endl;
        return 1;
    }

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        bool isused = false;
        for (int j = 0; j < used.size(); ++j) {
            if (used[j] == i) {
                isused = true;
                break;
            }
        }
        if (isused) continue;

        vector<int> temp;
        temp.assign(used.begin(), used.end());
        temp.push_back(i);

        ans += cal(depth - 1, num * LL_pow(10, (int)log10(c[i]) + 1) + c[i], temp);
    }

    return ans;
}

signed main() {
    while (cin >> n >> k, n) {
        for (int i = 0; i < n; ++i) {
            cin >> c[i];
        }
        sort(c, c + n);
        fill(mem, mem + 100000000, false);

        cout << cal(k, 0, vector<int>()) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    int n, k;
    while(cin >> n >> k) {
        if (n == 0) break;
        vector<int> card(n);
        rep(i, n) {
            cin >> card[i];
        }
        
        sort(card.begin(), card.end());
        set<int> ans;
        int sum;
        do {
            sum = 0;
            rep(i, k) {
                if (card[i] < 10) {
                    sum = 10 * sum + card[i];
                } else {
                    sum = 100 * sum + card[i];
                }
            }
            ans.insert(sum);
        } while (next_permutation(card.begin(), card.end()));

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

C++: #include<iostream>
#include<string>
#include<set>
#include<math.h>
using namespace std;

int digit_table[10],number[10];
set<int> set2,set3,set4;

void digit(int n,int s){
  int dgt=1;
  while(s/10!=0){
    s/=10;
    dgt+=1;
  }
  digit_table[n]=dgt;
}

int main(){
  int n,k,tmp,a,b;
  cin>>n>>k;

  while(n!=0&&k!=0){
  set<int> set2,set3,set4;
  for(int i=0;i<n;i++){
    cin>>tmp;
    number[i]=tmp;
    digit(i,tmp);
  //  cout<<"tmp="<<tmp<<" digit="<<digit_table[i]<<endl;
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        if(i!=j){
      //    cout<<number[i]*pow(10,digit_table[j])+number[j]<<endl;
          set2.insert(number[i]*pow(10,digit_table[j])+number[j]);
        }
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      for(int l=0;l<n;l++){
        if(i!=j&&j!=l&&l!=i){
          set3.insert(number[i]*pow(10,digit_table[j]+digit_table[l])+number[j]*pow(10,digit_table[l])+number[l]);
        }
      }
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      for(int l=0;l<n;l++){
        for(int m=0;m<n;m++){
          if(i!=j && i!=l && i!=m && j!=l && j!=m && l!=m){
            set4.insert(number[i]*pow(10,digit_table[j]+digit_table[l]+digit_table[m])+number[j]*pow(10,digit_table[l]+digit_table[m])+number[l]*pow(10,digit_table[m])+number[m]);
          }
        }
      }
    }
  }
//cout<<"answer=";
  if(k==2) cout<<set2.size()<<endl;
  if(k==3) cout<<set3.size()<<endl;
  if(k==4) cout<<set4.size()<<endl;
  cin>>n>>k;
}
  return 0;
}

C++: #include <stdio.h>
#include <limits.h>
#include <math.h>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem0546 : Lining up the cards **/

vector<string> card;
set<string> ans;
int N, K;


int main()
{
	while (cin>>N, N) {
		cin>>K;
		rep(i, N) {
			string s; cin>>s;
			card.push_back(s);
		}
		sort(card.begin(), card.end());
        set<string> ans;
        do {
            string s;
            rep(i, K) {
                s += card[i];
            }
            //cerr << s << endl;
            ans.insert(s);
        } while (next_permutation(card.begin(), card.end()));
		
		//set<string>::iterator it = ans.begin();
		//for (; it!=ans.end(); it++) cerr<<*it<<endl;
		cout << ans.size() << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef vector<int> comb;

int s_to_i(string s){
	bool flag = false;
	int n = 0;

	for(unsigned int i=0 ; i < s.size() ; ++i ){
		if( i == 0 && s[i] == '-' ){
			flag = true;
		}else if( s[i] >= '0' && s[i] <= '9' ){
			n *= 10;
			n += s[i] - '0';
		}else{
			break;
		}
	}
	return (flag)? -n : n ;
}

string i_to_s(int n){
	bool flag = false;
	string s;
	if( n < 0 ){
		flag = true;
		n *= -1;
	}
	do{
		s.push_back( (n%10)+'0' );
		n /= 10;
	}while( n != 0 );

	reverse( s.begin() , s.end() );
	if( flag ) s.insert( s.begin() , 1 , '-' );

	return s;
}

inline int first(int n){
	return ((1 << n) - 1);
}

inline int nextSet(int x){
	int small, ripple, newSmall,ones;
	
	small = x & -x;
	ripple = x + small;
	newSmall = ripple & -ripple;
	ones = (( newSmall / small ) >> 1) - 1;
	
	return ( ripple | ones );
}

comb setComb(int s, int n, comb a){
	comb c;
	for(int i=0 ; i<n ; i++){
		if( s & 1 ) c.push_back( a[i] );
		s >>= 1;
	}
	return c;
}

int main(){
	int n, k, x, s;
	comb c;
	
	while( cin >> n >> k , n || k ){
		vector<int> a;
		set<int> ans;
		
		for(int i=0 ; i<n ; i++ ){
			cin >> s;
			a.push_back( s );
		}
		
		x = first(k);
		while( !(x & ~first(n) ) ){
			c = setComb( x , n , a );
			
			do{
				string s = "";
				for(int i=0 ; i < (int)c.size() ; i++ ){
					s += i_to_s( c[i] );
				}
				int num = s_to_i(s);
				
				//if( !count(ans.begin(),ans.end(),num) )
					//ans.push_back( num );
				ans.insert( num );
    		}while( next_permutation( c.begin() , c.end() ) );
			
			x = nextSet( x );
		}
		cout << "ans : ";
		cout << ans.size() << endl;
	}
}