Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints. A color hint goes like that: a player names some color and points at all the cards of this color. Similarly goes the value hint. A player names some value and points at all the cards that contain the value.Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.

The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.

Print a single integer — the minimum number of hints that the other players should make.

NoteIn the first sample Borya already knows for each card that it is a green three.In the second sample we can show all fours and all red cards.In the third sample you need to make hints about any four colors.

#include <stdio.h>
#include <stdlib.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))

const int color[] = {['R'] = 0, ['G'] = 1, ['B'] = 2, ['Y'] = 3, ['W'] = 4};
int combiA[4], combiB[4], table[5][5], min = 10;

int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

void hoge(int maxA, int maxB) {
    int i, j;
    int flag[5][5] = {0}, ary[25], size;

    for (i = 0; i < maxA; i++)
        for (j = 0; j < 5; j++)
            flag[combiA[i]][j] |= 1 << combiA[i];

    for (j = 0; j < maxB; j++)
        for (i = 0; i < 5; i++)
            flag[i][combiB[j]] |= 1 << combiB[j] + 5;

    size = 0;
    for (i = 0; i < 5; i++)
        for (j = 0; j < 5; j++)
            if (table[i][j])
                ary[size++] = flag[i][j];

    qsort(ary, size, sizeof(int), &compare);

    for (i = 1; i < size && ary[i] != ary[i - 1]; i++) ;
    if (i == size)
        min = min(min, maxA + maxB);
}

void makeCombiB(int d, int maxA, int maxB) {
    int i;

    if (d == maxB) {
        hoge(maxA, maxB);
        return;
    }

    for (i = d == 0 ? 0 : combiB[d - 1] + 1; i < 5; i++) {
        combiB[d] = i;
        makeCombiB(d + 1, maxA, maxB);
    }
}

void makeCombiA(int d, int maxA) {
    int i;

    if (d == maxA) {
        for (i = 0; i < 5; i++)
            makeCombiB(0, maxA, i);
        return;
    }

    for (i = d == 0 ? 0 : combiA[d - 1] + 1; i < 5; i++) {
        combiA[d] = i;
        makeCombiA(d + 1, maxA);
    }
}

int main(void) {
    int i;
    int n;

    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        char ch1, ch2;

        scanf(" %c%c", &ch1, &ch2);

        table[color[ch1]][ch2 - '1'] = 1;
    }

    for (i = 0; i < 5; i++)
        makeCombiA(0, i);

    printf("%d\n", min);

    return 0;
}

#include <stdio.h>
#include <memory.h>

int flag[5][5];
int flag2[5][5];
int mark[10];

int
dfs(int depth){
    int i,j,count,index;
    if(depth ==0){
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                flag2[i][j] = flag[i][j];
            }
        }

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(mark[i] && mark[j+5]){
                    flag2[i][j]=0;
                }
            }
        }
        for(i=0 ;i<10 ;i++){
            if(! mark[i] )
                continue;
            if ( i < 5){
                count=0;
                index=0;
                for(j=0;j<5;j++){
                    if(flag2[i][j]){
                        count ++;
                        index = j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[i][index]=0;
            }
            else {
                count=0;
                index =0;
                for(j=0;j<5;j++){
                    if(flag2[j][i-5]){
                        count ++;
                        index =j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[index][i-5]=0;
            }
        }
        count =0 ;
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(flag2[i][j]){
                    count++;
                }
            }
        }
        if(count ==1)
            return 1;
        else
            return 0;
    }
    for(i=0;i<10;i++){
        if(mark[i])
            continue;
        mark[i] = 1;
        if(!dfs(depth-1))
            mark[i] =0;
        else
            return 1;
    }
    return 0;
}

int
main(int argc,
     char *argv[]){
    int n;
    scanf("%d",&n) ;
    char card[10];
    char color[5] ={'R','G','B','Y','W'};
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++){
        scanf("%s",card);
        for(j=0;j<5;j++){
            if(card[0] == color [j]){
                flag[j][card[1]-'1'] ++;
            }
        }
    }
    for(i=0;i<=10;i++){
        memset(mark,0,sizeof(mark));
        if(dfs(i)) {
            printf("%d\n",i);
            break;
        }
    }

    return 0;
}

#include<stdio.h>
int surity[1050][200][2],set_bit[15],no[1050][15][15],unhinted[200],aiv[1050];
int func(int n,int *a,int bit)
{
    int i;
 for(i=0;i<n;i++)
 {
    if((set_bit[a[i]/5+5]&bit)!=0)surity[bit][i][0]=1;
    if((set_bit[a[i]%5]&bit)!=0)surity[bit][i][1]=1;
 }
 int no_of_elem=0;
for(i=0;i<n;i++)
{
    if(surity[bit][i][0]==1&&surity[bit][i][1]==1)continue;
    else if(surity[bit][i][0]==1)no[bit][a[i]/5+5][a[i]%5]=1;
    else if(surity[bit][i][1]==1)no[bit][a[i]%5][a[i]/5+5]=1;
    else unhinted[no_of_elem++]=a[i];
}
for(i=0;i<=9;i++){int nos=0,j;for(j=0;j<=9;j++){if(no[bit][i][j])nos++;}if(nos>1)return 0;}
int no=unhinted[0];
for(i=0;i<no_of_elem;i++){if(unhinted[i]!=no)return 0;}return 1;
}
int no_of_bit(int bit)
{
int i=1,no=0;
while(i<=512){if((i&bit)!=0)no++;i*=2;}return no;
}
main()
{
int n,i;scanf("%d",&n);int a[n];set_bit[0]=1;for(i=1;i<=12;i++)set_bit[i]=set_bit[i-1]*2;char s[3];
for(i=0;i<n;i++)
{
    scanf("%s",s);
    switch(s[0])
    {
    case 'R': a[i]=0;break;
    case 'G': a[i]=5;break;
    case 'Y': a[i]=10;break;
    case 'B': a[i]=15;break;
    case 'W': a[i]=20;break;
    }
    a[i]+=s[1]-'0'-1;
}
for(i=0;i<=1023;i++){if(func(n,a,i))aiv[i]=1;}int min=50;
for(i=0;i<=1023;i++)
{
    int no=no_of_bit(i);
if(aiv[i]==1){if(no<min){min=no;}}
}
printf("%d",min);return 0;
}

/* practice with Dukkha */
#include <stdio.h>
#include <string.h>

#define N	100
#define A	(1 << 10)

int count(int n) {
	return n == 0 ? 0 : count(n & n - 1) + 1;
}

int main() {
	static int aa[N];
	static char used[A];
	int n, i, k, l, a, b, c, ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		static char s[2];
		char c, d;

		scanf("%s", s);
		c = s[0];
		d = s[1];
		a = 0;
		if (c == 'R')
			a |= 1 << 0;
		else if (c == 'G')
			a |= 1 << 1;
		else if (c == 'B')
			a |= 1 << 2;
		else if (c == 'Y')
			a |= 1 << 3;
		else
			a |= 1 << 4;
		a |= 1 << 5 + d - '1';
		aa[i] = a;
	}
	for (i = 0; i < n; i++)
		used[aa[i]] = 1;
	k = 0;
	for (a = 0; a < A; a++)
		if (used[a])
			k++;
	ans = 10;
	for (b = 0; b < A; b++) {
		c = count(b);
		if (ans > c) {
			memset(used, 0, sizeof used);
			for (i = 0; i < n; i++) {
				a = aa[i] & b;
				used[a] = 1;
			}
			l = 0;
			for (a = 0; a < A; a++)
				if (used[a])
					l++;
			if (k == l)
				ans = c;
		}
	}
	printf("%d\n", ans);
	return 0;
}

#include <stdio.h>

int number(char c, int i)
{ // номер карты [0, 24]
    switch (c)
    {
        case 'R': return i - 1;
        case 'G': return 5 + i - 1;
        case 'B': return 10 + i - 1;
        case 'Y': return 15 + i - 1;
        case 'W': return 20 + i - 1;
    }
    return 25;
}

int color(int number)
{
    return number / 5;
}

int value(int number)
{
    return number % 5;
}

int check_color(int number, int mask)
{
    return (1 << (5 + color(number))) & mask;
}

int check_value(int number, int mask)
{
    return (1 << value(number)) & mask;
}

int count(int mask)
{
    int res = 0;
    while (mask)
    {
        res += mask % 2;
        mask /= 2;
    }
    return res;
}

int main()
{
    int cards[25] = {0};
    int n, ans, i, j, mask, flag;
    char s[3];
    char c;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%s", s);
        c = s[0];
        j = s[1] - '0';
        cards[number(c, j)] = 1;
    }

    ans = 10;
    for(mask = 0; mask < 1023; mask++)
    {
        flag = 1;
        for (i = 0; i < 25; i++)
            if (cards[i])
            {
                for (j = i+1; j < 25; j++)
                    if (cards[j])
                    {
                        if (color(i) != color(j) &&
                                (check_color(i, mask) ||
                                 check_color(j, mask)))
                            continue;
                        if (value(i) != value(j) &&
                                (check_value(i, mask) ||
                                 check_value(j, mask)))
                            continue;

                        flag = 0;
                        break;
                    }
                if (!flag)
                    break;
            }
        if (flag && count(mask) < ans)
            ans = count(mask);
    }

    printf("%d\n", ans);
}

