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 <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <utility>
#include <functional>

using namespace std;

int n, k, sel[4];
string s[10];
bool used[10];
map<string, bool> mp;

void dfs(int d) {
	if (d == k) {
		string t;
		for (int i = 0; i < k; ++i) {
			t += s[sel[i]];
		}
		mp[t] = true;
		return;
	}

	for (int i = 0; i < n; ++i) {
		if (used[i])
			continue;
		sel[d] = i;
		used[i] = true;
		dfs(d + 1);
		used[i] = false;
	}
}

int main() {
	while (cin >> n >> k && n != 0) {
		for (int i = 0; i < n; ++i) {
			cin >> s[i];
		}

		fill(used, used + n, false);
		mp.clear();
		dfs(0);

		cout << mp.size() << endl;
	}

	return 0;
}

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

using namespace std;

void push_string(vector<string>& ss, set<string>& hoge){
  do{
    string tmp="";
    for(int i=0; i<ss.size(); i++)
      tmp += ss[i];
    //    cout << tmp << "\n";
    hoge.insert(tmp);
  }while(next_permutation(ss.begin(),ss.end()));
}


int main(){

  while(1){
    int n,k; cin >> n >> k;
    if(n==0 && k==0) break;
    vector<string> ss;
    set<string> hoge;
    for(int i=0; i<n; i++){
      string x; cin >> x;
      ss.push_back(x);
    }
    sort(ss.begin(),ss.end());
    switch(k){
    case 2:
      for(int i=0; i<n; i++){
	for(int j=i+1; j<n; j++){
	  vector<string> tmp;
	  tmp.clear();
	  tmp.push_back(ss[i]);
	  tmp.push_back(ss[j]);
	  push_string(tmp,hoge);
	}
      }
      break;
    case 3:
      for(int i=0; i<n; i++){
	for(int j=i+1; j<n; j++){
	  for(int k=j+1; k<n; k++){
	    vector<string> tmp;
	    tmp.push_back(ss[i]);
	    tmp.push_back(ss[j]);
	    tmp.push_back(ss[k]);
	    push_string(tmp,hoge);
	  }
	}
      }
      break;
    case 4:
      for(int i=0; i<n; i++){
	for(int j=i+1; j<n; j++){
	  for(int k=j+1; k<n; k++){
	    for(int l=k+1; l<n; l++){
	      vector<string> tmp;
	      tmp.push_back(ss[i]);
	      tmp.push_back(ss[j]);
	      tmp.push_back(ss[k]);
	      tmp.push_back(ss[l]);
	      push_string(tmp,hoge);
	    }
	  }
	}
      }
      break;
    }
    cout << hoge.size() << "\n";

  }

}

C++: #include<bits/stdc++.h>
#define N 10
using namespace std;
typedef long long ll;
int n,k,a[N];
set<ll> s;

int check(){
  int perm[N];
  for(int i=0;i<n;i++)perm[i]=i;
  do{
    ll num=0;
    for(int i=0;i<k;i++){
      if(!(a[perm[i]]/10))num=num*10+a[perm[i]];
      else num=num*100+a[perm[i]];
    }
    s.insert(num);
  }while(next_permutation(perm,perm+n));
  return s.size();
}

int main(){
  while(1){
    cin>>n>>k;
    if(!n&&!k)break;
    for(int i=0;i<n;i++)
      cin>>a[i];
    cout<<check()<<endl;
    s.clear();
  }
  return 0;
}

C++: #include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#define debug(x) cerr << #x << ':' << x << endl
using namespace std;
typedef long long ll;
const int INF=1000000;

int main(){
    int n, k, C[1000];
    int checked_num[INF]={};
    while(1){
        cin >> n >> k;
        if(n==0 && k==0) break;
        for(int i=0; i<n; i++)
            cin >> C[i];
        sort(C, C+n);

        // 全ての組み合わせを考える
        do{
            string num="";
            for(int i=0; i<k; i++)
                num+=to_string(C[i]);
            checked_num[stoi(num)]=1;
        }while(next_permutation(C, C+n));

        int ans=0;
        for(int i=0; i<INF; i++)
            ans+=checked_num[i];
        cout << ans << endl;

        memset(C, 0, 1000*sizeof(int));
        memset(checked_num, 0, INF*sizeof(int));
    }
    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 **/
int main()
{
	int N, K;
	while (cin>>N, N) {
		cin>>K;
		vector<int> card(N);
		rep(i, N) cin>>card[i];
		sort(card.begin(), card.end());
		set<string> ans;
		do {
			string s;
			rep(i, K) {
				char t[3]; sprintf(t, "%d", card[i]);
				s += t;
			}
			//cerr << s << endl;
			ans.insert(s);
		} while (next_permutation(card.begin(), card.end()));
		cout << ans.size() << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout<<(n)<<endl
#define Cout(n) cout<<(n)
#define rep(i,num,N) for(int(i)=(num);(i)<(N);++(i))
#define rrep(i,num,N) for(int (i)=(num);(i)>(N);--(i))
#define all(v) (v).begin() , (v).end()
#define rall(v) (v).rbegin() , (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,K;
    
    while(cin>>N>>K,N!=0&&K!=0){
        set<string> num;
        vector<string> card(N);
        rep(i,0,N) cin>>card[i];
        sort(all(card));

        do{
            rep(i,0,N-K){
                string s = "";
                rep(j,i,K+i){
                    s += card[j]; 
                }
                num.insert(s);
            }
            
        }while(next_permutation(all(card)));

        put(num.size());
    }
}