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++: //
//  main.cpp

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n,k,cards[11];
bool selected[11];
vector<vector<int>> selectlist;
vector<int> makelist;

void reset(){
	n = k = 0;
	selectlist.clear();
	makelist.clear();
	for(int i=0;i<11;i++){
		cards[i] = 0;
		selected[i] = false;
	}
}

void select_dfs_(int depth,int top,vector<int> vec){
	if(depth == k){
		selectlist.push_back(vec);
		return;
	}
	for(int i=top+1;i<n;i++){
		vec.push_back(cards[i]);
		select_dfs_(depth+1,i,vec);
		vec.pop_back();
	}
}

void makeint_dfs_(int depth,int makedint,vector<int> source){
	if(depth == k){
		makelist.push_back(makedint);
		return;
	}
	for(int i=0;i<k;i++){
		if(!selected[i]){
			selected[i] = true;
			int maked;
			if(source[i]/10 > 0){
				maked = makedint*100+source[i];
			}else{
				maked = makedint*10+source[i];
			}
			makeint_dfs_(depth+1,maked,source);
			selected[i] = false;
		}
	}
}

int main(){
	while(1){
		reset();
		
		cin >> n >> k;
		if(n == 0 && k == 0)break;
		for(int i=0;i<n;i++){
			cin >> cards[i];
		}
		
		for(int i=0;i<n-k;i++){
			vector<int> vec;
			vec.push_back(cards[i]);
			select_dfs_(1,i,vec);
		}
		
		for(int i=0;i<selectlist.size();i++){
			for(int j=0;j<selectlist[i].size();j++){
				selected[j] = true;
				makeint_dfs_(1,selectlist[i][j],selectlist[i]);
				selected[j] = false;
			}
		}
		
		sort(makelist.begin(),makelist.end());
		
		int answer=0,memo=-1;
		for(int i=0;i<makelist.size();i++){
			if(memo != makelist[i]){
				answer ++;
				memo = makelist[i];
			}
		}
		
		cout << answer << endl;
		
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int main(){
  int N,K;
  while( cin >> N >> K){
    if( N == 0 && K == 0){ break;}
    vector<string> P(N);
    for(int i = 0; i < N; i++){ int c; cin >> c; P[i] = to_string(c);}
  
   
    set<string> s;  
    
    if( K == 2){
      for(int i = 0; i < N-1; i++){
        for(int j = i+1; j < N; j++){
          vector<string> vec;
          vec.push_back(P[i]); vec.push_back(P[j]);
          sort(vec.begin(),vec.end());
        do{
          s.insert(vec[0]+vec[1]);
        }while(next_permutation(vec.begin(),vec.end()));
        
        }}}
    
    if( K == 3){
      for(int i = 0; i < N-2; i++){
        for(int j = i+1; j < N-1; j++){
          for(int k = j+1; k < N; k++){
            
          vector<string> vec;
          vec.push_back(P[i]); vec.push_back(P[j]); vec.push_back(P[k]);
          sort(vec.begin(),vec.end());
        do{
          s.insert(vec[0]+vec[1]+vec[2]);
        }while(next_permutation(vec.begin(),vec.end()));
        
            
           
          }
        }
      }
    }
    
    if( K == 4){
      for(int i = 0; i < N-3; i++){
        for(int j= i+1; j < N-2; j++){
          for(int k = j+1;k < N-1; k++){
            for(int l = k+1; l < N; l++){
         vector<string> vec;
          vec.push_back(P[i]); vec.push_back(P[j]); vec.push_back(P[k]); vec.push_back(P[l]);
          sort(vec.begin(),vec.end());
        do{
          s.insert(vec[0]+vec[1]+vec[2]+vec[3]);
        }while(next_permutation(vec.begin(),vec.end()));
        
            }
          }
        }
      }
    }
    cout << s.size() << endl;
  }
    
  
  return 0;}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, save3[10], roop2, card[10], res, save2;
bool save[100000000];
void solve(){
    roop--;
    for (int i = 0; i<n; i++){
        if(save3[i]==1) continue;
            if (roop == 0){
                save2 = res;
                if(card[i]/10>=1){
                    res = res*100;
                }
                else{
                    res = res*10;
                }
                res += card[i];
                if (save[res] == 0){
                    ans++;
                    save[res]=1;
                }
                res = save2;
            }
            else{
                if(card[i]/10>=1){
                    res = res*100;
                }
                else{
                    res = res*10;
                }
                res += card[i];
                save3[i]=1;
                solve();
                save3[i]=0;
                roop++;
                res -= card[i];
                if(card[i]/10>=1){
                    res = res/100;
                }
                else{
                    res = res/10;
                }
        }
    }
}
int main() {
    while (true){
        cin >> n >> k;
        roop2 = 0;
        if (n == 0 && k == 0){
            return 0;
        }
        for (int i = 0; i < n; i++){
            cin >> card[i];
        }
        for (int i = 0; i < n; i++){
            roop = k - 1;
            res = card[i];
            save3[i] = 1;
            solve();
            save3[i]=0;
        }
        cout << ans << endl;
        ans=0;
        for (int i = 0; i < 10000000; i++){
            save[i] = 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 <int> card (n, 0 );
		vector <int> comb (n, 0 );
		for (int i = 0; i < n; ++i){
			cin >> card[i];
			comb[i] = i;
		} // end for

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

		set <string> res;

		set <vector <int> >::iterator it = C.begin();
		for (; it != C.end(); ++it){
			string r = "";
			vector <int> v = (*it);
			vector <int>::iterator jt = v.begin();
			for (; jt != v.end(); ++jt){
				r += itos(card[(*jt)]);
			} // end for
			res.insert (r);
		} // end for 
		cout << res.size() << endl;
	} // end loop
		
	return 0;
}

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

set<int> numbers;
int n, k, cards[10], used[10];

void addcard(int x, int y){
  if(x == 0){
    numbers.insert(y);
  }
  for(int i = 0;i < n;i++){
    if(used[i] == 0){
      int shift = 10;
      if(cards[i] >= 10)shift *= 10;
      used[i] = 1;
      addcard(x - 1, y * shift + cards[i]);
      used[i] = 0;
    }
  }
}

int main(){
  while(1){
    numbers.clear();
    scanf("%d%d", &n, &k);
    if(n == 0 && k == 0)return 0;
    for(int i = 0;i < n;i++){
      used[i] = 0;
      scanf("%d", cards + i);
    }
    addcard(k, 0);
    printf("%d\n", numbers.size());
  }
}