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++: /* AOJ 0546
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0546
 *
 */


//include
#include<bits/stdc++.h>

//using
using namespace std;

//define static const typedef
#define FOR(i, s, n) for(int (i) = (s); (i) < (n); (i)++)
#define REP(i, n) FOR((i), 0, (n))
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define DUMP(x) cerr << #x << " = " << x << "\n";
#define DEBUG(x) cerr << #x << " = " << x << " (L:" << __LINE__ << ") " << __FILE__ << "\n";
#define F first
#define S second

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned long long ULL;

static const int INF = 1 << 25;

//global
set<string> a;
int n, k, ans;
VS str_array;
char b[2];
//function
void solve(int many_used, string now_string, VS can_use){
  if(k == many_used && !a.count(now_string)){
    ans++;
    a.insert(now_string);
    //DEBUG(now_string)
    return;
  }
  REP(i, can_use.size()){
    string str = can_use[i];
    can_use.erase(can_use.begin() + i);
    solve(many_used + 1, now_string + str, can_use);
    can_use.PB(str);
    SORT(can_use);
  }
}
int main(){
  while(scanf("%d %d", &n, &k) && n && k){
    REP(i, n){
      scanf("%s", b);
      str_array.PB((string)b);
    }
    ans = 0;
    SORT(str_array);
    solve(0, "", str_array);
    printf("%d\n", ans);
    str_array = VS();
    a = set<string>();
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

int next_combination(int comb) {
    int x = comb & -comb, y = comb + x;
    return ((comb & ~y) / x >> 1) | y;
}

int main() {
    int n, k;
    while (cin >> n >> k, n) {
        vector<string> cards(n);
        for (int i = 0; i < n; i++) cin >> cards[i];
        sort(cards.begin(), cards.end());
        unordered_set<string> res;
        for (int bit = (1 << k) - 1; bit < 1 << n; bit = next_combination(bit)) {
            vector<string> member;
            for (int i = 0; i < n; i++) {
                if (bit & (1 << i)) member.push_back(cards[i]);
            }
            do {
                string tmp = "";
                for (int i = 0; i < k; i++) tmp += member[i];
                res.insert(tmp);
            } while (next_permutation(member.begin(), member.end()));
        }
        cout << res.size() << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<sstream>
#include<set>
#include<string>
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++: #include <set>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

typedef long long ll;

#ifdef _MSC_VER
#define __builtin_popcount __popcnt
#endif

int n, m;
int main() {
	while (cin >> n >> m, n) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) cin >> a[i];
		vector<set<ll> > dp(1 << n);
		dp[0] = set<ll>({ 0 });
		for (int i = 1; i < 1 << n; i++) {
			if (__builtin_popcount(i) > m) continue;
			for (int j = 0; j < n; j++) {
				if (i & (1 << j)) {
					for (int k : dp[i - (1 << j)]) {
						dp[i].insert(stoll(to_string(k) + to_string(a[j])));
					}
				}
			}
		}
		set<ll> ret;
		for (int i = 0; i < 1 << n; i++) {
			if (__builtin_popcount(i) == m) {
				ret.insert(dp[i].begin(), dp[i].end());
			}
		}
		cout << ret.size() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
using namespace std;

int main() {
  while (true) {
    int n, k;
    cin >> n >> k;
    if (!n && !k) return 0;
    vector<string> v;
    for (int i = 0; i < n; i++) {
      string tmp;
      cin >> tmp;
      v.push_back(tmp);
    }
    sort(v.begin(), v.end());
    set<string> generate;
    do {
      string res;
      for (int i = 0; i < k; i++) {
        res += v[i];
      }
      generate.insert(res);
    } while (next_permutation(v.begin(), v.end()));
    cout << generate.size() << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<map>
 
using namespace std;
 
int n,k,ans;
vector<string> V;
map<string,int> M;
map<string,int> USED;
void init(){
  V.clear();
  M.clear();
  USED.clear();
  ans = 0;
}
 
void solve(string s, int rev, int used){
  // cout << "s " << s << endl;
  if(rev == k && USED[s] == 0){
    USED[s] = 1;
    ans++;
    return;
  }
 
  for(int i = 0; i < V.size(); i++)
    if(((1<<i)&used) == 0){
      if(M[s+V[i]] != rev+1){
	M[s+V[i]] = rev+1;
	solve(s+V[i],rev+1,used+(1<<i));
      }
    }
}
 
int main(){
  while(cin >> n >> k && n+k){
    init();
    for(int i = 0; i < n; i++){
      string in;
      cin >> in;
      V.push_back(in);
    }
    solve("",0,0);
    cout << ans << endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<set>
#include<sstream>
#include<string>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

int n, k;
int card[10];

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.str();
}

int main()
{
	while( scanf( "%d%d", &n, &k ), n|k )
	{
		rep( i, n )
			scanf( "%d", card+i );

		int comb = (1<<k)-1;
		std::set<std::string> s;
	
		while( comb < 1<<n )
		{
			std::vector<int> v;
			rep( i, n )
				if( comb >> i & 1 )
					v.push_back( i );

			do
			{
				std::string str;
				rep( i, k )
					str += intToStr( card[v[i]] );
				s.insert( str );
			}while( std::next_permutation( v.begin(), v.end() ) );

			int x = comb & -comb, y = comb + x;
			comb = ( (comb & ~y) / x >> 1 ) | y;
		}

		printf( "%d\n", s.size() );
	}

	return 0;
}