#include <stdio.h>
#include <string.h>

int main(){
  int n;
  scanf("%d\n", &n);
  int cards[100];
  char s[310];
  gets(s);
  int i;
  for(i=0; i<n; i++){
    int b;
    int a = s[3 * i + 1] - '1';
    switch(s[3 * i]){
      case 'R': b = 0; break;
      case 'G': b = 1; break;
      case 'B': b = 2; break;
      case 'Y': b = 3; break;
      case 'W': b = 4; break;
    }
    cards[i] = 5 * b + a;
  }
  int matrix[25];
  for(i=0; i<25; i++) matrix[i] = 0;
  for(i=0; i<n; i++){
    matrix[cards[i]]=1;
  }
  n = 0;
  for(i=0; i<25; i++){
    if(matrix[i] == 1){
      cards[n] = i;
      n++;
    }
  }
  int v, k;
  int res[100];
  int min = 100;
  for(v=0; v<1024; v++){
    for(i=0; i<n; i++) res[i] = 0;
    for(i=0; i<n; i++){
      for(k=0; k<5; k++){
        int a = (v >> k) & 1;
        if(cards[i] / 5 == k && a) res[i] += 1 << k;
      }
      for(k=0; k<5; k++){
        int a = (v >> (k + 5)) & 1;
        if(cards[i] % 5 == k && a) res[i] += 1 << (k + 5);
      }
    }
    int j;
    int test = 0;
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
      if(i!=j && res[i] == res[j]) test = 1;

    if(! test){
      int count = 0;
      int v1 = v;
      while(v1 > 0){
        count += v1 & 1;
        v1 >>= 1;
      }
      if(count < min){
        min = count;
      }
    }
  }
  printf("%d\n", min);
  return 0;
}

#include <stdio.h>
#include <string.h>
int get(char c){
	if(c == 'R') return 5;
	if(c == 'G') return 6;
	if(c == 'B') return 7;
	if(c == 'Y') return 8;
	if(c == 'W') return 9;
	return (c - '1');
}
int b(int n)
{
  int y = 0;
  while (n){
    if(n & 1) y++;
    n >>= 1;
  }
  return y;
}
int main(){
	int i;
	int j;
	int n;
	char s[3];
	int hint[26] = {0};
	int has[5][5] = {0};
	int cnt = 0;
	int mask = 0;
	int ok = 1;
	int ans = 10;
	scanf("%d", &n);
	for(i = 0; i < n; i++){
		scanf("%s", s);
		if(!has[get(s[0]) - 5][get(s[1])]){
			has[get(s[0]) - 5][get(s[1])] = 1;
			hint[cnt] |= 1 << get(s[0]);
			hint[cnt] |= 1 << get(s[1]);
			cnt++;
		}
	}
	for(mask = 0; mask < (1 << 10); mask++){
		ok = 1;
		for(i = 0; i < cnt && ok; i++){
			for(j = i + 1; j < cnt && ok; j++){
				if(mask & (hint[i] ^ hint[j]))
					continue;
				ok = 0;
			}
		}
		if(ok){
			if(ans > b(mask)) ans = b(mask);
		}
	}
	printf("%d", ans);
return 0;
}

#include <stdio.h>
#include <string.h>
int get(char c){
	if(c == 'R') return 5;
	if(c == 'G') return 6;
	if(c == 'B') return 7;
	if(c == 'Y') return 8;
	if(c == 'W') return 9;
	return (c - '1');
}
int b(int n)
{
  int y = 0;
  while (n){
    if(n & 1) y++;
    n >>= 1;
  }
  return y;
}
int main(){
	int i;
	int j;
	int n;
	char s[3];
	int hint[26] = {0};
	int has[5][5] = {0};
	int cnt = 0;
	int mask = 0;
	int ok = 1;
	int ans = 10;
	scanf("%d", &n);
	for(i = 0; i < n; i++){
		scanf("%s", s);
		if(!has[get(s[0]) - 5][get(s[1])]){
			has[get(s[0]) - 5][get(s[1])] = 1;
			hint[cnt] |= 1 << get(s[0]);
			hint[cnt] |= 1 << get(s[1]);
			cnt++;
		}
	}
	for(mask = 0; mask < (1 << 10); mask++){
		ok = 1;
		for(i = 0; i < cnt - 1 && ok; i++){
			for(j = i + 1; j < cnt && ok; j++){
				if(mask & (hint[i] ^ hint[j]))
					continue;
				ok = 0;
			}
		}
		if(ok){
			if(ans > b(mask)) ans = b(mask);
		}
	}
	printf("%d", ans);
return 0;
}

#include <stdio.h>
#include <stdlib.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))

const int color[] = {['R'] = 0, ['G'] = 1, ['B'] = 2, ['Y'] = 3, ['W'] = 4};
int combiA[4], combiB[4], table[5][5], min = 10;

int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

void hoge(int maxA, int maxB) {
    int i, j;
    int flag[5][5] = {0}, ary[25], size;

    for (i = 0; i < maxA; i++)
        for (j = 0; j < 5; j++)
            flag[combiA[i]][j] |= 1 << combiA[i];

    for (j = 0; j < maxB; j++)
        for (i = 0; i < 5; i++)
            flag[i][combiB[j]] |= 1 << combiB[j] + 5;

    size = 0;
    for (i = 0; i < 5; i++)
        for (j = 0; j < 5; j++)
            if (table[i][j])
                ary[size++] = flag[i][j];

    qsort(ary, size, sizeof(int), &compare);

    for (i = 1; i < size && ary[i] != ary[i - 1]; i++) ;
    if (i == size)
        min = min(min, maxA + maxB);
}

void makeCombiB(int d, int maxA, int maxB) {
    int i;

    if (d == maxB) {
        hoge(maxA, maxB);
        return;
    }

    for (i = d == 0 ? 0 : combiB[d - 1] + 1; i < 5; i++) {
        combiB[d] = i;
        makeCombiB(d + 1, maxA, maxB);
    }
}

void makeCombiA(int d, int maxA) {
    int i;

    if (d == maxA) {
        for (i = 0; i < 4; i++)
            makeCombiB(0, maxA, i);
        return;
    }

    for (i = d == 0 ? 0 : combiA[d - 1] + 1; i < 5; i++) {
        combiA[d] = i;
        makeCombiA(d + 1, maxA);
    }
}

int main(void) {
    int i;
    int n;

    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        char ch1, ch2;

        scanf(" %c%c", &ch1, &ch2);

        table[color[ch1]][ch2 - '1'] = 1;
    }

    for (i = 0; i < 5; i++)
        makeCombiA(0, i);

    printf("%d\n", min);

    return 0;
}

#include <stdio.h>
#include <memory.h>

int flag[5][5];
int flag2[5][5];
int mark[10];

int
dfs(int depth){
    int i,j,count,index;
    if(depth ==0){
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                flag2[i][j] = flag[i][j];
            }
        }

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(mark[i] && mark[j+5]){
                    flag2[i][j]=0;
                }
            }
        }
        for(i=0 ;i<10 ;i++){
            if(! mark[i] )
                continue;
            if ( i < 5){
                count=0;
                index=0;
                for(j=0;j<5;j++){
                    if(flag2[i][j]){
                        count ++;
                        index = j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[i][index]=0;
            }
            else {
                count=0;
                index =0;
                for(j=0;j<5;j++){
                    if(flag2[j][i-5]){
                        count ++;
                        index =j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[index][i-5]=0;
            }
        }
        count =0 ;
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(flag2[i][j]){
                    count++;
                }
            }
        }
        if(count ==1)
            return 1;
        else
            return 0;
    }
    for(i=0;i<10;i++){
        if(mark[i])
            continue;
        mark[i] = 1;
        if(!dfs(depth-1))
            mark[i] =0;
        else
            return 1;
    }
    return 0;
}

int
main(int argc,
     char *argv[]){
    int n;
    scanf("%d",&n) ;
    char card[10];
    char color[5] ={'R','G','B','Y','W'};
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++){
        scanf("%s",card);
        for(j=0;j<5;j++){
            if(card[0] == color [j]){
                flag[j][card[1]-'1'] ++;
            }
        }
    }
    if(n!=2) {
        printf("n = %d\n",n);
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                printf("%d ", flag[i][j]);
            }
            printf("\n");
        }
        return 0;
    }
    for(i=0;i<=10;i++){
        memset(mark,0,sizeof(mark));
        if(dfs(i)) {
            printf("%d\n",i);
            break;
        }
    }

    return 0;
}

#include <stdio.h>
#include <memory.h>

int flag[5][5];
int flag2[5][5];
int mark[10];
int ans ;

