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
static set<pair<string, VS> > e;
static int n, k, ans;
static VS str_array;
static VS ans_string;
static char b[2];
//function
void solve(int many_used, string now_string, VS can_use){
  if(e.count(pair<string, VS>(now_string, can_use))){
      return;
  }
  e.insert(pair<string, VS>(now_string, can_use));
  if(k == many_used && !binary_search(ALL(ans_string), now_string)){
    ans++;
    ans_string.PB(now_string);
    SORT(ans_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();
    e = set<pair<string,VS> >();
    ans_string = VS();
  }
  return 0;
}

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

using namespace std;

int n;
int num[5];
int card[11];
set<string> ans;

void serch(int m,int k,int now)
{
	
	for(int i=0;i<m;i++)
	{
		for(int j=i+1;j<m;j++)
		{
			if(num[i]==num[j])
			{
				return;
			}
		}
	}
	
	if(m==k)
	{
		string str;
		char  ch[8];
		for(int i=0;i<m;i++)
		{
			sprintf(ch,"%d",card[num[i]]);
			str+=string(ch);
		}
		ans.insert(str);
		return ;
	}
	
	num[m]=now;
	
	for(int i=0;i<n;i++)
	{
		serch(m+1,k,i);
	}
	
	return ;
}

int main()
{
	int k;
	int i,j;
	
	for(;;)
	{
		cin >> n >> k;
		if(n==0 && k==0)
			break;
		
		ans.clear();
		
		for(i=0;i<n;i++)
			cin >> card[i] ;
		
		for(i=0;i<n;i++)
		{
			serch(0,k,i);
		}
		
		set<string>::iterator it = ans.begin();
		
/*
		for(;it!=ans.end();++it)
		{
			cout << "ans:" << *it << endl;
		}
*/
		
		cout << ans.size() << endl;
	}
	
}

C++: #include<stdio.h>
#define INF 100000
int min(int a,int b){return a<b?a:b;}
int getk(int a){
	int res=0;
	while(a>0){
		res++;
		a/=10;
	}
	return res;
}
int pow(int a,int b){
	int l;
	int rt=1;
	for(l=0;l<b;l++)rt*=10;
	return rt;
}
int card[10];
bool pos[100000000];
int main(){
	int i,j;
	int n,k;
	while(1){
	scanf("%d",&n);
	scanf("%d",&k);
		if(n==0)return 0;
	for(i=0;i<n;i++)scanf("%d",&card[i]);
	for(i=0;i<100000000;i++)pos[i]=false;
	int max=1;
	int ket;
	int next;
	int ni;
	int num;
	int ans=0;
	bool used[10];
	for(i=0;i<k;i++)max*=n;
	for(i=0;i<max;i++){
cont:		
		for(j=0;j<n;j++)used[j]=false;
		ket=1;
		ni=i;
		num=0;
		for(j=0;j<k;j++){
			next=ni%n;
			ni/=n;
			if(used[next]){
				i++;
				if(i==max)goto end;
				goto cont;
			}
			else used[next]=true;
			num+=card[next]*ket;
			ket*=pow(10,getk(card[next]));
		}
		if(!pos[num]){
			ans++;
			pos[num]=true;
		}
	}
end:
	printf("%d\n",ans);
	}
}

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

vector<int> ary,aryh;

template <class BifirectionalIterator> bool next_partial_permutation(BifirectionalIterator first,BifirectionalIterator last,int k){
	reverse(first+k,last);
	return next_permutation(first,last);
}

int myp(int x,int y){
	int a;
	a=1;
	for(int i=0;i<y;i++)a*=x;
	return a;
}

int main(){
	int N,K,tmp,ket,hog;
	set<int> s;
	set<int>::iterator it;
	while(1){
		cin>>N>>K;
		ary.clear();
		if(N==0&&K==0) break;
		for(int i=0;i<N;i++){
			cin>>tmp;
			ary.push_back(tmp);
		}
		s.clear();

		sort(ary.begin(),ary.end());
		do{
			
			ket=hog=0;
			for(int i=0;i<K;i++){
				hog+=ary[i]*myp(10,ket);
				//?????????
				if(ary[i]>9){
					ket+=2;
				}
				else{
					ket++;
				}
			}
			s.insert(hog);

		}while(next_partial_permutation(ary.begin(),ary.end(),K));
		//
		/*for(it=s.begin();it!=s.end();it++){
			cout<<*it<<" ";
		}
		cout<<endl;*/
		//
		cout<<s.size()<<endl;
	}
	return 0;
}

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

int main(){
    int n, k;
    while(cin >> n >> k, n+k){
        vector<string> v(n), res;
        for(int i = 0; i < n; i++)  cin >> v[i];
        for(int i = 0; i < 1<<n; i++){
            vector<string> c;
            for(int j = 0; j < n; j++){
                if((i>>j)&1)    c.push_back(v[j]);
            }
            if(c.size() != k)   continue;
            sort(c.begin(), c.end());
            do{
                string t = "";
                for(int j = 0; j < k; j++)  t += c[j];
                res.push_back(t);
            }while(next_permutation(c.begin(), c.end()));
        }
        sort(res.begin(), res.end());
        res.erase(unique(res.begin(),res.end()),res.end());
        cout << res.size() << endl;
    }
    return 0;
}