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 <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
 
const int INF = 10000000;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
 
int n, k;
set<string> st;
string s;
vi v;
 
void calc(int c, vi u){
    if(u.size() == k){
    	// rep(i, u.size()){
    	// 	cout<<" "<<u[i];
    	// }
    	// cout<<endl;
        do{
            string t;
            for(int i = 0; i < k; i++){
                t += to_string(u[i]);
            }
            st.insert(t);
        }while(next_permutation(all(u)));
        return;
    }
    for(int i = c; i<n; i++){
        u.pb(v[i]);
        calc(i+1, u);
        u.pop_back();
    }
}
 
 
int main(){
    while(cin>>n>>k){
        if(n == 0 && k == 0) break;
        v = vi(n);
        rep(i, n) cin>>v[i];
        s = "";
        st.clear();
        vi tmp;
        calc(0,tmp);
        // for(auto it = st.begin(); it != st.end(); it++){
        // 	cout<<" "<<*it<<endl;
        // }
        cout<<st.size()<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int n, m, a[10], ans;
map<int,int> mp;

int solve(int tmp[4], int k, int sum, int p){
	int t, u[4];
	rep(i,4)u[i] = tmp[i];
	u[k++] = p;
	if(a[p] >= 10)sum = sum*100+a[p];
	else sum = sum*10+a[p];
	
	if(m == k){
		if(mp[sum]==0)ans++;
		mp[sum] = 1;
		return 0;
	}
	
	for(int i = 0;i < n;i++){
		t = 1;
		rep(j,k)if(u[j] == i)t = 0;
		if(t)solve(u, k, sum, i);
	}
	return 0;
}

int main(){
	int u[4] = {0,0,0,0};
	while(1){
		ans = 0;
		mp.clear();
		scanf("%d%d", &n, &m);
		if(!n)break;
		rep(i,n)scanf("%d", &a[i]);
		for(int i = 0;i < n;i++){
			solve(u, 0, 0, i);
		}
		printf("%d\n", ans);
	}
	return 0;
}

C++: #include <iostream>
#include <map>
#include <list>
#include <sstream>
#include <string>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

string print(list<string>& l){
	string s="";
	for(list<string>::iterator it=l.begin();it!=l.end();it++){
		s+=*it;
	}
	return s;
}

int main(void){
	int n,K;

	while(cin>>n>>K,n||K){
		string a[10];
		string s;
		std::map<string,bool> m;
		std::list<string> L;
		for(int i=0;i<n;i++)cin>>a[i];
		rep(i,n){
			L.push_back(a[i]);
			if(K==1){
				m[print(L)]=true;
				L.pop_back();
				continue;
			}
			rep(j,n){
				if(i==j)continue;
				L.push_back(a[j]);
				if(K==2){
					m[print(L)]=true;
					L.pop_back();
					continue;
				}
				rep(k,n){
					if(i==k||j==k)continue;
					L.push_back(a[k]);
					if(K==3){
						m[print(L)]=true;
						L.pop_back();
						continue;
					}
					rep(l,n){
						if(i==l||j==l||k==l)continue;
						L.push_back(a[l]);
						if(K==4){
							m[print(L)]=true;
							L.pop_back();
							continue;
						}
						L.pop_back();
					}
					L.pop_back();
				}
				L.pop_back();
			}
			L.pop_back();
		}
		cout<<m.size()<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<math.h>
using namespace std ;

int n , k ;
int m[20] ;
int num[100000000] = {} ;

int calc( int y , int x ){
	int j ;
	for( j=1 ; pow(10,j) < y ; j++ ){}
	
	return y + x * pow(10,j) ;
}

int f( int p , int sum , int bit_sum ){
	if( p == k ){
		if( num[sum] == 0 ){
			num[sum] = 1 ;
			return 1 ;
		}
	}
	
	int ans=0 ;
	for( int i=0 ; i<n ; i++ ){
		int c = pow(2,i) ;
		if( c & bit_sum ){}
		else {
			ans += f( p+1 , calc(sum,m[i]) , c+bit_sum ) ;
		}
	}
	
	return ans ;
}

int main(){
	
	while( cin >> n >> k ){
		if( n==0 && k==0 ) break ;
		
		for( int i=0 ; i<n ; i++ ) cin >> m[i] ;
		for( int i=0 ; i<100000000 ; i++ ) num[i] = 0 ;
		
		int ans2 = 0 ;
		for( int i=0 ; i<n ; i++ ){
			ans2 += f(1,m[i],pow(2,i)) ;
		}
		
		cout << ans2 << endl ;
	}
	
return 0 ;
}

C++: #include<iostream>
#include<set>
#include<string>
#include<sstream>
#include<algorithm>

int n, k;
int cards[11];
std::set<std::string>s;

std::string toStr(int n){
	std::stringstream z;
	std::string res;

	z << n;
	z >> res;

	return res;
}

void makecard(){
	int id[11];
	for (int i = 0; i < n; i++)id[i] = i;
	s.clear();

	do{
		std::string res;
		for (int i = 0; i < k; i++){
			res += toStr(cards[id[i]]);
		}
		s.insert(res);
	} while (std::next_permutation(id, id + n));
}

int main(){

	while (std::cin >> n >> k, n || k){

		for (int i = 0; i < n; i++){
			std::cin >> cards[i];
		}

		makecard();

		std::cout << s.size() << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

int n,k,d[10];

int sum(int a,int b,int c=0,int d=0){
	int x=a*(b>=10?100:10)+b;
	if(k==2)return x;
	x=x*(c>=10?100:10)+c;
	if(k==3)return x;
	return x=x*(d>=10?100:10)+d;
}

int main(){
	while(scanf("%d%d",&n,&k),n){
		for(int i=0;i<n;i++)scanf("%d",&d[i]);
		sort(d,d+n);
		vector<int> data;
		for(int i=n-1;i>=0;i--){
			for(int j=n-1;j>=0;j--){
				if(i==j)continue;
				if(k==2){
					data.push_back(sum(d[i],d[j]));
					continue;
				}
				for(int r=n-1;r>=0;r--){
					if(i==r||j==r)continue;
					if(k==3){
						data.push_back(sum(d[i],d[j],d[r]));
						continue;
					}
					for(int z=n-1;z>=0;z--){
						if(i==z||j==z||r==z)continue;
						data.push_back(sum(d[i],d[j],d[r],d[z]));
					}
				}
			}
		}
		sort(data.begin(),data.end());
		data.erase(unique(data.begin(),data.end()),data.end());
		printf("%ld\n",data.size());
	}
}

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

int main(){
    int n, k;
    while(cin >> n >> k){
        if(n + k == 0)  break;
        string x[n];
        for(int i = 0; i < n; i++)  cin >> x[i];
        sort(x, x+n);

        set<int> ans;
        do{
            string tmp = "";
            for(int i = 0; i < k; i++)  tmp = tmp + x[i];
            ans.insert(stoi(tmp));
        }while(next_permutation(x, x+n));

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