int
dfs(int depth){
    int i,j,count,index;
    if(depth ==0){
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                flag2[i][j] = flag[i][j];
            }
        }

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(mark[i] && mark[j+5]){
                    flag2[i][j]=0;
                }
            }
        }
        for(i=0 ;i<10 ;i++){
            if(! mark[i] )
                continue;
            if ( i < 5){
                count=0;
                index=0;
                for(j=0;j<5;j++){
                    if(flag2[i][j]){
                        count ++;
                        index = j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[i][index]=0;
            }
            else {
                count=0;
                index =0;
                for(j=0;j<5;j++){
                    if(flag2[j][i-5]){
                        count ++;
                        index =j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[index][i-5]=0;
            }
        }
        count =0 ;
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(flag2[i][j]){
                    count++;
                }
            }
        }
        if(count ==1)
            return 1;
        else
            return 0;
    }
    for(i=0;i<10;i++){
        if(mark[i])
            continue;
        mark[i] = 1;
        if(!dfs(depth-1))
            mark[i] =0;
        else
            return 1;
    }
    return 0;
}

int
main(int argc,
     char *argv[]){
    int n;
    scanf("%d",&n);
    char card[2];
    char color[5] ={'R','G','B','Y','W'};
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++){
        scanf("%s",card);
        for(j=0;j<5;j++){
            if(card[0] == color [j]){
                flag[j][card[1]-'1'] ++;
            }
        }
    }
    if(n==4) {
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                printf("%d ", flag[i][j]);
            }
            printf("\n");
        }
    }
    for(i=0;i<=10;i++){
        memset(mark,0,sizeof(mark));
        if(dfs(i)) {
            printf("%d\n",i);
            break;
        }
    }

    return 0;
}

#include <stdio.h>
#include <memory.h>

int flag[5][5];
int flag2[5][5];
int mark[10];
int ans ;

int
dfs(int depth){
    int i,j,count,index;
    if(depth ==0){
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                flag2[i][j] = flag[i][j];
            }
        }

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(mark[i] && mark[j+5]){
                    flag2[i][j]=0;
                }
            }
        }
        for(i=0 ;i<10 ;i++){
            if(! mark[i] )
                continue;
            if ( i < 5){
                count=0;
                index=0;
                for(j=0;j<5;j++){
                    if(flag2[i][j]){
                        count ++;
                        index = j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[i][index]=0;
            }
            else {
                count=0;
                index =0;
                for(j=0;j<5;j++){
                    if(flag2[j][i-5]){
                        count ++;
                        index =j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[index][i-5]=0;
            }
        }
        count =0 ;
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(flag2[i][j]){
                    count++;
                }
            }
        }
        if(count ==1)
            return 1;
        else
            return 0;
    }
    for(i=0;i<10;i++){
        if(mark[i])
            continue;
        mark[i] = 1;
        if(!dfs(depth-1))
            mark[i] =0;
        else
            return 1;
    }
    return 0;
}

int
main(int argc,
     char *argv[]){
    int n;
    scanf("%d",&n) ;
    char card[2];
    char color[5] ={'R','G','B','Y','W'};
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++){
        scanf("%s",card);
        for(j=0;j<5;j++){
            if(card[0] == color [j]){
                flag[j][card[1]-'1'] ++;
            }
        }
    }
    if(n!=2) {
        printf("n = %d\n",n);
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                printf("%d ", flag[i][j]);
            }
            printf("\n");
        }
        return 0;
    }
    for(i=0;i<=10;i++){
        memset(mark,0,sizeof(mark));
        if(dfs(i)) {
            printf("%d\n",i);
            break;
        }
    }

    return 0;
}

#include <stdio.h>
#include <memory.h>

int flag[5][5];
int flag2[5][5];
int mark[10];
int ans ;

int
dfs(int depth){
    int i,j,count,index;
    if(depth ==0){

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                flag2[i][j] = flag[i][j];
            }
        }

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(mark[i] && mark[j+5]){
                    flag2[i][j]=0;
                }
            }
        }
        for(i=0 ;i<10 ;i++){
            if(! mark[i] )
                continue;
            if ( i < 5){
                count=0;
                index=0;
                for(j=0;j<5;j++){
                    if(flag2[i][j]){
                        count ++;
                        index = j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[i][index]=0;
            }
            else {
                count=0;
                index =0;
                for(j=0;j<5;j++){
                    if(flag2[j][i-5]){
                        count ++;
                        index =j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[index][i-5]=0;
            }
        }
        count =0 ;
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(flag2[i][j]){
                    count++;
                }
            }
        }
        if(count ==1)
            return 1;
        else
            return 0;
    }
    for(i=0;i<10;i++){
        if(mark[i])
            continue;
        mark[i] = 1;
        if(!dfs(depth-1))
            mark[i] =0;
        else
            return 1;
    }
    return 0;
}

int
main(int argc,
     char *argv[]){
    int n;
    scanf("%d",&n);
    char card[2];
    char color[5] ={'R','G','B','Y','W'};
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++){
        scanf("%s",card);
        for(j=0;j<5;j++){
            if(card[0] == color [j]){
                flag[j][card[1]-'1'] ++;
            }
        }
    }
    for(i=0;i<=10;i++){
        memset(mark,0,sizeof(mark));
        if(dfs(i)) {
            printf("%d\n",i);
            break;
        }
    }

    return 0;
}

#include <stdio.h>
#include <memory.h>

int flag[5][5];
int flag2[5][5];
int mark[10];
int ans ;

int
dfs(int depth){
    int i,j,count,index;
    if(depth ==0){
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                flag2[i][j] = flag[i][j];
            }
        }

        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(mark[i] && mark[j+5]){
                    flag2[i][j]=0;
                }
            }
        }
        for(i=0 ;i<10 ;i++){
            if(! mark[i] )
                continue;
            if ( i < 5){
                count=0;
                index=0;
                for(j=0;j<5;j++){
                    if(flag2[i][j]){
                        count ++;
                        index = j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[i][index]=0;
            }
            else {
                count=0;
                index =0;
                for(j=0;j<5;j++){
                    if(flag2[j][i-5]){
                        count ++;
                        index =j;
                    }
                }
                if( count >1 ){
                    return 0;
                }
                flag2[index][i-5]=0;
            }
        }
        count =0 ;
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                if(flag2[i][j]){
                    count++;
                }
            }
        }
        if(count ==1)
            return 1;
        else
            return 0;
    }
    for(i=0;i<10;i++){
        if(mark[i])
            continue;
        mark[i] = 1;
        if(!dfs(depth-1))
            mark[i] =0;
        else
            return 1;
    }
    return 0;
}

int
main(int argc,
     char *argv[]){
    int n;
    scanf("%d",&n) ;
    char card[2];
    char color[5] ={'R','G','B','Y','W'};
    int i,j;
    memset(flag,0,sizeof(flag));
    for(i=0;i<n;i++){
        scanf("%s",card);
        for(j=0;j<5;j++){
            if(card[0] == color [j]){
                flag[j][card[1]-'1'] ++;
            }
        }
    }
    if(n==4) {
        for(i=0;i<5;i++){
            for(j=0;j<5;j++){
                printf("%d ", flag[i][j]);
            }
            printf("\n");
        }
        return 0;
    }
    for(i=0;i<=10;i++){
        memset(mark,0,sizeof(mark));
        if(dfs(i)) {
            printf("%d\n",i);
            break;
        }
    }

    return 0;
}

#include<stdio.h>
int surity[1050][200][2],set_bit[15],no[1050][15],unhinted[200],aiv[1050];
int func(int n,int *a,int bit)
{
    int i;
 for(i=0;i<n;i++)
 {
    if((set_bit[a[i]/5+5]&bit)!=0)surity[bit][i][0]=1;
    if((set_bit[a[i]%5]&bit)!=0)surity[bit][i][1]=1;
 }
 int no_of_elem=0;
for(i=0;i<n;i++)
{
    if(surity[bit][i][0]==1&&surity[bit][i][1]==1)continue;
    else if (surity[bit][i][0]==1)no[bit][a[i]/5+5]++;
    else if(surity[bit][i][1]==1)no[bit][a[i]%5]++;
    else unhinted[no_of_elem++]=a[i];
}
for(i=0;i<=9;i++){if(no[bit][i]>1)return 0;}
int no=unhinted[0];
for(i=0;i<no_of_elem;i++){if(unhinted[i]!=no)return 0;}return 1;
}
int no_of_bit(int bit)
{
int i=1,no=0;
while(i<=512){if((i&bit)!=0)no++;i*=2;}return no;
}
main()
{
int n,i;scanf("%d",&n);int a[n];set_bit[0]=1;for(i=1;i<=12;i++)set_bit[i]=set_bit[i-1]*2;char s[3];
for(i=0;i<n;i++)
{
    scanf("%s",s);
    switch(s[0])
    {
    case 'R': a[i]=0;break;
    case 'G': a[i]=5;break;
    case 'Y': a[i]=10;break;
    case 'B': a[i]=15;break;
    case 'W': a[i]=20;break;
    }
    a[i]+=s[1]-'0'-1;
}
for(i=0;i<=1023;i++){if(func(n,a,i))aiv[i]=1;}int min=50;
for(i=0;i<=1023;i++)
{
    int no=no_of_bit(i);
if(aiv[i]==1){if(no<min){min=no;}}
}
printf("%d",min);return 0;
}

