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 <climits>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000007


using namespace std;
#define ll long long
#define dbg if(0)
#define ISRANGE(val,lo,hi) ((lo<=val)&&(val<hi))
//単純に考えて並べ方は最大でも10*9*8通り(十分)
//存在するか否かで検索をかけるのにいくつ掛かるか？が問題→多分余裕

ll s[10];
set<int> nums;

ll keta(ll a){
    ll b=a;
    ll ans=10;
    while(b/10!=0){
        b/=10;
        ans*=10;
    }
    return ans;
}

ll getsearch(ll n,ll k,ll used,ll value){
    ll ans=0;
    if(k==0){
        if(nums.find(value)==nums.end()){
            nums.insert(value);
        dbg cout <<"kouho:"<<value<<endl;
            return 1;
        }else{
            return 0;
        }
    }
    for(ll i=0; i < n; i++){
        if((used & (1<<i))==0){
            used+=(1<<i);
            ans+=getsearch(n,k-1,used,value*keta(s[i])+s[i]);
            used-=(1<<i);
        }
    }    
    return ans;
}

int main(){
    ll ans;

    while(true){
        ll n,k;
        nums.clear();
        cin >>n>>k;
        if(n==0&&k==0)break;
        for(int i=0;i<n;i++){
            cin >>s[i];
        }
        cout <<getsearch(n,k,0,0)<<endl;

    }

    return 0;

}

C++: #include <iostream>
#include <string>
using namespace std;
int n, k, ans = 0, roop, num, save3, roop2;
bool save[10000000];
string card[10], res, save2, save5[10000000];
void solve(int a){
	roop--;
	for (int i = 0; i<n; i++){
		if(i==a) continue;
		if (save3 != i){
			if (roop == 0){
				save2 = res;
				res += card[i];
				num = stoi(res);
				if (save[num] == 0){
					ans++;
				}
				save[num] = 1;
				save[roop2] = num;
				roop2++;
				res = save2;
			}
			else{
				save5[i]=res;
				res += card[i];
				solve(i);
				roop++;
				res = save5[i];
			}
		}
	}
}
int main() {
	while (true){
		cin >> n >> k;
		roop2 = 0;
		if (n == 0 && k == 0){
			return 0;
		}
		for (int i = 0; i < n; i++){
			cin >> card[i];
		}
		for (int i = 0; i < n; i++){
			roop = k - 1;
			res = card[i];
			save3 = i;
			solve(i);
		}
		cout << ans << endl;
		for (int i = 0; i < 10000000; i++){
			save[i] = 0;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF (1<<29)//INF
#define F first
#define S second
#define VS vector<string>
#define VI vector<int>
#define REPI(i,s,n) for(int i=s;i<n;i++)//sからn未満の間iを回す
#define REPJ(j,s,n) for(int j=s;j<n;j++)//sからn未満の間iを回す
#define RREPI(i,s,n) for(int i=s;i>=n;i--)//sからn以下の間iを回す
#define RREPJ(j,s,n) for(int j=s;j>=n;j--)//sからn以下の間jを回す
#define ALL(a) a.begin(),a.end()//ソートとかに使える sort(ALL(vec))のように
#define SET(a,c) memset(a,c,sizeof(a));//aをc（0or1）で初期化する
#define DEBUGONE(x) cout<<#x<<" : "<<x<<endl;//デバックで一つの変数を表示
#define DEBUGTWO(x,y) cout<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl;//デバックで二つの変数を表示
#define DEBUGTHREE(x,y,z) cout<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl;//デバックで三つの変数を表示
#define OUTONE(x) cout<<x<<endl;//一つの変数を表示
#define OUTTWO(x,y) cout<<x<<" "<<y<<endl;//二つの変数を表示
#define OUTTHREE(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;//三つの変数を表示
typedef long long int64;
typedef pair<string,int> Psi;
typedef pair<int,int> Pii;
map<Psi,int64> mem;
string num[15];
int n,k;
int64 ans=0;
int64 bitdfs(int now,string nows,int bits);
main(){
  while(cin>>n>>k,n||k){
    ans=0;
    mem.clear();
    REPI(i,0,n){
      cin>>num[i];
    }
    cout<<bitdfs(0,"",0)<<endl;
  }
}
int64 bitdfs(int now,string nows,int bits){
  if(now>=k){
    if(mem[Psi(nows,now)]==0){
      mem[Psi(nows,now)]=-1;
      return 1;
    }
    else{
      return 0;
    }
  }
  else{
    int64 cnt=0;
    int bitst=1;
    REPI(i,0,n){
      int b=bitst&bits;
      if(b==0){
	cnt+=bitdfs(now+1,nows+num[i],bits|bitst);
      }
      bitst=bitst<<1;
    }
    return mem[Psi(nows,now)]=cnt;
  }
}

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

int main(){
  int n, k;
  while(cin >> n >> k && (n|k)){
    string card[10];
    string t;
    for(int i = 0 ; i < n ; i++)  cin >> card[i];
    
    sort(card, card+n);

    set<string> res;
    
    do{
      string s = "";
      for(int i = 0 ; i < k ; i++){
        s += card[i];
      }
      res.insert(s);
    }while(next_permutation(card, card+n));
      
    cout << res.size() << endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int n,k,a[10],power[5];
vector<string>vec;
int main(){
	while(true){
		cin>>n>>k;vec.clear();power[0]=1;for(int i=1;i<5;i++)power[i]=power[i-1]*n;
		if(n==0 && k==0)break;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<power[k];i++){
			int bit[4];for(int j=0;j<k;j++)bit[j]=(i/power[j])%n;
			bool OK=true;
			if(k==2 && (bit[0]==bit[1]))OK=false;
			if(k==3 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==4 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==3 && (bit[0]==bit[3] || bit[1]==bit[3] || bit[2]==bit[3]))OK=false;
			if(OK==true){
				string S="";
				for(int j=0;j<k;j++)S+=to_string(a[bit[j]]);
				vec.push_back(S);
			}
		}
		sort(S.begin(),S.end());int cnt=1;
		for(int i=1;i<S.size();i++){
			if(S[i-1]!=S[i])cnt++;
		}
		cout<<cnt<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF (1<<29)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0};

typedef long long int ll;

using namespace std;

bool c[100000000];
vector<string> card;
set<int> used;
int n,k;

void saiki(int t,string s){
    if(t == k){
	c[stoi(s)] = true;
    }
    REP(i,n){
	if(used.find(i) == used.end()){
	    used.insert(i);
	    saiki(t+1,s+card[i]);
	    used.erase(i);
	}
    }
}

int main(){
    while(true){
	cin >> n >> k;
	if(n==0&&k==0) break;
	card = vector<string>(n);
	memset(c,false,sizeof(c));
	REP(i,n){
	    cin >> card[i];
	}
	saiki(0,"");
	int ans = 0;
	REP(i,100000000){
	    if(c[i]) ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #import<iostream>
#import<set>
#import<algorithm>
using namespace std;main(){int n,k,c[10],i,t;for(;cin>>n>>k,i=n;){for(;i--;)cin>>c[i];sort(c,c+n);set<int>s;do{t=0;for(i=k;i--;)t=t*(c[i]>9?100:10)+c[i];s.insert(t);}while(next_permutation(c,c+n));cout<<s.size()<<endl;}}