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<pair<string, int> > a;
int n, k, ans;
VS str_array;
char b[2];
//function
void solve(int many_used, VI used_card, string now_string){
  SORT(used_card);
  if(a.count(pair<string, int>(now_string, many_used))){
    return;
  }
  a.insert(pair<string, int>(now_string, many_used));
  if(k == many_used){
    ans++;
    return;
  }
  REP(i, n){
    if(binary_search(ALL(used_card), i)){
      continue;
    }
    used_card.PB(i);
    solve(many_used + 1, used_card, now_string + str_array[i]);
    used_card.pop_back();
  }
}
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, VI(), "");
    printf("%d\n", ans);
    str_array = VS();
    a = set<pair<string, int> >();
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(n,a) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

map<string,bool> mp;
string t[100];
int k;

void g(string s,int m,bool b[],int n){
  if(m==k){
    mp[s]=true;
    return;
  }
  rep(i,n){
    if(!b[i]){
      b[i]=true;
      g(s+t[i],m+1,b,n);
      b[i]=false;
    }
  }
}

void f(int n){
  mp.clear();
  string s="";

  rep(i,n){
    cin>>t[i];
  }
  bool b[100]={};
  g(s,0,b,n);

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


int main(){
  int n;
  while(1){
    cin>>n>>k;
    if(n==0) break;
    f(n);
  }
}

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

#define rep(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define rept(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )

int N, K;
std::string num[11];
std::set<std::string> data;

void f( int used, int cnt, std::string d ){
	if( cnt == 0 ){
		data.insert(d);
		return;
	}

	rep( i, N ){
		if( used & (1 << i) ) continue;

		f( used | 1 << i, cnt - 1, d + num[i] );
	}
}

int main(){
	while( scanf("%d%d", &N, &K), N * K ){
		data.clear();

		rep( i, N ) std::cin >> num[i];

		std::cout << K << std::endl;
		
		f( 0, K, "" );
		
		std::cout << data.size() << std::endl;
	}
}

C++: #include <iostream>
#include <set>
#include <vector>
#include <sstream>

using namespace std;

set<string> s;
vector<int> cards;
int k;
int n;

bool used[10];
int perm[10];
vector<vector<int> > permList;

void make_perm(int cnt){
	if(cnt != k){
		for(int i = 0; i < n; i++){
			if(!used[i]){
				used[i] = true;
				perm[cnt] = i;
				make_perm(cnt+1);
				used[i] = false;
			}
		}
	}
	else{
		vector<int> tmp;
		for(int i = 0; i < k; i++){
			tmp.push_back(perm[i]);
		}
		permList.push_back(tmp);
	}
}


int main(){

	while(cin >> n >> k && !(n == 0 && k == 0)){
		fill(used,used+10,false);


		for(int i = 0; i < n; i++){
			int c;
			cin >> c;
			cards.push_back(c);
		}
	//	dfs(0,0,0);
		make_perm(0);

		for(int i = 0; i < permList.size(); i++){
			string str;
			for(int j = 0; j < permList[0].size(); j++){
				//cout << permList[i][j] << " " << flush;
				stringstream ss;
				ss << cards[permList[i][j]];
				str += ss.str();
			}
			//cout << str << endl;
			//cout << endl;
			s.insert(str);
		}

		cout << s.size() << endl;

		permList.clear();
		cards.clear();
		s.clear();
	}

	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
int main(){
    for(;;){
        int n,k;
        scanf("%d%d",&n,&k);
        if(n==0&&k==0)break;
        vector<int> ns(n);
        for(int i=0;i<n;i++)scanf("%d",&ns[i]);
        set<int> ints;
        sort(ns.begin(),ns.end());
        do{
            int sum=0;
            for(int i=0;i<k;i++){
                if(ns[i]<10) sum=sum*10+ns[i];
                else sum=sum*100+ns[i];
            }
            ints.insert(sum);
        }while(next_permutation(ns.begin(),ns.end()));
        printf("%d\n",ints.size());
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define loop(i,n) for(int i=0;i<n;i++)
int d[11];
set<int> s;

int main(){
  int n,k;
  while(cin>>n>>k,n){
    loop(i,n)cin>>d[i];
    s.clear();
    sort(d,d+n);
    do{
      string t="";
      loop(i,k)t+=to_string(d[i]);
      s.insert(stoi(t));
    }while(next_permutation(d,d+n));
    cout<<s.size()<<endl;
  }
  return 0;
}