#include<stdio.h>
int surity[1050][200][2],set_bit[15],no[1050][15],unhinted[200],aiv[1050];
int func(int n,int *a,int bit)
{
    int i;
 for(i=0;i<n;i++)
 {
    if((set_bit[a[i]/5+5]&bit)!=0)surity[bit][i][0]=1;
    if((set_bit[a[i]%5]&bit)!=0)surity[bit][i][1]=1;
 }
 int no_of_elem=0;
for(i=0;i<n;i++)
{
    if(surity[bit][i][0]==1&&surity[bit][i][1]==1)continue;
    else if (surity[bit][i][0]==1)no[bit][a[i]/5+5]++;
    else if(surity[bit][i][1]==1)no[bit][a[i]%5]++;
    else unhinted[no_of_elem++]=a[i];
}
for(i=0;i<9;i++){if(no[bit][i]>1)return 0;}
int no=unhinted[0];
for(i=0;i<no_of_elem;i++){if(unhinted[i]!=no)return 0;}return 1;
}
int no_of_bit(int bit)
{
int i=1,no=0;
while(i<=512){if((i&bit)!=0)no++;i*=2;}return no;
}
main()
{
int n,i;scanf("%d",&n);int a[n];set_bit[0]=1;for(i=1;i<=12;i++)set_bit[i]=set_bit[i-1]*2;char s[3];
for(i=0;i<n;i++)
{
    scanf("%s",s);
    switch(s[0])
    {
    case 'R': a[i]=0;break;
    case 'G': a[i]=5;break;
    case 'Y': a[i]=10;break;
    case 'B': a[i]=15;break;
    case 'W': a[i]=20;break;
    }
    a[i]+=s[1]-'0'-1;
}
for(i=0;i<=1023;i++){if(func(n,a,i))aiv[i]=1;}int min=50;
for(i=0;i<=1023;i++)
{
    int no=no_of_bit(i);
if(aiv[i]==1){if(no<min)min=no;}
}
printf("%d",min);return 0;
}

#include<stdio.h>
int surity[1050][200][2],set_bit[15],no[1050][15],unhinted[200],aiv[1050];
int func(int n,int *a,int bit)
{
    int i;
 for(i=0;i<n;i++)
 {
    if((set_bit[a[i]/5+5]&bit)!=0)surity[bit][i][0]=1;
    if((set_bit[a[i]%5]&bit)!=0)surity[bit][i][1]=1;
 }
 int no_of_elem=0;
for(i=0;i<n;i++)
{
    if(surity[bit][i][0]==1&&surity[bit][i][1]==1)continue;
    else if (surity[bit][i][0]==1)no[bit][a[i]/5+5]++;
    else if(surity[bit][i][1]==1)no[bit][a[i]%5]++;
    else unhinted[no_of_elem++]=a[i];
}
for(i=0;i<9;i++){if(no[bit][i]>1)return 0;}
int no=unhinted[0];
for(i=0;i<no_of_elem;i++){if(unhinted[i]!=no)return 0;}return 1;
}
int no_of_bit(int bit)
{
int i=1,no=0;
while(i<=512){if((i&bit)!=0)no++;i*=2;}return no;
}
main()
{
int n,i;scanf("%d",&n);int a[n];set_bit[0]=1;for(i=1;i<=12;i++)set_bit[i]=set_bit[i-1]*2;char s[3];
for(i=0;i<n;i++)
{
    scanf("%s",s);
    switch(s[0])
    {
    case 'R': a[i]=0;break;
    case 'G': a[i]=5;break;
    case 'Y': a[i]=10;break;
    case 'B': a[i]=15;break;
    case 'W': a[i]=20;break;
    }
    a[i]+=s[1]-'0';
}
for(i=0;i<=1023;i++){if(func(n,a,i))aiv[i]=1;}int min=50;
for(i=0;i<=1023;i++)
{
    int no=no_of_bit(i);
if(aiv[i]==1){if(no<min)min=no;}
}
printf("%d",min);return 0;
}

#include <stdio.h>
#include <string.h>

int main(){
  int n;
  scanf("%d\n", &n);
  int cards[100];
  char s[310];
  gets(s);
  int i;
  for(i=0; i<n; i++){
    int b;
    int a = s[3 * i + 1] - '0';
    switch(s[3 * i]){
      case 'R': b = 0; break;
      case 'G': b = 1; break;
      case 'B': b = 2; break;
      case 'Y': b = 3; break;
      case 'W': b = 4; break;
    }
    cards[i] = 5 * b + a;
  }
  int matrix[25];
  for(i=0; i<25; i++) matrix[i] = 0;
  for(i=0; i<n; i++){
    matrix[cards[i]]=1;
  }
  n = 0;
  for(i=0; i<25; i++){
    if(matrix[i] == 1){
      cards[n] = i;
      n++;
    }
  }
  int v, k;
  int res[100];
  int min = 100;
  for(v=0; v<1024; v++){
    for(i=0; i<n; i++) res[i] = 0;
    for(i=0; i<n; i++){
      for(k=0; k<5; k++){
        int a = (v >> k) & 1;
        if(cards[i] / 5 == k && a) res[i] += 1 << k;
      }
      for(k=0; k<5; k++){
        int a = (v >> (k + 5)) & 1;
        if(cards[i] % 5 == k && a) res[i] += 1 << (k + 5);
      }
    }
    int j;
    int test = 0;
    for(i=0; i<n; i++)
    for(j=0; j<n; j++)
      if(i!=j && res[i] == res[j]) test = 1;

    if(! test){
      int count = 0;
      int v1 = v;
      while(v1 > 0){
        count += v1 & 1;
        v1 >>= 1;
      }
      if(count < min){
        min = count;
      }
    }
  }
  printf("%d\n", min);
  return 0;
}


A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't.The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares.The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions...

The first line of input contains two integers $$$H$$$ and $$$W$$$ ($$$1 \le H, W \le 5$$$), separated by a space, — the height and the width of the cake. The next $$$H$$$ lines contain a string of $$$W$$$ characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry.

Output the number of berries the mouse will eat following her strategy.

#include<stdio.h>
char mp[9][9];
int dir;
int n,m,x,y;
int get_dis(int dir){
  if(dir==0&&y==m)return 100;
  if(dir==1&&x==n)return 100;
  if(dir==0){
    for(int i=y+1;i<=m;i++)
      if(mp[x][i]=='*')return i-y;
  }else
    for(int i=x+1;i<=n;i++)
      if(mp[i][y]=='*')return i-x;
  return 100;
}

int main(void){
  scanf("%d%d",&n,&m);
  for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
  dir=0;
  int ans=0;
  x=1;y=1;
  for(int i=0;i<n+m;i++){
    if(mp[x][y]=='*')ans++;
    //    printf("%d %d\n",x,y);
    if(get_dis(dir)<get_dis(!dir)){
      if(dir==0)y++;
      else x++;
    }else if(get_dis(dir)>get_dis(!dir)){
      if(dir==0)
	x++;
      else
	y++;
      dir=!dir;
    }else{
      y++;
    }
  }
  printf("%d\n",ans);
  return 0;
}

#include<stdio.h>
char mp[9][9];
int dir;
int n,m,x,y;
int get_dis(int dir){
  if(dir==0&&y==m)return 100;
  if(dir==1&&x==n)return 100;
  if(dir==0){
    for(int i=y+1;i<=m;i++)
      if(mp[x][i]=='*')return i-y;
  }else
    for(int i=x+1;i<=n;i++)
      if(mp[i][y]=='*')return i-x;
  return 100;
}

int main(void){
  scanf("%d%d",&n,&m);
  for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
  dir=0;
  int ans=0;
  x=1;y=1;
  for(int i=0;i<n+m;i++){
    if(mp[x][y]=='*')ans++;
    //    printf("%d %d\n",x,y);
    if(get_dis(dir)<get_dis(!dir)){
      if(dir==0)y++;
      else x++;
    }else if(get_dis(dir)>get_dis(!dir)){
      if(dir==0)
	x++;
      else
	y++;
      dir=!dir;
    }else{
      x++;
    }
  }
  printf("%d\n",ans);
  return 0;
}

#include<stdio.h>
char mp[9][9];
int dir;
int n,m,x,y;
int get_dis(int dir){
  if(dir==0&&y==m)return 100;
  if(dir==1&&x==n)return 100;
  if(dir==0){
    for(int i=y+1;i<=m;i++)
      if(mp[x][i]=='*')return i-y;
  }else
    for(int i=x+1;i<=n;i++)
      if(mp[i][y]=='*')return i-x;
  return 100;
}

int main(void){
  scanf("%d%d",&n,&m);
  for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
  dir=0;
  int ans=0;
  x=1;y=1;
  for(int i=0;i<n+m;i++){
    if(mp[x][y]=='*')ans++;
    //    printf("%d %d\n",x,y);
    if(get_dis(dir)<=get_dis(!dir)){
      if(dir==0)y++;
      else x++;
    }else{
      if(dir==0)x++;
      else y++;
      dir=!dir;
    }
  }
  printf("%d\n",ans);
  return 0;
}


