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;
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;
    stringstream ss;
    ss << cards[i];
    a+=ss.str();
    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++: 
//10-4 カード並べ
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<time.h>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<stack>
#include<cstdlib>
#include<algorithm>
#include<random>
#include<cassert>
using namespace std;
#define LL long long
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define ROOP() while(true)

int n,k;
int num[10];
bool used[10];
set<string> ans;

void choose(string now, int nokori){
    if(nokori==0){
        ans.insert(now);
    }
    else{
        for(int i=0; i<n; i++){
            if(used[i] == false){
                used[i] = true;
                choose(to_string(num[i])+now, nokori-1);
                used[i] = false;
            }
        }
    }
}

int main(){
    ROOP(){
        cin >> n >> k;
        if(n==0 && k==0) break;

        ans.clear();

        for(int i=0; i<n; i++){
            int tmp;
            cin >> tmp;
            num[i] = tmp;
            used[i] = false;
        }

        for(int i=0; i<n; i++){
            used[i] = true;
            choose(to_string(num[i]), k-1);
            used[i] = false;
        }

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

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

using namespace std;

int n;
int k;

vector<int> card;
set< string > cd;
set< vector<int> > ccd;

/*
string IteratorToString(vector<int>::iterator begin, vector<int>::iterator end){
	string str = "";
	char ch[256];
	for(;begin < end;begin++){
		sprintf(ch,"%d",*begin);
		str += ch;
	}
	return str;
}
*/
string VectorToString(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
	}
	return str;
}

string VectorToDebugText(const vector<int> &vec){
	string str = "";
	for(int i=0;i<vec.size();i++){
		char ch[256];
		sprintf(ch,"%d",vec[i]);
		str += ch;
		str += ":";
	}
	return str;
}

void solve(int a,vector<int> v){
	if(v.size() == k){
		sort(v.begin(),v.end());
		ccd.insert(v);
		return;
	}
	if(a == n){return;}
	solve(a + 1,v);
	v.push_back(card[a]);
	solve(a + 1,v);
}


int main(){
	while(1){
		int self;
		scanf("%d %d",&n,&k);
		if(n == 0 && k == 0){
			break;
		}
		card.clear();
		cd.clear();
		ccd.clear();
		for(int i=0;i<n;i++){
			scanf("%d",&self);
			card.push_back(self);
		}
		vector<int> cc(k);
	
		sort(card.begin(),card.end());
		/*
		while(next_permutation(card.begin(),card.end())){
			 cd.insert(VectorToString(card.begin(),card.begin()+k));
		}
		*/
		solve(0,vector<int>());
		for(set<vector<int> >::iterator it = ccd.begin();it != ccd.end();it++){
			vector<int> vec = *it;
			sort(vec.begin(),vec.end());
			cd.insert(VectorToString(vec));
			//printf("[%s]\n",VectorToDebugText(vec).c_str());
			while(next_permutation(vec.begin(),vec.end())){
				//printf("%s\n",VectorToDebugText(vec).c_str());
				//printf("%s\n",VectorToString(vec).c_str());
				cd.insert(VectorToString(vec));
			}
		}
		printf("%d\n",cd.size());
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define f first
#define s second

set<string> ans;
vi in;
vi select;

void dfs(int k,int pos){
  //cout<<"dfs "<<k<<" "<<pos<<endl;
  if(k==0){
    sort(ALL(select));
    do{
      string str;
      rep(i,select.size()){
        stringstream ss;
        ss<<select[i];
        str+=ss.str();
      }
      ans.insert(str);
    }while(next_permutation(ALL(select)));
    sort(ALL(select));
    reverse(ALL(select));
  }else{
    for(int i=pos;i<in.size();i++){
      select[k-1]=in[i];
      dfs(k-1,i+1);
    }
  }
}

main(){
  int n,k;
  while(cin>>n>>k,k|n){
    in.clear();
    ans.clear();
    select.clear();
    select.resize(k);
    rep(i,n){
      int t;
      cin>>t;
      in.pb(t);
    }
    sort(ALL(in));

    
    dfs(k,0);
    cout<<ans.size()<<endl;
  }
}

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

using namespace std;
int main()
{

	bool p[1000001];
	int n, k;
	string m[1000];
	string a;
	int count = 0;
	while (1){
		count = 0;
		cin >> n >> k;
		if (n == 0)return 0;
		for (int i = 0; i < n; i++){
			cin >> m[i];
		}
		if (k == 4){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){
					for (int i3 = 0; i3 < n; i3++){
						for (int i4 = 0; i4 < n; i4++){
							if (i != i2&&i3 != i4&&i != i3&&i != i4&&i2 != i3&&i2 != i4){
								a = m[i] + m[i2] + m[i3] + m[i4];
								p[atol(a.c_str())] = false;
								a.clear();
							}
						}
					}
				}
			}

		}
		long u = 0;
		if (k == 3){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){
					for (int i3 = 0; i3 < n; i3++){
						if (i != i2&&i != i3&&i2 != i3){
							a = m[i];
							a += m[i2];
							a += m[i3];

							u = atol(a.c_str());
							p[u] = false;
							a.clear();
						}
					}
				}
			}
		}
		int o;
		if (k == 2){
			for (int i = 0; i < n; i++){
				for (int i2 = 0; i2 < n; i2++){

					if (i != i2){
						a = m[i] + m[i2];
						o = atoi(a.c_str());


						p[o] = false;
						a.clear();
					}
				}
			}
		}
		
		for (int i = 0; i < 100000; i++){
			if (!p[i])count++;
		}
		cout << count << endl;
	}
}