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 <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)

using ll=long long;
using ld=long double;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int, int>;
using vpii=vector<pii>;

template<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>p){os<<"("<<p.fst<<", "<<p.scd<<")";return os;}
template<class T>ostream&operator<<(ostream&os,const vector<T>v){rep(i,v.size()){if(i)os<<", ";os<<v[i];}return os;}
template<typename T>T&max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T&min(T&a,T&b){if(a < b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a < b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a > b){a=b;return true;}return false;}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

int n, k;
string a[11];
bool used[11];
set<string> se;

void dfs(int j, string s="")
{
    if (j == 0) {
        se.insert(s);
        return;
    }

    rep(i, n) {
        if (used[i]) continue;
        used[i] = true;

        dfs(j - 1, s + a[i]);

        used[i] = false;
    }
}

int main()
{
    cin >> n;
    cin >> k;

    rep(i, n) cin >> a[i];

    dfs(k);

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

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<cstdio>
#include<set>
#include<cstring>

using namespace std;

set<int> se;

int N,K;

int card[10];
bool used[10];

void rec(int num,int k)
{
	if(k==K)
	{
		se.insert(num);
		return;
	}
	for(int i=0;i<N;i++)
	{
		if(used[i]==true) continue;
		used[i]=true;
		int nnum;
		if(card[i]<10) nnum=num*10+card[i];
		else nnum=num*100+card[i];
		rec(nnum,k+1);
		used[i]=false;
	}
}

int main()
{
	scanf("%d%d",&N,&K);
	for(int i=0;i<N;i++)
	{
		scanf("%d",card+i);
	}
	memset(used,false,sizeof(used));
	rec(0,0);
	printf("%d\n",se.size());
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1<<29
#define MAX 11
using namespace std;

int a[MAX],s[MAX];

void solve(){
 int n,k;
 for(;;){
  cin>>n;
  cin>>k;
  if(!n) break;
  set<string> S;
  FOR(i,k) cin>>a[i];
  FOR(i,1<<n){
   if(__builtin_popcount(i)!=k) continue;
   int p=0;
   FOR(j,n) if((1<<j)&i) s[p++]=j;
   vector<int> ord(k);
   FOR(j,k) ord.pb(j);
   string s="";
   do{
    FOR(j,k) s+=to_string(s[ord[j]]);
    S.insert(s);
   }while(next_permutation(ord.begin(),ord.end()));
  }
  cout<<S.size()<<endl;
 }
}

int main(){
 solve();
 return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<algorithm>
using namespace std;
int card[11],n,k,bit=1,ans=0;
string str;
vector<string> V;
vector<int> num;
string IntToString(int n){
	stringstream ss;
	ss<<n;
	return ss.str();
}

int main(){
	while(1){
		cin>>n;
		cin>>k;
		ans=0;
		if(n==0 && k==0)
		break;
		for(int i=0;i<n;i++)
		cin>>card[i];
		V.erase(V.begin(),V.end());
		for(int i=0;i<(1<<n);i++){
			num.erase(num.begin(),num.end());
			int count=0;
			int flag=0;
			for(int j=0;j<n;j++){
				if(i&(1<<j)){
					num.push_back(card[j]);
					count++;
				}
			}
			if(count==k){
				sort(num.begin(),num.end());
				while(1){
					flag=0;
					str="";
					for(int j=0;j<k;j++){
						str+=IntToString(num[j]);
					}
					for(int j=0;j<V.size();j++){
						if(V[j]==str)
						flag=1;
					}
					if(flag==0){
						V.push_back(str);
						ans++;
					}
					if(next_permutation(num.begin(),num.end())==0)
					break;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
#include <set>
using namespace std;

inline int toInt(string s){
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}

int main(){
  int n, k;
  while(cin >> n >> k){
    if(n == 0 && k == 0) break;
    string s;
    vector<string> data;
    for(int i = 0 ; i < n ; i++){
      cin >> s;
      data.push_back(s);
    }
    sort(data.begin(), data.end());
    set<int> res;
    do{
      string t = "";
      for(int i = 0 ; i < k ; i++){
	t += data[i];
      }
      int tt = toInt(t);
      res.insert(tt);
    }while(next_permutation(data.begin(), data.end()));
    
    cout << res.size() << endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main(void) {
  int i, j, n, k, l, m, o;
  while( 1 ) {
    scanf("%d%d", &n, &k);
    if(!n && !k) break;
    vector<string> v;
    string a[n], t;
    for(i = 0; i < n; ++i) cin >> a[i];
    for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) {
      if(i == j) continue;
      t = a[i] + a[j];
      if(k == 2) v.push_back(t);
      else {
        for(l = 0; l < n; ++l) {
          if(i == l || j == l) continue;
          t += a[l];
          if(k == 3) v.push_back(t);
          else {
            for(m = 0; m < n; ++m) {
              if(i == m || j == m || l == m) continue;
              t += a[m];
              v.push_back(t);
              for(o = 0; o < a[m].length(); ++o) t.pop_back();
            }
          }
        for(o = 0; o < a[l].length(); ++o) t.pop_back();
        }
      }
    }
   sort(v.begin(), v.end());
   printf("%d\n", (int)(unique(v.begin(), v.end()) - v.begin()));
   vector<string>::iterator it;
   //for(it = v.begin(); it != unique(v.begin(), v.end()); ++it) cout << (*it) << " ";
  }
  return 0;
}