On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.It is well known that one month on this planet consists of $$$n^2$$$ days, so calendars, represented as square matrix $$$n$$$ by $$$n$$$ are extremely popular.Weather conditions are even more unusual. Due to the unique composition of the atmosphere, when interacting with sunlight, every day sky takes one of three colors: blue, green or red.To play the bingo, you need to observe the sky for one month — after each day, its cell is painted with the color of the sky in that day, that is, blue, green or red.At the end of the month, students examine the calendar. If at least one row or column contains only cells of one color, that month is called lucky.Let's call two colorings of calendar different, if at least one cell has different colors in them. It is easy to see that there are $$$3^{n \cdot n}$$$ different colorings. How much of them are lucky? Since this number can be quite large, print it modulo $$$998244353$$$.

The first and only line of input contains a single integer $$$n$$$ ($$$1 \le n \le 1000\,000$$$) — the number of rows and columns in the calendar.

Print one number — number of lucky colorings of the calendar modulo $$$998244353$$$

NoteIn the first sample any coloring is lucky, since the only column contains cells of only one color.In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky:  While these colorings are not lucky:

#include <stdio.h>
#define N 1000005
const int P=998244353;
long long inv[N];
long long ex(long long b, int i) {
	long long p=1;
	for(; i; i>>=1, b=b*b%P) if(i&1) p=p*b%P;
	return p;
}
int main() {
	int n, i, ans, binom, power;
	scanf("%d", &n);
	inv[1]=1;
	for(i=2; i<=n; ++i) inv[i]=inv[P%i]*(P-P/i)%P;
	power=ex(3,n);
	ans=(ex((power+P-1)%P,n)*3-ex((power+P-3)%P,n)*2%P+P-ex(power,n)+P)%P;
	binom=n&1?1:-1;
	power=1;
	for(i=0; i<n; ++i) {
		if(i) {
			binom=(P-binom)*(n-i+1ll)%P*inv[i]%P;
			power=power*3ll%P;
		}
		ans=(ans+binom*ex(power-1, n)*3ll)%P;
	}
	printf("%d\n", ans<0?ans+P:ans);
	return 0;
}


One tradition of welcoming the New Year is launching fireworks into the sky. Usually a launched firework flies vertically upward for some period of time, then explodes, splitting into several parts flying in different directions. Sometimes those parts also explode after some period of time, splitting into even more parts, and so on.Limak, who lives in an infinite grid, has a single firework. The behaviour of the firework is described with a recursion depth n and a duration for each level of recursion t1, t2, ..., tn. Once Limak launches the firework in some cell, the firework starts moving upward. After covering t1 cells (including the starting cell), it explodes and splits into two parts, each moving in the direction changed by 45 degrees (see the pictures below for clarification). So, one part moves in the top-left direction, while the other one moves in the top-right direction. Each part explodes again after covering t2 cells, splitting into two parts moving in directions again changed by 45 degrees. The process continues till the n-th level of recursion, when all 2n - 1 existing parts explode and disappear without creating new parts. After a few levels of recursion, it's possible that some parts will be at the same place and at the same time — it is allowed and such parts do not crash.Before launching the firework, Limak must make sure that nobody stands in cells which will be visited at least once by the firework. Can you count the number of those cells?

The first line of the input contains a single integer n (1 ≤ n ≤ 30) — the total depth of the recursion. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 5). On the i-th level each of 2i - 1 parts will cover ti cells before exploding.

Print one integer, denoting the number of cells which will be visited at least once by any part of the firework.

NoteFor the first sample, the drawings below show the situation after each level of recursion. Limak launched the firework from the bottom-most red cell. It covered t1 = 4 cells (marked red), exploded and divided into two parts (their further movement is marked green). All explosions are marked with an 'X' character. On the last drawing, there are 4 red, 4 green, 8 orange and 23 pink cells. So, the total number of visited cells is 4 + 4 + 8 + 23 = 39.  For the second sample, the drawings below show the situation after levels 4, 5 and 6. The middle drawing shows directions of all parts that will move in the next level.

//set many funcs template
//Ver.20190820
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<assert.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384

