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 <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>

using namespace std;
int n, m;
vector<int> cards;
const int INF = 1 << 29;
typedef long long LL;

set<LL> nums;

vector<bool> used;

//a それ数を追加 b使っていい個数 cそれまでの数
void dfs(int a, int b, LL c)
{
	c += cards[a];
	if(b == 0)
	{
		nums.insert(c);
		return;
	}

	for (int i = 0; i < n; i++)
	{
		if(!used[i])
		{
			used[i] = true;
			dfs(i, b - 1, c * (cards[i] < 10 ? 10 : 100));
			used[i] = false;
		}
	}
}

int main(void)
{
	while(cin >> n >> m && n && m)
	{
		cards.resize(n);
		for (int i = 0; i < n; i++)
		{
			cin >> cards[i];
		}
		nums.clear();
		used.resize(n);
		for (int i = 0; i < n; i++)
		{
			used[i] = false;
		}
		for (int i = 0; i < n; i++)
		{
			used[i] = true;
			dfs(i, m - 1, 0);
			used[i] = false;
		}
		cout << nums.size() << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<sstream>
using namespace std;
bool memo[100000000];
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[128];
        queue<P> Q;
        memset(memo,0,sizeof(memo));
        for(int i = 0; i < n; i++){
            bool x[10]={0};
            x[i] = 1;
            scanf("%d",&card[i]);
            Q.push(P(card[i],1,x));
        }
        int ans = 0;
        while( Q.size() ){
            P p = Q.front();Q.pop();
            if(p.cnt == m){
                if( memo[p.num] )continue;
                ans++;
                memo[p.num] = true;
            }
            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;
}

C++: #include<bits/stdc++.h>
using namespace std;
set<string> st;
int n,k;
int flag[10];
string v[10];
void dfs(int maisu,string s){
  if(maisu==k){
    st.insert(s);
    return;
  }
  for(int i=0;i<n;i++){
    if(flag[i]==0){
      flag[i]=1;
      dfs(maisu+1,s+v[i]);
      flag[i]=0;
    }
  }
  return;
}
  
int main(){
  while(1){
    cin>>n>>k;
    if(n==0 && k==0) break;
    for(int i=0;i<n;i++){
      cin>>v[i];
      flag[i]=0;
    }
    
    st.clear();
    dfs(0,"");
    cout<<st.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[20000000];
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<20000000;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<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;

map<string,bool> m;
int n,k;
string a[20];

bool f[20];
void dfs(string str,int dep){
    if(dep == k){
        m[str] = true;
        return;
    }
    for(int i = 0;i < n;i++){
        if(f[i] == false&&(dep!=0||a[i][0]!='0')){
            f[i] = true;
            dfs(str + a[i],dep+1);
            f[i] = false;
        }
    }
}
int main(void){

    while(cin>>n>>k,k){
        for(int i = 0;i < n;i++){
            cin>>a[i];
        }
        dfs("",0);
        int cnt = 0;
        map<string,bool>::iterator it = m.begin();
        while( it != m.end() )
        {
            //cout << (*it).first << ":" << (*it).second << endl;
            ++it;
            cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define LL long long
static const LL INF = 1<<50;

using namespace std;

int n,k,ans;
LL s[9000000];
string num[11];
void cal(string sum,int depth,int bit){
    int res=0;
    if(depth==k){
        int num;
        num=atoi(sum.data());
        s[num]=num;
        //cout<<s[num]<<endl;
    }
    for(int i=1;i<=n;++i){
        int b=1<<i;
        if(bit&b){
            int k=bit-b;
            cal(sum+num[i],depth+1,k);
        }
    }
}

int main(){
    while(1){
        for(int i=0;i<11;++i)num[i]='0';
        memset(s,0,sizeof(s));
        int p=1;
        cin>>n>>k;
        if(n==0&&k==0)break;
        string a;
        for(int i=1;i<=n;++i){
            p+=pow(2,i);
        }
        for(int i=1;i<=n;++i){
            cin>>num[i];
        }
        cal(a,0,p);
        sort(s,s+114514);
        for(int i=0;i<114514;++i){
            if(s[i]&&s[i]!=s[i+1]){
                ans++;
            }
        }
        cout<<ans<<endl;
        ans=0;
    }
}

C++: //54
#include<iostream>
#include<string>
#include<set>

using namespace std;

string c[10];
set<string> st;

void calc(string s,int n,int d,int u){
  if(d==0){
    st.insert(s);
  }else{
    for(int i=0;i<n;i++){
      if(!(u&1<<i)){
	calc(s+c[i],n,d-1,u|1<<i);
      }
    }
  }
}

int main(){
  for(int n,k;cin>>n>>k,n|k;){
    st.clear();
    for(int i=0;i<n;i++){
      cin>>c[i];
    }
    calc("",n,k,0);
    cout<<st.size()<<endl;
  }
  return 0;
}