#include<iostream>
using namespace std;
int n, k, a[100], permCount;
void tipar() //afisarea unei solutii
{
int i;
for(i=0; i<k; i++) //vom avea k nivele
cout<<a[i]<<" ";
cout<<"\n";
}
bool valid(int k)
{
/*
int i;
for(i=0; i<k; i++)
if(a[i]==a[k]) // elementele unui aranjament sunt diferite
return 0;
*/
return 1;
}
void backtracking(int nivel)
{
if(nivel==k)
tipar(), permCount++;
else
{
a[nivel]=0; //initializare nivel
while(a[nivel]<n)
{
a[nivel]++;
if(valid(nivel)) //functie de validare
backtracking(nivel+1); //trecerea la nivelul urmator
}
}
}
int main()
{
cin>>n>>k;
backtracking(0);
cout << endl << permCount;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbiwgaywgYVsxMDBdLCBwZXJtQ291bnQ7Cgp2b2lkIHRpcGFyKCkgLy9hZmlzYXJlYSB1bmVpIHNvbHV0aWkKewogICAgaW50IGk7CiAgICBmb3IoaT0wOyBpPGs7IGkrKykgLy92b20gYXZlYSBrIG5pdmVsZQogICAgICAgIGNvdXQ8PGFbaV08PCIgIjsKICAgIGNvdXQ8PCJcbiI7Cn0KCmJvb2wgdmFsaWQoaW50IGspCnsKCS8qCiAgICBpbnQgaTsKICAgIGZvcihpPTA7IGk8azsgaSsrKQogICAgICAgIGlmKGFbaV09PWFba10pIC8vIGVsZW1lbnRlbGUgdW51aSBhcmFuamFtZW50IHN1bnQgZGlmZXJpdGUKICAgICAgICAgICByZXR1cm4gMDsKCSovCiAgICByZXR1cm4gMTsKfQoKdm9pZCBiYWNrdHJhY2tpbmcoaW50IG5pdmVsKQp7CiAgICBpZihuaXZlbD09aykKICAgICAgICB0aXBhcigpLCBwZXJtQ291bnQrKzsKICAgIGVsc2UKICAgIHsKICAgICAgICBhW25pdmVsXT0wOyAvL2luaXRpYWxpemFyZSBuaXZlbAogICAgICAgIHdoaWxlKGFbbml2ZWxdPG4pCiAgICAgICAgewogICAgICAgICAgICBhW25pdmVsXSsrOwogICAgICAgICAgICBpZih2YWxpZChuaXZlbCkpIC8vZnVuY3RpZSBkZSB2YWxpZGFyZQogICAgICAgICAgICAgICAgYmFja3RyYWNraW5nKG5pdmVsKzEpOyAvL3RyZWNlcmVhIGxhIG5pdmVsdWwgdXJtYXRvcgogICAgICAgIH0KICAgIH0KfQoKCmludCBtYWluKCkKewoJIGNpbj4+bj4+azsKICAgIGJhY2t0cmFja2luZygwKTsKICAgIGNvdXQgPDwgZW5kbCA8PCBwZXJtQ291bnQ7CiAgICByZXR1cm4gMDsKfQoK