int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int round(int a,int b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
int ceil(int a,int b){if(a%b==0){return a/b;}return (a/b)+1;}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int nHr(int a,int b){return nCr(a+b-1,b);}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int pow(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
int dsum(int x){int r=0;while(x){r+=(x%10);x/=10;}return r;}
int dsumb(int x,int b){int r=0;while(x){r+=(x%b);x/=b;}return r;}
int sankaku(int x){return ((1+x)*x)/2;}
void swap(int *a,int *b){int c;c=(*a);(*a)=(*b);(*b)=c;}
long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llround(long long a,long long b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
long long llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
long long lllcm(long long a,long long b){long long c=llgcd(a,b);a/=c;return a*b;}
long long llnCr(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
long long llnHr(long long a,long long b){return llnCr(a+b-1,b);}
long long llfact(long long a){long long i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
long long llpow(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
long long lldsum(long long x){long long r=0;while(x){r+=(x%10);x/=10;}return r;}
long long lldsumb(long long x,long long b){long long r=0;while(x){r+=(x%b);x/=b;}return r;}
long long llsankaku(long long x){return ((1+x)*x)/2;}
void llswap(long long *a,long long *b){long long c;c=(*a);(*a)=(*b);(*b)=c;}
double dbmax(double a,double b){if(a>b){return a;}return b;}
double dbmin(double a,double b){if(a<b){return a;}return b;}
double dbzt(double a,double b){return dbmax(a,b)-dbmin(a,b);}
void dbswap(double *a,double *b){double c;c=(*a);(*a)=(*b);(*b)=c;}
void chswap(char *a,char *b){char c;c=(*a);(*a)=(*b);(*b)=c;}
int sortfncsj(const void *a,const void *b){if(*(int *)a>*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int sortfnckj(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int llsortfnckj(const void *a,const void *b){if(*(long long *)a<*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int dbsortfncsj(const void *a,const void *b){if(*(double *)a>*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int dbsortfnckj(const void *a,const void *b){if(*(double *)a<*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int strsortfncsj(const void *a,const void *b){return strcmp((char *)a,(char *)b);}
int strsortfnckj(const void *a,const void *b){return strcmp((char *)b,(char *)a);}
int chsortfncsj(const void *a,const void *b){if(*(char *)a>*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}
int chsortfnckj(const void *a,const void *b){if(*(char *)a<*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}

void shuffledget(int x[],int n){
    int i,b[524288],p,c;
    for(i=0;i<n;i++){
        b[i]=i;
    }
    for(i=n;i>=1;i--){
        p=rand()%i;
        c=b[i-1];b[i-1]=b[p];b[p]=c;
    }
    for(i=0;i<n;i++){
        scanf("%d",&x[b[i]]);
    }
}

int dx4[4]={1,-1,0,0};
int dy4[4]={0,0,1,-1};
int dx8[8]={-1,-1,-1,0,0,1,1,1};
int dy8[8]={-1,0,1,-1,1,-1,0,1};

int search(int x,int a[],int n){
    int st=0,fi=n-1,te;
    while(st<=fi){
        te=(st+fi)/2;
        if(a[te]<x){st=te+1;}else{fi=te-1;}
    }
    return st;
}

void prarr(int arr[],int n){
  int i;
  for(i=0;i<n;i++){
    if(i){printf(" ");}
    printf("%d",arr[i]);
  }
  printf("\n");
  return;
}

void getperm(int a[],int n){
  int i,p;
  for(i=0;i<n;i++){
    a[i]=i;
  }
  for(i=n-1;i>=1;i--){
    p=rand()%(i+1);
    swap(&a[p],&a[i]);
  }
}

typedef struct{
int val;
int node;
}sd;

int sdsortfnc(const void *a,const void *b){
if(((sd*)a)->val < ((sd*)b)->val){return -1;}
if(((sd*)a)->val > ((sd*)b)->val){return 1;}
return 0;
}

void coordinate_comp(int a[],int n){
  int i,c=0;
  sd dat[524288];
  for(i=0;i<n;i++){
    dat[i].val=a[i];
    dat[i].node=i;
  }
  qsort(dat,n,sizeof(dat[0]),sdsortfnc);
  a[dat[0].node]=c;
  for(i=1;i<n;i++){
    if(dat[i-1].val!=dat[i].val){c++;}
    a[dat[i].node]=c;
  }
}

int n,a[30],res=0;
int mem[400][400]={0};
bool fl[400][400][32][8]={0};

int rotx[8]={1,1,0,-1,-1,-1,0,1};
int roty[8]={0,1,1,1,0,-1,-1,-1};

void solve(int step,int x,int y,int key){
  //printf("%d : (%d , %d) + %d\n",step,x,y,key);
  if(step==n){return;}
  int i,l=a[step];
  if(fl[x][y][step][key]!=0){return;}
  fl[x][y][step][key]=1;
  for(i=0;i<l;i++){
    x+=rotx[key];
    y+=roty[key];
    mem[x][y]=1;
  }
  solve(step+1,x,y,(key+1)&7);
  solve(step+1,x,y,(key+7)&7);
}

int main(void){
  int i,j;
  scanf("%d",&n);
  for(i=0;i<n;i++){scanf("%d",&a[i]);}
  solve(0,200,200,0);
  for(i=0;i<400;i++){
    for(j=0;j<400;j++){
      //printf("%d",mem[i][j]);
      if(mem[i][j]==1){res++;}
    }//puts("");
  }
  printf("%d\n",res);
  return 0;
}

#include <stdio.h>
#include <stdlib.h>

void pathFinder(int arr[],int hash[500][500],int last[500][500][32][8],int index,int n,int *count,int flag,int x,int y)
{
	if(index == n)
	{
		return;
	}

	int i,j,k;

	int temp1,temp2;

	last[x][y][index][flag] = 1;

	for(i=0;i<arr[index];i++)
	{
		if(hash[x][y] == 0)
		{
			hash[x][y] = 1;
			(*count)++;
		}
		if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}
	}

	temp1 = (flag + 1) % 8;
	temp2 = (flag + 7) % 8;

		if(flag == 0)
		{
			y--;
		}
		else if(flag == 1)
		{
			x--;
			y--;
		}
		else if(flag == 2)
		{
			x--;
		}
		else if(flag == 3)
		{
			x--;
			y++;
		}
		else if(flag == 4)
		{
			y++;
		}
		else if(flag == 5)
		{
			x++;
			y++;
		}
		else if(flag == 6)
		{
			x++;
		}
		else if(flag == 7)
		{
			x++;
			y--;
		}
	int p=x,q=y;
	flag=temp1;
	if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}

	if(last[x][y][index+1][flag] == 0)
	pathFinder(arr,hash,last,index+1,n,count,temp1,x,y);

	x=p;y=q;
	flag=temp2;
	if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}

	if(last[x][y][index+1][flag] == 0)
	pathFinder(arr,hash,last,index+1,n,count,temp2,x,y);

}

int main()
{
	int i,j,k,n;
	scanf("%d",&n);

	int arr[n];
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
	}

	int hash[500][500] = {0};

	int last[500][500][32][8]={0};

	k = 0;

	//hash[500][500] = 1;

	pathFinder(arr,hash,last,0,n,&k,0,250,250);

	printf("%d\n",k);

	return 0;
}

//set many funcs template
//Ver.20190820
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<assert.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384

int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int round(int a,int b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
int ceil(int a,int b){if(a%b==0){return a/b;}return (a/b)+1;}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int nHr(int a,int b){return nCr(a+b-1,b);}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int pow(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
int dsum(int x){int r=0;while(x){r+=(x%10);x/=10;}return r;}
int dsumb(int x,int b){int r=0;while(x){r+=(x%b);x/=b;}return r;}
int sankaku(int x){return ((1+x)*x)/2;}
void swap(int *a,int *b){int c;c=(*a);(*a)=(*b);(*b)=c;}
long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llround(long long a,long long b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
long long llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
long long lllcm(long long a,long long b){long long c=llgcd(a,b);a/=c;return a*b;}
long long llnCr(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
long long llnHr(long long a,long long b){return llnCr(a+b-1,b);}
long long llfact(long long a){long long i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
long long llpow(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
long long lldsum(long long x){long long r=0;while(x){r+=(x%10);x/=10;}return r;}
long long lldsumb(long long x,long long b){long long r=0;while(x){r+=(x%b);x/=b;}return r;}
long long llsankaku(long long x){return ((1+x)*x)/2;}
void llswap(long long *a,long long *b){long long c;c=(*a);(*a)=(*b);(*b)=c;}
double dbmax(double a,double b){if(a>b){return a;}return b;}
double dbmin(double a,double b){if(a<b){return a;}return b;}
double dbzt(double a,double b){return dbmax(a,b)-dbmin(a,b);}
void dbswap(double *a,double *b){double c;c=(*a);(*a)=(*b);(*b)=c;}
void chswap(char *a,char *b){char c;c=(*a);(*a)=(*b);(*b)=c;}
int sortfncsj(const void *a,const void *b){if(*(int *)a>*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int sortfnckj(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int llsortfnckj(const void *a,const void *b){if(*(long long *)a<*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int dbsortfncsj(const void *a,const void *b){if(*(double *)a>*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int dbsortfnckj(const void *a,const void *b){if(*(double *)a<*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int strsortfncsj(const void *a,const void *b){return strcmp((char *)a,(char *)b);}
int strsortfnckj(const void *a,const void *b){return strcmp((char *)b,(char *)a);}
int chsortfncsj(const void *a,const void *b){if(*(char *)a>*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}
int chsortfnckj(const void *a,const void *b){if(*(char *)a<*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}

void shuffledget(int x[],int n){
    int i,b[524288],p,c;
    for(i=0;i<n;i++){
        b[i]=i;
    }
    for(i=n;i>=1;i--){
        p=rand()%i;
        c=b[i-1];b[i-1]=b[p];b[p]=c;
    }
    for(i=0;i<n;i++){
        scanf("%d",&x[b[i]]);
    }
}

int dx4[4]={1,-1,0,0};
int dy4[4]={0,0,1,-1};
int dx8[8]={-1,-1,-1,0,0,1,1,1};
int dy8[8]={-1,0,1,-1,1,-1,0,1};

int search(int x,int a[],int n){
    int st=0,fi=n-1,te;
    while(st<=fi){
        te=(st+fi)/2;
        if(a[te]<x){st=te+1;}else{fi=te-1;}
    }
    return st;
}

void prarr(int arr[],int n){
  int i;
  for(i=0;i<n;i++){
    if(i){printf(" ");}
    printf("%d",arr[i]);
  }
  printf("\n");
  return;
}

void getperm(int a[],int n){
  int i,p;
  for(i=0;i<n;i++){
    a[i]=i;
  }
  for(i=n-1;i>=1;i--){
    p=rand()%(i+1);
    swap(&a[p],&a[i]);
  }
}

typedef struct{
int val;
int node;
}sd;

int sdsortfnc(const void *a,const void *b){
if(((sd*)a)->val < ((sd*)b)->val){return -1;}
if(((sd*)a)->val > ((sd*)b)->val){return 1;}
return 0;
}

void coordinate_comp(int a[],int n){
  int i,c=0;
  sd dat[524288];
  for(i=0;i<n;i++){
    dat[i].val=a[i];
    dat[i].node=i;
  }
  qsort(dat,n,sizeof(dat[0]),sdsortfnc);
  a[dat[0].node]=c;
  for(i=1;i<n;i++){
    if(dat[i-1].val!=dat[i].val){c++;}
    a[dat[i].node]=c;
  }
}

int n,a[30],res=0;
int mem[400][400]={0};
int fl[400][400][8]={0};

int rotx[8]={1,1,0,-1,-1,-1,0,1};
int roty[8]={0,1,1,1,0,-1,-1,-1};

void solve(int step,int x,int y,int key){
  //printf("%d : (%d , %d) + %d\n",step,x,y,key);
  if(step==n){return;}
  int i,l=a[step];
  if(fl[x][y][key]>=l){return;}
  fl[x][y][key]=l;
  for(i=0;i<l;i++){
    x+=rotx[key];
    y+=roty[key];
    mem[x][y]=1;
  }
  solve(step+1,x,y,(key+1)&7);
  solve(step+1,x,y,(key+7)&7);
}

int main(void){
  int i,j;
  scanf("%d",&n);
  for(i=0;i<n;i++){scanf("%d",&a[i]);}
  //mem[200][200]=1;
  solve(0,200,200,0);
  for(i=0;i<400;i++){
    for(j=0;j<400;j++){
      //printf("%d",mem[i][j]);
      if(mem[i][j]==1){res++;}
    }//puts("");
  }
  printf("%d\n",res);
  return 0;
}

#include <stdio.h>
#include <stdlib.h>

void pathFinder(int arr[],int hash[1000][1000],int index,int n,int *count,int flag,int x,int y)
{
	if(index == n)
	{
		return;
	}

	//printf("x = %d y = %d\n",x,y);

	int i,j,k;

	int temp1,temp2;

	for(i=0;i<arr[index] + 1;i++)
	{
		if(hash[x][y] == 0)
		{
			hash[x][y] = 1;
			(*count)++;
		}
		if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}
	}

	temp1 = (flag + 1) % 8;
	temp2 = (flag + 7) % 8;

		if(flag == 0)
		{
			y--;
		}
		else if(flag == 1)
		{
			x--;
			y--;
		}
		else if(flag == 2)
		{
			x--;
		}
		else if(flag == 3)
		{
			x--;
			y++;
		}
		else if(flag == 4)
		{
			y++;
		}
		else if(flag == 5)
		{
			x++;
			y++;
		}
		else if(flag == 6)
		{
			x++;
		}
		else if(flag == 7)
		{
			x++;
			y--;
		}

	pathFinder(arr,hash,index+1,n,count,temp1,x,y);

	pathFinder(arr,hash,index+1,n,count,temp2,x,y);

}

int main()
{
	int i,j,k,n;
	scanf("%d",&n);

	int arr[n];
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
	}

	int hash[1000][1000] = {0};

	k = 0;

	hash[0][0] = 1;

	pathFinder(arr,hash,0,n,&k,0,0,0);

	printf("%d\n",k);

	return 0;
}

#include <stdio.h>
#include <stdlib.h>

void pathFinder(int arr[],int hash[1000][1000],int index,int n,int *count,int flag,int x,int y)
{
	if(index == n)
	{
		return;
	}

	//printf("x = %d y = %d\n",x,y);

	int i,j,k;

	int temp1,temp2;

	for(i=0;i<arr[index] + 1;i++)
	{
		if(hash[x][y] == 0)
		{
			hash[x][y] = 1;
			(*count)++;
		}
		if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}
	}

	temp1 = (flag + 1) % 8;
	temp2 = (flag + 7) % 8;

		if(flag == 0)
		{
			y--;
		}
		else if(flag == 1)
		{
			x--;
			y--;
		}
		else if(flag == 2)
		{
			x--;
		}
		else if(flag == 3)
		{
			x--;
			y++;
		}
		else if(flag == 4)
		{
			y++;
		}
		else if(flag == 5)
		{
			x++;
			y++;
		}
		else if(flag == 6)
		{
			x++;
		}
		else if(flag == 7)
		{
			x++;
			y--;
		}

	pathFinder(arr,hash,index+1,n,count,temp1,x,y);

	pathFinder(arr,hash,index+1,n,count,temp2,x,y);

}

int main()
{
	int i,j,k,n;
	scanf("%d",&n);

	int arr[n];
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
	}

	int hash[1000][1000] = {0};

	k = 0;

	hash[500][500] = 1;

	pathFinder(arr,hash,0,n,&k,0,500,500);

	printf("%d\n",k);

	return 0;
}

#include <stdio.h>
#include <stdlib.h>

void pathFinder(int arr[],int hash[1000][1000],int index,int n,int *count,int flag,int x,int y)
{
	if(index == n)
	{
		return;
	}

	//printf("x = %d y = %d\n",x,y);

	int i,j,k;

	int temp1,temp2;

	for(i=0;i<arr[index];i++)
	{
		if(hash[x][y] == 0)
		{
			hash[x][y] = 1;
			(*count)++;
		}
		if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}
	}

	temp1 = (flag + 1) % 8;
	temp2 = (flag + 7) % 8;

		/*if(flag == 0)
		{
			y--;
		}
		else if(flag == 1)
		{
			x--;
			y--;
		}
		else if(flag == 2)
		{
			x--;
		}
		else if(flag == 3)
		{
			x--;
			y++;
		}
		else if(flag == 4)
		{
			y++;
		}
		else if(flag == 5)
		{
			x++;
			y++;
		}
		else if(flag == 6)
		{
			x++;
		}
		else if(flag == 7)
		{
			x++;
			y--;
		}*/
	int p=x,q=y;
	flag=temp1;
	if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}

	pathFinder(arr,hash,index+1,n,count,temp1,x,y);

	x=p;y=q;
	flag=temp2;
	if(flag == 0)
		{
			y++;
		}
		else if(flag == 1)
		{
			x++;
			y++;
		}
		else if(flag == 2)
		{
			x++;
		}
		else if(flag == 3)
		{
			x++;
			y--;
		}
		else if(flag == 4)
		{
			y--;
		}
		else if(flag == 5)
		{
			x--;
			y--;
		}
		else if(flag == 6)
		{
			x--;
		}
		else if(flag == 7)
		{
			x--;
			y++;
		}

	pathFinder(arr,hash,index+1,n,count,temp2,x,y);

}

int main()
{
	int i,j,k,n;
	scanf("%d",&n);

	int arr[n];
	for(i=0;i<n;i++)
	{
		scanf("%d",&arr[i]);
	}

	int hash[1000][1000] = {0};

	k = 0;

	//hash[500][500] = 1;

	pathFinder(arr,hash,0,n,&k,0,500,500);

	printf("%d\n",k);

	return 0;
}


Let's consider an n × n square matrix, consisting of digits one and zero.We'll consider a matrix good, if it meets the following condition: in each row of the matrix all ones go in one group. That is, each row of the matrix looks like that 00...0011...1100...00 (or simply consists of zeroes if it has no ones).You are given matrix a of size n × n, consisting of zeroes and ones. Your task is to determine whether you can get a good matrix b from it by rearranging the columns or not.

The first line contains integer n (1 ≤ n ≤ 500) — the size of matrix a. Each of n following lines contains n characters "0" and "1" — matrix a. Note that the characters are written without separators.

Print "YES" in the first line, if you can rearrange the matrix columns so as to get a good matrix b. In the next n lines print the good matrix b. If there are multiple answers, you are allowed to print any of them. If it is impossible to get a good matrix, print "NO".


Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place. Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.

The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).

Print n space-separated integers, representing the permutation that is the answer for the question.

NoteThe standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].

#include<stdio.h>
long long int fib[55];
void fibo(long long int n)
{
	if(n==53)
		return;
	fib[n]=fib[n-1]+fib[n-2];
	fibo(n+1);
}
void func(long long int k,long long int r,long long int n)
{
	if(n==0)
		return;
	if(k<=fib[n])
	{
		printf("%lld ",1+r);
		func(k,r+1,n-1);
	}
	else
	{
		printf("%lld %lld ",2+r,1+r);
		func(k-fib[n],r+2,n-2);
	}
}
long long int main()
{
	long long int n,k;
	scanf("%lld %lld",&n,&k);
	fib[1]=1;
	fib[2]=1;
	fibo(3);
	func(k,0,n);
	printf("\n");
	return 0;
}

/* practice with Dukkha */
#include <stdio.h>

#define N	50

int main() {
	static long long ff[N + 1];
	int n, i, a;
	long long k;

	scanf("%d%lld", &n, &k);
	ff[0] = ff[1] = 1;
	for (i = 2; i <= n; i++)
		ff[i] = ff[i - 1] + ff[i - 2];
	a = 0;
	while (n > 0)
		if (k <= ff[n - 1]) {
			printf("%d ", a + 1);
			a++;
			n--;
		} else {
			k -= ff[n - 1];
			printf("%d %d ", a + 2, a + 1);
			a += 2;
			n -= 2;
		}
	printf("\n");
	return 0;
}

#include<stdio.h>
#define ll long long
ll fib[51];

void solve( int n, int *a, ll k ) {
	if( n == 0 )
		return;
	if( k <= fib[n-1] ) {
		solve( n-1, a+1, k );
	}
	else {
		int t = a[0];
		a[0] = a[1];
		a[1] = t;
		solve( n-2, a+2, k - fib[n-1]);
	}
	return;
}

int main() {
	int n;
	long long k;
	scanf("%d%lld", &n, &k);
	int a[n];
	fib[0]=1;
	fib[1]=1;
	for( int i=0; i<n; ++i )
		a[i]=i+1;
	for(int i=2; i<=n; ++i )
		fib[i] = fib[i-1]+fib[i-2];
	solve( n, a, k );
	for( int i=0; i<n; ++i )
		printf("%d ", a[i]);
	printf("\n");
}

#include <stdio.h>
#include <inttypes.h>
#include <assert.h>

uint64_t fib[51];

void findperm(uint64_t n, uint64_t k, uint64_t offset){
  assert( k <= fib[n]);
  if (n == 0){
    puts("");
    return;
  }
  if (n == 1){
    printf("%llu\n", offset);
    return;
  }
  if ( k <= fib[n-1]){
    printf("%llu ", offset);
    findperm(n-1, k, offset+1);
  }
  else{
    printf("%llu %llu ", offset+1, offset);
    findperm(n-2, k -fib[n-1], offset+2);
  }
}

int main(void){
  uint64_t n, k;
  fib[0] = 1;
  fib[1] = 1;
  for (size_t k = 2; k <= 50; k++)
    fib[k] = fib[k-1]+fib[k-2];

  scanf("%llu %llu", &n, &k);
  findperm(n, k, 1);
  return 0;
}

#include<stdio.h>
typedef long long unsigned llu;
typedef unsigned u;
llu F[64];
void C(u n,llu l,u p)
{
	if(!n){putchar('\n');return;}
	if(F[n-1]>=l)
	{
		printf("%u ",p);
		C(n-1,l,p+1);
	}
	else
	{
		printf("%u %u ",p+1,p);
		C(n-2,l-F[n-1],p+2);
	}
	return;
}
int main()
{
	u i,j,k,n;llu l;
	for(F[F[0]=i=1]=1;++i<64;F[i]=F[i-1]+F[i-2]);
	scanf("%u%I64u",&n,&l);
	C(n,l,1);
	return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

long long fib[52];

void F(int n, long long k, int inc){
    if (n == 1) printf("%d ", 1 + inc);
    if (n == 2){
        if (k == 1) printf("%d %d ", 1 + inc, 2 + inc);
        else printf("%d %d ", 2 + inc, 1 + inc);
    }
    if (n < 3) return;

    if (k <= fib[n - 1]){
        printf("%d ", 1 + inc);
        F(n - 1, k, inc + 1);
    }
    else{
        printf("%d %d ", 2 + inc, 1 + inc);
        F(n - 2, k - fib[n - 1], inc + 2);
    }
}

int main(){
    int i, j, n;
    long long k;

    fib[0] = 1, fib[1] = 1;
    for (i = 2; i < 52; i++) fib[i] = fib[i - 1] + fib[i - 2];

    while (scanf("%d %I64d", &n, &k) != EOF){
        F(n, k, 0);
        puts("");
    }
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main(){
	int n,i;
	long long k,*left;
	scanf("%d %I64d",&n,&k);
	if(n<3){
		if(k==1){
			printf("1");
			if(n==1)
				printf("\n");
			else
				printf(" 2\n");
		}
		else
			printf("2 1\n");
		return 0;
	}
	left=(long long *)malloc(sizeof(long long)*n);
	left[n-1]=0;
	left[n-2]=1;
	for(i=n-3;i>0;i--){
		left[i]=left[i+1]+left[i+2]+1;
	}
	k--;
	for(i=1;i<n;i++){
		if(k>left[i]){
			printf("%d %d ",i+1,i);
			k-=left[i]+1;
			i++;
		}
		else
			printf("%d ",i);
	}
	if(i==n)
		printf("%d ",n);
	printf("\n");
	return 0;
}

#include <stdio.h>

long long f[60],k;
int i,j;
int n;
int p[60];

void dfs(int,int,long long);

int main()
{
    scanf("%d%lld",&n,&k);
    f[0]=f[1]=1;
    for(i=2;i<=n;i++)
    {
        f[i]=f[i-1]+f[i-2];
    }
    dfs(1,n,k);
    for(i=1;i<n;i++)
        printf("%d ",p[i]);
    printf("%d\n",p[i]);
    return 0;
}

void dfs(int i,int j,long long tmp)
{
    int k,w,ww;
    /*printf("%d %d %lld\n",i,j,tmp);*/
    if(i==n+1) return;
    for(k=1;k<=2 && k<=j;k++)
    {
        if(f[j-k]>=tmp)
        {
            w=i+k-1;
            ww=i+k-1;
            while(i<=ww)
            {
                p[i]=w;
                i++;
                w--;
            }
            dfs(i,j-k,tmp);
            return;
        }
        else tmp-=f[j-k];
    }
}

#include<stdio.h>
long long int fib[55];
void fibo(int n)
{
	if(n==53)
		return;
	fib[n]=fib[n-1]+fib[n-2];
	fibo(n+1);
}
void func(int k,int r,int n)
{
	if(n==0)
		return;
	if(k<=fib[n])
	{
		printf("%d ",1+r);
		func(k,r+1,n-1);
	}
	else
	{
		printf("%d %d ",2+r,1+r);
		func(k-fib[n],r+2,n-2);
	}
}
int main()
{
	int n,k;
	scanf("%d %d",&n,&k);
	fib[1]=1;
	fib[2]=1;
	fibo(3);
	func(k,0,n);
	printf("\n");
	return 0;
}

#include<stdio.h>
typedef long long unsigned llu;
typedef unsigned u;
void F(u n,llu l,u p)
{
	if(!n){printf("%u\n",p);return;}
	u i=1,j=n-1;
	while((1llu<<j)<l){l-=(1llu<<j--);++i;}
	for(j=i;j--;)printf("%u ",j+p);
	if(n>=i)F(n-i,l,p+i);
	return;
}
int main()
{
	u n;llu l;
	scanf("%u%I64u",&n,&l);
	F(n-1,l,1);
	return 0;
}

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)

long long fib[52];

void F(int n, int k, int inc){
    if (n == 1) printf("%d ", 1 + inc);
    if (n == 2){
        if (k == 1) printf("%d %d ", 1 + inc, 2 + inc);
        else printf("%d %d ", 2 + inc, 1 + inc);
    }
    if (n < 3) return;

    if (k <= fib[n - 1]){
        printf("%d ", 1 + inc);
        F(n - 1, k, inc + 1);
    }
    else{
        printf("%d %d ", 2 + inc, 1 + inc);
        F(n - 2, k - fib[n - 1], inc + 2);
    }
}

int main(){
    int i, j, n;
    long long k;

    fib[0] = 1, fib[1] = 1;
    for (i = 2; i < 52; i++) fib[i] = fib[i - 1] + fib[i - 2];

    while (scanf("%d %I64d", &n, &k) != EOF){
        F(n, k, 0);
        puts("");
    }
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main(){
	int n,i;
	long long k,*left;
	scanf("%d %I64d",&n,&k);
	if(n<3){
		if(k==1){
			printf("1");
			if(n==1)
				printf("\n");
			else
				printf(" 2\n");
		}
		else
			printf("2 1\n");
		return 0;
	}
	left=(long long *)malloc(sizeof(long long)*n);
	left[n-1]=0;
	left[n-2]=1;
	left[n-3]=2;
	for(i=n-4;i>0;i--){
		left[i]=left[i+1]+left[i+3]+2;
	}
	k--;
	for(i=1;i<n;i++){
		if(k>left[i]){
			printf("%d %d ",i+1,i);
			k-=left[i]+1;
			i++;
		}
		else
			printf("%d ",i);
	}
	if(i==n)
		printf("%d ",n);
	printf("\n");
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main(){
	int n,i;
	long long k,*left;
	scanf("%d %I64d",&n,&k);
	if(n<3){
		if(k==1){
			printf("1");
			if(n==1)
				printf("\n");
			else
				printf(" 2\n");
		}
		else
			printf("2 1\n");
		return 0;
	}
	left=(long long *)malloc(sizeof(long long)*n);
	left--;
	left[n]=-1;
	left[n-1]=0;
	left[n-2]=1;
	for(i=n-3;i>0;i--){
		left[i]=left[i+1]+left[i+3]+2;
	}
	k--;
	for(i=1;i<n;i++){
		if(k>left[i]){
			printf("%d %d ",i+1,i);
			k-=left[i]+1;
			i++;
		}
		else
			printf("%d ",i);
	}
	if(i==n)
		printf("%d ",n);
	printf("\n");
	return 0;
}

#include <stdio.h>

long long f[60];
int i,j;
int n,k;
int p[60];

void dfs(int,int,long long);

int main()
{
    scanf("%d%lld",&n,&k);
    f[0]=f[1]=1;
    for(i=2;i<=n;i++)
    {
        f[i]=f[i-1]+f[i-2];
    }
    dfs(1,n,k);
    for(i=1;i<n;i++)
        printf("%d ",p[i]);
    printf("%d\n",p[i]);
    return 0;
}

void dfs(int i,int j,long long tmp)
{
    int k,w,ww;
    /*printf("%d %d %lld\n",i,j,tmp);*/
    if(i==n+1) return;
    for(k=1;k<=2 && k<=j;k++)
    {
        if(f[j-k]>=tmp)
        {
            w=i+k-1;
            ww=i+k-1;
            while(i<=ww)
            {
                p[i]=w;
                i++;
                w--;
            }
            dfs(i,j-k,tmp);
            return;
        }
        else tmp-=f[j-k];
    }
}

#include <stdio.h>

long long f[60];
int i,j;
int n,k;
int p[60];

void dfs(int,int,long long);

int main()
{
    scanf("%d%d",&n,&k);
    f[0]=f[1]=1;
    for(i=2;i<=n;i++)
    {
        f[i]=f[i-1]+f[i-2];
    }
    dfs(1,n,k);
    for(i=1;i<n;i++)
        printf("%d ",p[i]);
    printf("%d\n",p[i]);
    return 0;
}

void dfs(int i,int j,long long tmp)
{
    int k,w,ww;
    /*printf("%d %d %lld\n",i,j,tmp);*/
    if(i==n+1) return;
    for(k=1;k<=2 && k<=j;k++)
    {
        if(f[j-k]>=tmp)
        {
            w=i+k-1;
            ww=i+k-1;
            while(i<=ww)
            {
                p[i]=w;
                i++;
                w--;
            }
            dfs(i,j-k,tmp);
            return;
        }
        else tmp-=f[j-k];
    }
}

#include <stdio.h>

long long f[60];
int i,j;
int n,k;
int p[60];

void dfs(int,int,long long);

int main()
{
    scanf("%d%d",&n,&k);
    f[0]=f[1]=1;
    for(i=2;i<=n;i++)
    {
        f[i]=f[i-1]*2;
    }
    dfs(1,n,k);
    for(i=1;i<n;i++)
        printf("%d ",p[i]);
    printf("%d\n",p[i]);
    return 0;
}

void dfs(int i,int j,long long tmp)
{
    int k,w,ww;
    /*printf("%d %d %lld\n",i,j,tmp);*/
    if(i==n+1) return;
    for(k=1;k<=j;k++)
    {
        if(f[j-k]>=tmp)
        {
            w=i+k-1;
            ww=i+k-1;
            while(i<=ww)
            {
                p[i]=w;
                i++;
                w--;
            }
            dfs(i,j-k,tmp);
            return;
        }
        else tmp-=f[j-k];
    }
}