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>
#include <stdio.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (long long i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int n,k;
set<string> st;
string s[10];
bool used[10];

void dfs(int depth,string num) {
	if(depth==0) {
		st.insert(num);
		return;
	}
	REP(i,n) {
		if(used[i]) continue;
		used[i]=true;
		dfs(depth-1,num+s[i]);
		used[i]=false;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

    while(true) { 
	cin>>n>>k;
	if(n==0&&k==0) break;
	st.clear();
	REP(i,n) {
		cin>>s[i];
	}
	REP(i,n) {
		REP(i,10) used[i]=false;
		used[i]=true;
		dfs(k-1,s[i]);
	}

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

	return 0;
}

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

int main()
{
	int n, k;
	while (1){
		cin >> n >> k;
		if (n == 0 and k == 0) return 0;
		string s[n];
		set<string> s_perm;;
		for (int i = 0; i < n; i++) cin >> s[i];

		sort(s, s+n);

		do {
			string tmp;
			for (int i = 0; i < k; i++) tmp += s[i];
			s_perm.insert(tmp);
		} while (next_permutation(s, s+n));

		cout << (int)s_perm.size() << endl;
	}
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<sstream>
#include<cstdlib>

using namespace std;

int main(){
	while(1){
		int n,k;
		scanf("%d%d",&n,&k);
		
		if(n==0)
			break;
		
		vector<int> card(n);
		for(int i=0;i<n;i++){
			scanf("%d",&card[i]);
		}
		
		sort(card.begin(),card.end());
		
		vector<int> data;
		do{
			
			stringstream sst;
			for(int i=0;i<k;i++){
				sst<<card[i];
			}
			data.push_back(atoi(sst.str().c_str()));
			
			
			reverse( card.begin()+k,card.end());
		}while( next_permutation(card.begin(),card.end()) );
		
		sort(data.begin(),data.end());
		
		
		printf("%d\n",distance(data.begin(),unique(data.begin(),data.end())) );
	}
}

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;
  string s;
  FOR(i,n) 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);
   do{
    s="";
    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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>

using namespace std;

#define sz			size()
#define lgt			length()
#define rep(i,n)	for (int i = 0; i < n; i++)
#define ALL(X)		(X).begin(),(X).end()
#define RALL(X)		(X).rbegin(),(X).rend()
#define pb			push_back
#define SORT(a)		sort(ALL(a));
#define RSORT(a)	sort(RALL(a));
#define mp			make_pair
#define dump(x)		cerr<<#x<<" = "<<(x)<<endl;
#define fir			first
#define sec			second

typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<string> vs;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
vs form(string a,char s){vs v;string b;rep(i,a.lgt){if(a[i]==s){v.pb(b);b="";}else b+=a[i];}v.pb(b);return v;}
vi formint(string a,char s){vi v;string b;rep(i,a.lgt){if(a[i]==s){v.pb(toInt(b));b="";}else b+=a[i];}v.pb(toInt(b));return v;}

const double EPS=1e-10,PI=acos(-1.0);
const int INF=2100000000;

int main(void)
{
	int n, k;
	while (cin >> n >> k, n && k){
		string num[16];
		vector <string> ans;
		for (int i = 0; i < n; i++)	cin >> num[i];
		
		if (k == 2){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					if (i != j){
//						cout << i << " " << j << endl;
						string tmp;
						tmp += num[i];
						tmp += num[j];
//						cout << "*" << tmp << endl;
						ans.pb(tmp);
					}
				}
			}
		}
		if (k == 3){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					for (int k = 0; k < n; k++){
						if (i != j && j != k && i != k){
							string tmp;
							tmp += num[i];
							tmp += num[j];
							tmp += num[k];
							ans.pb(tmp);
						}
					}
				}
			}
		}
		if (k == 4){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					for (int k = 0; k < n; k++){
						for (int l = 0; l < n; l++){
							if (i != j && i != k && i != l && j != k && j != l && k != l){
								string tmp;
								tmp += num[i];
								tmp += num[j];
								tmp += num[k];
								tmp += num[l];
								ans.pb(tmp);
							}
						}
					}
				}
			}
		}
		
		ans.pb("2100000000");
		SORT(ans);
		int res = 0;
		for (int i = 0; i < ans.sz - 1; i++){
			if (ans[i] != ans[i + 1]) res++;
		}
		cout << res << endl;
	}
	
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<sstream>
#include<set>
using namespace std;
class P{
    public:
    int num;
    int cnt;
    bool used[10];
    P(){}
    P(int num,int cnt,bool a[10]):num(num),cnt(cnt){
        for(int i = 0; i < 10; i++){
            used[i] = a[i];
        }
    }
};
int bond(int x,int y){
    stringstream ss1;
    ss1 << x;
    stringstream ss2;
    ss2 << y;
    string ans = ss1.str() + ss2.str();
    stringstream Ans;
    Ans << ans;
    int res;
    Ans >> res;
    return res;
}
int main(){
    int n;
    int m;
    while(scanf("%d%d",&n,&m),(n|m)){
        int card[10];
        set<int> S;
        queue<P> Q;
        for(short i = 0; i < n; i++){
            bool x[10]={0};
            x[i] = 1;
            scanf("%d",&card[i]);
            Q.push(P(card[i],1,x));
        }
        short ans = 0;
        while( Q.size() ){
            P p = Q.front();Q.pop();
            if(p.cnt == m){
                if(S.find(p.num) == S.end())ans++,S.insert(p.num);
            }
            else{
                for(int i = 0; i < n; i++){
                    if(p.used[i] == 0){
                        p.used[i] = 1;
                        Q.push(P(bond(p.num,card[i]),p.cnt+1,p.used));
                        p.used[i] = 0;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}