Mary has just graduated from one well-known University and is now attending celebration party. Students like to dream of a beautiful life, so they used champagne glasses to construct a small pyramid. The height of the pyramid is n. The top level consists of only 1 glass, that stands on 2 glasses on the second level (counting from the top), then 3 glasses on the third level and so on.The bottom level consists of n glasses.Vlad has seen in the movies many times how the champagne beautifully flows from top levels to bottom ones, filling all the glasses simultaneously. So he took a bottle and started to pour it in the glass located at the top of the pyramid.Each second, Vlad pours to the top glass the amount of champagne equal to the size of exactly one glass. If the glass is already full, but there is some champagne flowing in it, then it pours over the edge of the glass and is equally distributed over two glasses standing under. If the overflowed glass is at the bottom level, then the champagne pours on the table. For the purpose of this problem we consider that champagne is distributed among pyramid glasses immediately. Vlad is interested in the number of completely full glasses if he stops pouring champagne in t seconds.Pictures below illustrate the pyramid consisting of three levels.

The only line of the input contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 10 000) — the height of the pyramid and the number of seconds Vlad will be pouring champagne from the bottle.

Print the single integer — the number of completely full glasses after t seconds.

NoteIn the first sample, the glasses full after 5 seconds are: the top glass, both glasses on the second level and the middle glass at the bottom level. Left and right glasses of the bottom level will be half-empty.

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

int main() {

    int i, j, count, rcount, n, t, index;
    float extra;

    scanf("%d %d", &rcount, &t);
    n = rcount*(rcount+1)/2;

    float *arr = (float *)malloc(n*sizeof(float));

    for(i=0; i<n; i++) {
        arr[i] = 0;
    }

    arr[0] = t;
    index = -1;
    for(i=0; i<rcount-1; i++) {
        for(j=0; j<=i; j++) {
            if(arr[++index] > 1) {
                extra = arr[index]-1;
                arr[index] = 1;
                arr[index+i+1] += extra/2;
                arr[index+i+1+1] += extra/2;
            }
        }
    }
    //Last row
    for(j=0; j<=i; j++) {
        if(arr[++index] > 1) {
            extra = arr[index]-1;
            arr[index] = 1;
            //arr[index+i+1] += extra/2;
            //arr[index+i+1+1] += extra/2;
        }
    }

    count=0;
    for(i=0; i<n; i++) {
        if(arr[i] == 1) {
            count++;
        }
    }

    printf("%d", count);

    return 0;
}

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

int findLChild(int n) {
    int i, levelNo, nCopy;
    nCopy = n;
    n++;
    levelNo = 0;
    do {
        levelNo++;
        n -= levelNo;
    }while(n>0);
    return nCopy+levelNo;
}

void pour(float *arr, int n, int glassNo, float quant) {

    if(glassNo <= n-1) {
        arr[glassNo] += quant;
        if(arr[glassNo] > 1) {
            float extra = arr[glassNo] - 1;
            arr[glassNo] = 1;
            int lChild = findLChild(glassNo);
            pour(arr, n, lChild, extra/2);
            pour(arr, n, lChild+1, extra/2);
        }
    }

}

int main() {

    int i, count, flag, n, t;
    float lower, upper, quant, quantc;

    scanf("%d%d", &n, &t);
    n = n*(n+1)/2;

    float *arr = (float *)malloc(n*sizeof(float));

    for(i=0; i<n; i++) {
        arr[i] = 0;
    }

    pour(arr, n, 0, t);
    //for(i=0; i<n; i++) {
    //    printf("%d %f\n", i, arr[i]);
    //}

    count=0;
    for(i=0; i<n; i++) {
        if(arr[i] == 1) {
            count++;
        }
    }

    printf("%d", count);

    return 0;
}

#include<stdio.h>
#include<math.h>
#define MAXN 11

double a[MAXN][MAXN];
const double eps=1e-5;
int n;

void dfs(int r,int c,double v)
{
    if(r==n+1)return ;
    if(fabs(v)<eps)return ;
    if(a[r][c]+eps>=1.0)
    {
        dfs(r+1,c,v/2);
        dfs(r+1,c+1,v/2);
    }
    else
    {
        if(a[r][c]+v+eps<1.0)a[r][c]+=v;
        else
        {
            dfs(r+1,c,(v-(1-a[r][c]))/2);
            dfs(r+1,c+1,(v-(1-a[r][c]))/2);
            a[r][c]=1.0;
        }
    }
}

int main()
{
    int i,j,cnt;
    double t;

    scanf("%d%lf",&n,&t);
    for(i=0;i<t;i++)dfs(1,1,1);
    cnt=0;
    for(i=1;i<=n;i++)
        for(j=1;j<=i;j++)if(a[i][j]+eps>=1.0)cnt++;
    printf("%d\n",cnt);

    return 0;
}

//题目是有趣的
//就是说在t秒的时候有多少是满的！
#include<stdio.h>
#define N 10
int main() {
    int n,t;
    scanf("%d%d",&n,&t);
    double pyramid[N][N];
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            pyramid[i][j]=0.0;
        }
    }
    while(t--){
        pyramid[0][0]+=1;
        for(i=0;i<n-1;i++){
            for(j=0;j<=i;j++){
                if(pyramid[i][j]>1){
                    double more=pyramid[i][j]-1;
                    pyramid[i][j]=1;
                    pyramid[i+1][j]+=(more/2.0);
                    pyramid[i+1][j+1]+=(more/2.0);
                }
            }
        }
    }
    int glass=0;
    for(i=0;i<n;i++){
        for(j=0;j<=i;j++){
            //printf(" %f",pyramid[i][j]);
            if(pyramid[i][j]>=1.0){
                glass++;
            }
        }
        //printf("\n");
    }

    printf("%d\n",glass);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define INF 2000000000
#ifdef ONLINE_JUDGE
#define ILL(x) scanf("%I64d", &(x));
#define OLL(x) printf("%I64d", x);
#else
#define ILL(x) scanf("%lld", &(x));
#define OLL(x) printf("%lld", x);
#endif

int gl[11][11];

int main() {
	int n, t, r=0;
	scanf("%d%d", &n, &t);
	gl[0][0] = t << 10;
	for(int i=0 ; i<n ; i++) {
		for(int j=0 ; j<=i ; j++) {
			if(gl[i][j] >= 1024) {
				gl[i+1][j] += (gl[i][j] - 1024) >> 1;
				gl[i+1][j+1] += (gl[i][j] - 1024) >> 1;
				r++;
			}
		}
	}
	printf("%d\n", r);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
    int n,i,t,j,totglasses,k;
    scanf("%d %d",&n,&t);
    totglasses = (n*(n+1))/2;
    double howfull[20][20];
    double howfall[20][20];
    int pascal[20][20];
    for(i=0;i<20;i++)
    {
        for(j=0;j<20;j++)
        {
            howfull[i][j] = 0;howfall[i][j] = 0;
            pascal[i][j] = 0;
        }
    }
    pascal[0][0] = 1;
    for(i=0;i<12;i++)
    {
        for(j=0;j<=i;j++)
        {
            pascal[i+1][j] += pascal[i][j];
            pascal[i+1][j+1] += pascal[i][j];
        }
    }
    if(t==0)
    {
        printf("0\n");
        return 0;
    }
    else if(t==1)
    {
        printf("1\n");
        return 0;
    }
    else
    {
        howfull[0][0] = 1;
        for(i=1;i<t;i++)
        {
            howfall[0][0] = 1;
            for(j=0;j<n;j++)
            {
                for(k=0;k<=j;k++)
                {
                    if(howfall[j][k]>=0.00000001)
                    {
                        howfull[j][k] += howfall[j][k];
                        if(1-howfull[j][k]<=0.00000001)
                        {
                            howfall[j+1][k] += (howfull[j][k] - 1)/2;
                            howfall[j+1][k+1] += (howfull[j][k] - 1)/2;
                            howfull[j][k]=1;
                        }

                        howfall[j][k]=0;
                    }
                }
            }
        }
        int ans=0;
            for(j=0;j<n;j++)
            {
                for(k=0;k<=j;k++)
                {
                    if(1-howfull[j][k]<=0.00000001)
                    {
                        ans++;
                    }
                    //printf("%lf ",howfull[j][k]);
                }
                //printf("\n");
            }
        printf("%d\n",ans);
    }
	// your code goes here
	return 0;
}

#include <stdio.h>
long double arr[100]={0};
int n;
void add(int ind,long double value,int level)
{
	if(arr[ind]+value>1)
	{
		if(ind+level<=(n*(n+1))/2)
		add(ind+level,(arr[ind]+value-1)/2,level+1);
		else
			return;
		if(ind+level+1<=(n*(n+1)/2))
		add(ind+level+1,(arr[ind]+value-1)/2,level+1);
		else
			return ;
	}
	else
	{
		arr[ind]=arr[ind]+value;
		return;
	}
}
int main()
{
	int i,t,count=0;
	scanf("%d %d",&n,&t);
	for(i=0;i<t;i++)
	{
		add(1,1,1);
	}
	for(i=1;i<=(n*(n+1))/2;i++)
	{
		if(arr[i]==1)
			count++;
	}
	printf("%d\n",count);
	return 0;
}

#include <stdio.h>
int main(void)
{
        int level, time, i, j, glass=0;
        float pour[12][12]={0};
        scanf("%d%d",&level,&time);
        pour[1][1] = time;
        for(i=1;i<=level;i++)
        {
                for(j=1;j<=i;j++)
                {
                        if(pour[i][j]>=1){
                                pour[i+1][j] += (pour[i][j]-1)/2;
                                pour[i+1][j+1] += (pour[i][j]-1)/2;
                                glass++;
                        }
                }
        }
        printf("%d",glass);
        return 0;
}

#include<stdio.h>
#include<string.h>
double a[10][10];
int n;
void dd(int x,int y,double t)
{
	a[x][y]+=t;
	if(a[x][y]>1)
	{
		double num=(a[x][y]-1)/2;
		a[x][y]=1;
		int tx=x+1;
		int ty=y+1;
		if(tx<n)
		{
			dd(tx,y,num);
			dd(tx,ty,num);
		}
	}
}
int main()
{

	int t;
	while(scanf("%d%d",&n,&t)!=EOF)
	{
		memset(a,0,sizeof(a));
		dd(0,0,t);
		int i,j;
		int counter=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<=i;j++)
			{
				if(a[i][j]==1)
					counter++;
			}
		}
		printf("%d\n",counter);
	}
	return 0;
}

//
//  main.c
//  160525 CF-B
//
//  Created by tjua on 5/25/16.
//  Copyright © 2016 tjua. All rights reserved.
//

#include <stdio.h>

int main(int argc, const char * argv[]) {
    // insert code here...

    int n, t, count = 0;
    double a[12][12] = {0}, k;
    int i, j;
//    struct node{
//        //no.
//        int s; // 10 bits
//        int
//    };
//

    // true baolo
    scanf("%d%d", &n, &t);

    a[1][1]=t;
    for (i=1;i<=n;i++) {
        for (j=1;j<=i;j++) {
            if (a[i][j]>1-0.00001){
                count++;
                k=(a[i][j]-1)/2;
                a[i+1][j]+=k;
                a[i+1][j+1]=k;
            }
        }
    }

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

   // printf("Hello, World!\n");
    return 0;
}

#include<stdio.h>
#include<math.h>
float a[11][11]={0.0};
int count=0;
void recur(int m,int n,int limit,int time){
    int i;
    if(m==limit){
        return;
    }
    else{
        if(a[m][n]==1){
            recur(m+1,n,limit,time);
            recur(m+1,n+1,limit,time);
        }
        else if(a[m][n]<1){
            a[m][n]+=pow(2.0,-1*m);
        }
    }
}
int main(){
    int i,j,k,n,t;
    scanf("%d%d",&n,&t);
    for(i=1;i<=t;i++){
        recur(0,0,n,t);
    }
    for(i=0;i<n;i++){
        for(j=0;j<=i;j++){
            if(a[i][j]==1)count++;
        }
    }
    printf("%d",count);
    scanf(" ");
    return 0;
}

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

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

int n, t;
double ar[15][15];

void pour(int i, int j){
    if (i > n) return;
    if (ar[i][j] > 1.0){
        double x = (ar[i][j] - 1.0) / 2.0;
        ar[i + 1][j] += x, ar[i + 1][j + 1] += x;
        pour(i + 1, j);
        pour(i + 1, j + 1);
        ar[i][j] = 1.0;
    }
}

int main(){
    int i, j, k, l, res;

    while (scanf("%d %d", &n, &t) != EOF){
        clr(ar);
        while (t--){
            ar[1][1] += 1.0;
            pour(1, 1);
        }

        res = 0;
        for (i = 1; i <= n; i++){
            for (j = 1; j <= i; j++){
                if (fabs(ar[i][j] - 1.0) <= 1e-9) res++;
            }
        }

        printf("%d\n", res);
    }
    return 0;
}

#include<stdio.h>
int level[100];
int solve(float a[],int num,int index,float amount)
{
    float bcha;
    int lev;
   if(index>num)
        return 0;
   if(amount<=0)
    return 0;
   if(a[index]+amount<=1)
   {
       a[index]=a[index]+amount;
       return 0;
   }
   else
   {
       bcha=a[index]+amount-1;
       lev=level[index];
       solve(a,num,index+lev,bcha/2);
       solve(a,num,index+lev+1,bcha/2);
   }

return 0;
}
int main()
{
    int i,j,k,n,m,sum,sum1,l,num,t,ans,nl;
    sum=1;
    n=1;
    sum1=(n*(n+1))/2;
    l=1;
    while(i<=60)
    {
        level[sum]=l;
        sum++;
        if(sum>sum1)
        {
            n++;
            sum1=(n*(n+1))/2;
            l++;
        }
        i++;
    }
    scanf("%d%d",&nl,&t);
    num=(nl*(nl+1))/2;
    float a[100]={0};
    for(i=1;i<=t;i++)
        solve(a,num,1,1);
    ans=0;
    for(i=1;i<=num;i++)
    {
        if(a[i]>=1.00)
            ans++;
    }
    printf("%d",ans);
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define size_cup (sizeof(__cup))

struct st_cup	{
	double filled_vol;
	struct st_cup *rightBro;
	struct st_cup *left;
	struct st_cup *right;
};
typedef struct st_cup __cup;

int maxLevel;
int maxCup;
int m;

int create_tree(__cup *node, int level)	{
	__cup *temp;
	if(level == maxLevel)
		return 0;
	temp = (__cup *)malloc(size_cup);
	temp->filled_vol = 0.00;
	temp->left = NULL;
	temp->right = NULL;
	node->left = temp;
	if(node->rightBro == NULL)	{
		temp = (__cup *)malloc(size_cup);
		temp->filled_vol = 0.00;
		temp->rightBro = NULL;
		temp->right = NULL;
		temp->left = NULL;
		node->right = temp;
		node->left->rightBro = node->right;
	}
	else	{
		node->right = node->rightBro->left;
		node->left->rightBro = node->right;
	}
	level++;
	create_tree(node->right, level);
	create_tree(node->left, level);
	return 0;
}

int show_tree(__cup *head)	{
	__cup *temp, *ttemp;
	temp = head;
	int i, j;
	while(temp)	{
		ttemp = temp;	j = 1;
		while(ttemp)	{
			printf("%d ",j++);
			ttemp = ttemp->rightBro;
		}
		puts("");
		temp = temp->left;
	}
	return 0;
}

int free_tree(__cup *head)	{
	__cup *temp, *ttemp, *saveLeft, *saveRightBro;
	int i = 0;
	temp = head;
	while(temp)	{
		saveLeft = temp->left;
		ttemp = temp;
		while(ttemp)	{	i++;
			saveRightBro = ttemp->rightBro;
			free(ttemp);
			ttemp = saveRightBro;
		}
		temp = saveLeft;
	}

	return i;
}

int manage(__cup *node, double fa)	{
	__cup *temp;

	if(m == maxCup)	return 0;
	if(node->filled_vol < 1.00)	{
		node->filled_vol += fa;
		fa = 0.00;
		if(node->filled_vol >= 1.00)	{
			m++;
			fa = node->filled_vol - 1.00;
		}
	}
	if(fa>0.00)	{
		if(node->left!=NULL)	manage(node->left, (fa/2.00));
		if(node->right!=NULL)	manage(node->right, (fa/2.00));
	}
	return 0;
}

int main()	{
	__cup *head;
	int i, time;

	m = 0;
	scanf("%d %d", &maxLevel, &time);
	maxCup = maxLevel*(maxLevel+1)/2;
//	printf("maxCup: %d\n",maxCup);
	head = (__cup *)malloc(size_cup);
	head->rightBro = NULL;
	head->left = head->right = NULL;
	create_tree(head, 1);
	for(i = 0; i<time; i++)	manage(head, 1.00);
	printf("%d\n", m);
//	show_tree(head);
	free_tree(head);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define size_cup (sizeof(__cup))

struct st_cup	{
	double filled_vol;
	struct st_cup *rightBro;
	struct st_cup *left;
	struct st_cup *right;
};
typedef struct st_cup __cup;
int maxLevel, maxCup, m;

int create_tree(__cup *node, int level)	{
	__cup *temp;
	if(level == maxLevel)	return 0;
	temp = (__cup *)malloc(size_cup);
	temp->filled_vol = 0.00;	temp->left = NULL;	temp->right = NULL;
	node->left = temp;
	if(node->rightBro == NULL)	{
		temp = (__cup *)malloc(size_cup);
		temp->filled_vol = 0.00;	temp->rightBro = NULL;
		temp->right = NULL;	temp->left = NULL;
		node->right = temp;
		node->left->rightBro = node->right;
	}
	else	{
		node->right = node->rightBro->left;
		node->left->rightBro = node->right;
	}
	level++;
	create_tree(node->right, level);
	create_tree(node->left, level);
	return 0;
}

int free_tree(__cup *head)	{
	__cup *temp, *ttemp, *saveLeft, *saveRightBro;
	temp = head;
	while(temp)	{
		saveLeft = temp->left;		ttemp = temp;
		while(ttemp)	{
			saveRightBro = ttemp->rightBro;	free(ttemp);
			ttemp = saveRightBro;
		}
		temp = saveLeft;
	}
	return 0;
}

int manage(__cup *node, double fa)	{
	__cup *temp;
	if(m == maxCup)	return 0;
	if(node->filled_vol < 1.00)	{
		node->filled_vol += fa;	fa = 0.00;
		if(node->filled_vol >= 1.00)	{
			m++;	fa = node->filled_vol - 1.00;
		}
	}
	if(fa>0.00)	{
		if(node->left)	manage(node->left, (fa/2.00));
		if(node->right)	manage(node->right, (fa/2.00));
	}
	return 0;
}

int main()	{
	__cup *head;
	int i, time;
	m = 0;
	scanf("%d %d", &maxLevel, &time);
	maxCup = maxLevel*(maxLevel+1)/2;
	head = (__cup *)malloc(size_cup);
	head->rightBro = NULL;
	head->left = NULL;	head->right = NULL;
	create_tree(head, 1);
	manage(head, (double)time);
	printf("%d\n", m);
	free_tree(head);
	return 0;
}

#include<stdio.h>
#include<stdlib.h>
#define size_cup (sizeof(__cup))

struct st_cup	{
	double filled_vol;
	struct st_cup *rightBro;
	struct st_cup *left;
	struct st_cup *right;
};
typedef struct st_cup __cup;
int maxLevel, maxCup, m;

int create_tree(__cup *node, int level)	{
	__cup *temp;
	if(level == maxLevel)	return 0;
	temp = (__cup *)malloc(size_cup);
	temp->filled_vol = 0.00;	temp->left = NULL;	temp->right = NULL;
	node->left = temp;
	if(node->rightBro == NULL)	{
		temp = (__cup *)malloc(size_cup);
		temp->filled_vol = 0.00;	temp->rightBro = NULL;
		temp->right = NULL;	temp->left = NULL;
		node->right = temp;
		node->left->rightBro = node->right;
	}
	else	{
		node->right = node->rightBro->left;
		node->left->rightBro = node->right;
	}
	level++;
	create_tree(node->right, level);
	create_tree(node->left, level);
	return 0;
}

int free_tree(__cup *head)	{
	__cup *temp, *ttemp, *saveLeft, *saveRightBro;
	temp = head;
	while(temp)	{
		saveLeft = temp->left;		ttemp = temp;
		while(ttemp)	{
			saveRightBro = ttemp->rightBro;	free(ttemp);
			ttemp = saveRightBro;
		}
		temp = saveLeft;
	}
	return 0;
}

int manage(__cup *node, double fa)	{
	__cup *temp;
	if(m == maxCup)	return 0;
	if(node->filled_vol < 1.00)	{
		node->filled_vol += fa;	fa = 0.00;
		if(node->filled_vol >= 1.00)	{
			m++;	fa = node->filled_vol - 1.00;
		}
	}
	if(fa>0.00)	{
		if(node->left)	manage(node->left, (fa/2.00));
		if(node->right)	manage(node->right, (fa/2.00));
	}
	return 0;
}

int main()	{
	__cup *head;
	int i, time;
	m = 0;
	scanf("%d %d", &maxLevel, &time);
	maxCup = maxLevel*(maxLevel+1)/2;
	head = (__cup *)malloc(size_cup);
	head->rightBro = NULL;
	head->left = NULL;	head->right = NULL;
	create_tree(head, 1);
	for(i = 0; i<time; i++)	manage(head, 1.00);
	printf("%d\n", m);
	free_tree(head);
	return 0;
}

#include <stdio.h>

int main(int argc, char const *argv[])
{
	int n, t;
	scanf("%d %d", &n, &t);

	if(t < 1) {
		printf("0\n");
		return 0;
	}

	double a[n+1][n+1];
	int i, j, k;

	for(i = 0; i <= n; ++i) {
		for(j = 0; j <= n; ++j) {
			a[i][j] = (double)0;
		}
	}

	for(i = 0; i < t; ++i) {
		a[0][0] += (double)1;
		for(j = 0; j < n; ++j) {
			for(k = 0; k <= j; ++k) {
				if(a[j][k] > (double)1) {
					double diff = a[j][k] - (double)1;
					a[j][k] = (double)1;
					a[j+1][k] += diff / 2;
					a[j+1][k+1] += diff / 2;
				}
				//elapsed_time++;
			}
		}
	}

	int ans = 0;

	for(i = 0; i < n; ++i) {
		for(j = 0; j < n; ++j) {
			//printf("%lf ", a[i][j]);

			if(a[i][j] == (double)1) {
				ans++;
			}
		}
		//printf("\n");
	}

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

	return 0;
}

#include<stdio.h>

struct node{
    double cup;
    int level;
};
struct node Glass[56+12];

int main()
{
    int N, T, i, j, count=0, adv;
    double rest;
    for(i=1;i<=11;i++){
        for(j=1;j<=i;j++){
            Glass[count].level=i;
            Glass[count++].cup=0.00;
        }
    }
    scanf("%d %d",&N, &T);
    count=0;
    Glass[0].cup=T;
    for(i=0;Glass[i].level<=N;i++){
        //printf(">%lf\n",Glass[i].cup);
        if(Glass[i].cup>1.00){
            rest=Glass[i].cup-1.00;
            Glass[i].cup=1.00;
            adv=Glass[i].level;
            Glass[i+adv].cup+=rest/2.0;
            Glass[i+adv+1].cup+=rest/2.0;
        }
        if(Glass[i].cup>=1.00) count++;
    }
    printf("%d\n",count);
    return 0;
}

#include <stdio.h>

int n, t;
double arr[11][12];
int res;

void solve(int i, int j, double sum)
{
	if (!j || j > i) return;
	if (i > n) return;

	if (arr[i][j] == 1 && sum > 0)
	{
		solve(i + 1, j, sum/2);
		solve(i + 1, j + 1, sum/2);
	}
	else arr[i][j] += sum;
}
int main()
{
	scanf("%d %d", &n, &t);

	for (int i = 0; i < t; i++)
		solve(1, 1, 1);

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (arr[i][j] == 1) res++;
	printf("%d", res);
}

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

double  cup[1000][1000];
int main (){
  int h , t ;
  scanf("%d%d",&h,&t);
  for(int i=0;i<h;i++)
    for(int j=0;j<h;j++)
      cup[i][j]=0;
  cup[0][0] = t;
  int cnt = 0;
  for (int i = 0;i<h;i++){
    for (int j = 0;j<=i;j++){
        if(cup[i][j]>=1){
          double x = cup[i][j] - 1;
          cup[i][j] = 1;
          cup[i+1][j]+= x/2.0;
          cup[i+1][j+1]+= x/2.0;
          cnt++;
        }
    }
  }
  printf("%d\n",cnt);
  return 0;
}

#include <stdio.h>

int main(void) {
	int i, j;
	int n, t;
	int amount[55] = {0}, ans;

	scanf("%d %d", &n, &t);

	while (t--) {
		amount[0] += 512;
		for (i = 0; i < n - 1; i++) {
			const int cur = i * (i + 1) / 2, next = (i + 1) * (i + 2) / 2;
			for (j = cur; j < next; j++)
				if (amount[j] > 512) {
					amount[next + j - cur] += (amount[j] - 512) / 2;
					amount[next + j - cur + 1] += (amount[j] - 512) / 2;
					amount[j] = 512;
				}
		}
	}

	ans = 0;
	for (i = 0; i < n * (n + 1) / 2; i++)
		ans += amount[i] >= 512;

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

	return 0;
}

#include <math.h>
#include <stdio.h>

double amount[55];

int main(void) {
	int i, j;
	int n, t;
	int ans;

	scanf("%d %d", &n, &t);

	while (t--) {
		amount[0] += 1.0;
		for (i = 0; i < n; i++) {
			const int cur = i * (i + 1) / 2, next = (i + 1) * (i + 2) / 2;
			for (j = cur; j < next; j++)
				if (amount[j] > 1.0) {
					amount[next + j - cur] += (amount[j] - 1.0) / 2.0;
					amount[next + j - cur + 1] += (amount[j] - 1.0) / 2.0;
					amount[j] = 1.0;
				}
		}
	}

	ans = 0;
	for (i = 0; i < n * (n + 1) / 2; i++)
		ans += fabs(amount[i] - 1) < 1e-9;

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

	return 0;
}

#include <stdio.h>

float a[56];
int b[56];
int count = 0;
long n, t;

void pour(int i, int level, float dl){
    if (level > n) return;
    if (a[i] < 1.0) {
        a[i] += dl;
        if (a[i] >= 1) b[i] = 1;
        return;
    }
    pour(i+level,level+1,dl/2);
    pour(i+level+1,level+1,dl/2);
}

int main(){
    scanf("%ld %ld", &n, &t);
    int i;
    for (i = 1; i <= 55; i++)
        a[i] = 0;
    for (i = 1; i <= 55; i++)
        b[i] = 0;
    for (i = 1; i <= t; i++)
        pour(1,1,1.0);
    count = 0;
    for (i = 1; i <= 55; i++)
        if (b[i]) count++;
    printf("%d", count);
}

#include <stdio.h>
#define G 45
int glasses[10][10];
int powers[10];

void printmarix(int matrix[][10], int n, int m) {
    int i,j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main () {
    int n , t, i, j, volume, remaining, answer = 0;
    powers[0] = 1;
    powers[1] = 2;
    powers[2] = 4;
    powers[3] = 8;
    powers[4] = 16;
    powers[5] = 32;
    powers[6] = 64;
    powers[7] = 128;
    powers[8] = 256;
    powers[9] = 512;
    powers[10] = 1024;

    scanf("%d %d", &n, &t);
    volume = powers[n];
    glasses[0][0] = t*volume;
    for (i = 0; i < n; i++)
        for (j = 0; j < i+1; j++) {
            // printmarix(glasses, 10, 10);
            if (glasses[i][j] > volume) {
                remaining = glasses[i][j] - volume;
                glasses[i][j] = volume;
                glasses[i+1][j] += remaining/2;
                glasses[i+1][j+1] += remaining/2;
            }
        }

    for (i = 0; i < n; i++)
        for (j = 0; j < i+1; j++) {
            if (glasses[i][j] == volume)
                answer++;
    }
    printf("%d\n", answer);
    return 0;
}

#include <stdio.h>

int n, t;
double arr[11][11];

int squ(int x){
	int ret=1;

	for (int i=1; i<x; i++){
		ret*=2;
	}

	return ret;
}

int main(){

	int i, j,flag,sol=0;

	scanf("%d %d", &n, &t);

	arr[1][1]=(double)t;

	for (i=1; i<=n; i++){
		flag=0;
		for (j=1; j<=i; j++){
			if (arr[i][j]>1){
				arr[i][j]-=1;
				sol++;
				arr[i+1][j]+=arr[i][j]/2;
				arr[i+1][j+1]+=arr[i][j]/2;
				flag=1;
			}
			else if (arr[i][j]==1) sol++;
		}
		if (flag==0) break;
	}

	printf("%d\n", sol);
	return 0;
}

#include<stdio.h>

double calice[10][10];

void adiciona(double val, int linha, int coluna, int n){
	calice[linha][coluna] += val;
	if(linha>=n){return;}
	double excesso;
	if( calice[linha][coluna]>1 ){
		excesso = calice[linha][coluna] -1;
		calice[linha][coluna]=1;
		adiciona(excesso/2,linha+1,coluna,n);
		adiciona(excesso/2,linha+1,coluna+1,n);
	}
}

int main(void){
	int n, time;
	int i, j;
	int cheios=0;
	scanf("%d %d", &n, &time);
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			calice[i][j] = 0;
		}
	}
	for(i=0;i<time;i++){
		adiciona(1, 0,0,n);
	}
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(calice[i][j] == 1){
				cheios++;
			}
		}
	}
	printf("%d\n", cheios);

	return 0;
}

#include<stdio.h>
void cuprec(float arr[],int num,float amount,int height)
{
	if(height>10)
		return;
	if(arr[num]+amount<=1)
	{
		arr[num]=arr[num]+amount;
	}
	else
	{
		cuprec(arr,num+height+1,(amount-(1-arr[num]))/2,height+1);
		cuprec(arr,num+height,(amount-(1-arr[num]))/2,height+1);
	}
}
int main()
{
	float arr[56];
	int n,t,i,k,ncup,ans=0;
	scanf("%d%d",&n,&t);
	ncup=(n*(n+1))/2;
	for(i=0;i<56;i++)
	{
		arr[i]=0.000;
	}
	for(i=1;i<=t;i++)
	{
		cuprec(arr,1,1.00,1);
	}
	for(i=1;i<=ncup;i++)
	{
		if(arr[i]==1.00)
			ans++;
	}
	printf("%d\n",ans);
	return 0;
}

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

int py[12][11], vis[11][11], ne, ans, n, t;

void gao(){
    int i, j, temp;
    while(vis[n][1] == 0 && ne <= t){
        py[1][1] += 512;
        for(i = 1; i <= n; i ++){
            for(j = 1; j <= i; j ++){
                if(py[i][j] > 512){
                    temp = py[i][j] - 512, py[i][j] = 512;
                    temp /= 2;
                    py[i + 1][j] += temp, py[i + 1][j + 1] += temp;
                }
                if(py[i][j] == 512 && vis[i][j] == 0){
                    ans ++;
                    vis[i][j] = 1;
                }
            }
        }
        ne ++;
    }
}

int main()
{
    int i, sum;
    while(scanf("%d%d", &n, &t) != EOF){
        memset(py, 0, sizeof(py));
        memset(vis, 0, sizeof(vis));
        ne = 1, ans = 0;
        gao();
        sum = (n * (n + 1)) / 2;
        if(t > 1023)
            printf("%d\n", sum);
        else
            printf("%d\n", ans);
    }
    return 0;
}

#include<stdio.h>
#include<string.h>
#include <stdio.h>
#include <math.h>
#define swap(x,y) {int t=y;y=x;x=t;}

int main(){
    int n,m,i,j;
    while(scanf("%d %d",&i,&j)!=EOF){
    double a[11][11]={0};int num=0;
    a[0][0]=j;
    for(n=0;n<i;n++){
        for(m=0;m<11;m++){
            if(a[n][m]>=1){
                a[n+1][m+1]+=(a[n][m]-1)/2;
                a[n+1][m]+=(a[n][m]-1)/2;
                num++;
            }
        }
    }
    printf("%d\n",num);
    }
	return 0;
}

#include <stdio.h>
int n,t;
double glass[11][11]={0};
void flew(int level, int index){
	double sur = glass[level][index]-1;
	glass[level][index]-=sur;

	sur=sur/2.0;
	if(level+1<=n){
		glass[level+1][index]+=sur;
		if(glass[level+1][index]>1) flew(level+1,index);
		if(index+1<=n){
			glass[level+1][index+1]+=sur;
			if(glass[level+1][index+1]>1) flew(level+1,index+1);
		}
	}
}
int main(void) {
	int sec=0,i,j,count=0;
	scanf("%d%d",&n,&t);
	while(sec!=t){
		glass[1][1]++;
		if(glass[1][1]>1)flew(1,1);
		sec++;
	}
	for(i=1;i<=n;i++)
		for(j=1;j<=i;j++)
			if(glass[i][j]>=1)
				count++;

	printf("%d",count);
}

/* Coached by Dukkha */
#include <stdio.h>

#define N 10

int main() {
	static int aa[N + 1][N + 1];
	int n, t, m, i, j, ans;

	scanf("%d%d", &n, &t);
	m = 1 << n;
	while (t--) {
		aa[0][0] += m;
		for (i = 0; i < n; i++)
			for (j = 0; j <= i; j++) {
				int a = aa[i][j];

				if (a > m) {
					int x = (a - m) / 2;

					aa[i + 1][j] += x;
					aa[i + 1][j + 1] += x;
					aa[i][j] = m;
				}
			}
	}
	ans = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j <= i; j++)
			if (aa[i][j] == m)
				ans++;
	printf("%d\n", ans);
	return 0;
}

#include <stdio.h>
int n,c=0;
float t,arr[10][10]={0};

void add(int x,int y,float value,int k)
{
    float diff;
    if(value==0||n==k)
    {
        return;
    }
    else if(arr[x][y]==1)
    {
        add(x+1,y,value/2,k+1);
        add(x+1,y+1,value/2,k+1);
    }
    else if(arr[x][y]+value<1)
    {
    arr[x][y]=arr[x][y]+value;
    }
    else
    {
        diff=arr[x][y]+value-1;
        arr[x][y]=1;
        c++;
        add(x+1,y,diff/2,k+1);
        add(x+1,y+1,diff/2,k+1);
    }
}

int main(void) {
    int i,j;
    scanf("%d%f",&n,&t);
	add(0,0,t,0);
	printf("%d",c);
	return 0;
}

#include<stdio.h>
#define ll long long
#define ld long double
#define ui unsigned int
#define ull unsigned ll
#define foi(n) for(ll i=1;i<n;i++)
#define foj(n) for(ll j=1;j<n;j++)
#define fok(n) for(ll k=0;k<n;k++)
#pragma GCC optimize("O3", "unroll-loops")
//****************************************************************

//*****************UVAISH ZAFRI(Rush_For_AC)**********************
int main() {

    double G[15][15];
    foi(15)
    foj(15)
       G[i][j]=(double)0;

    ll N;double T ;
    scanf("%lld%lf",&N,&T);
    ll ans=0 ;

    G[1][1]=(double)T ;

    foi(N+1)
    foj(i+1)
        if(G[i][j]>=(double)1)
            {
                ++ans ;
                G[i+1][j] += (G[i][j]-(double)1)/(double)2 ;
                G[i+1][j+1] += (G[i][j]-(double)1)/(double)2 ;
            }
    printf("%lld",ans);
    return 0;
}

#include<stdio.h>

int main() {
    int i, count, flag;
    float lower, upper, quant;
    scanf("%f", &quant);

    count = 0;
    flag = 1;
    i=1;
    while(quant > 0 && flag > 0) {
        //Level number i had quant enterred into it
        flag = 0;
        if(i==1) {
            //Ratio 1
            if(quant >= 1 ) {
                quant -= 1;
                count += 1;
                i++;
                flag = 1;
            }
        } else if(i==2) {
            //Ratio 1:1
            if(quant >= 2 ) {
                quant -= 2;
                count += 2;
                i++;
                flag = 1;
            }
        } else {
            //Ratio 1:2:.....:2:1 i.e., (i-2) times 1 and 2 times half
            lower = quant*0.5/(i-1);
            upper = quant*1/(i-1);
            if(lower >= 1) {
                quant -= i;
                count += i;
                i++;
                flag = 1;
            } else if(upper >= 1) {
                quant -= i-2;
                count += i-2;
                i++;
                flag = 1;
            }
        }
    }

    printf("%d", count);

    return 0;
}

#include<stdio.h>

int main() {

    int i, count, flag, n, t;
    float lower, upper, quant;

    scanf("%d%d", &n, &t);

    quant = t;

    count = 0;
    flag = 1;
    i=1;
    while(quant > 0 && flag > 0) {
        //Level number i had quant enterred into it
        flag = 0;
        if(i==1) {
            //Ratio 1
            if(quant >= 1 ) {
                quant -= 1;
                count += 1;
                i++;
                flag = 1;
            }
        } else if(i==2) {
            //Ratio 1:1
            if(quant >= 2 ) {
                quant -= 2;
                count += 2;
                i++;
                flag = 1;
            }
        } else {
            //Ratio 1:2:.....:2:1 i.e., (i-2) times 1 and 2 times half
            lower = quant/(2*(i-1));
            upper = quant/(i-1);
            //printf("\nlower %f upper %f", lower, upper);
            if(lower >= 0.9999) {
                quant -= i;
                count += i;
                i++;
                flag = 1;
            } else if(upper >= 0.9999) {
                quant -= i-2;
                count += i-2;
                i++;
                flag = 1;
            }
        }

        if(i>n)
            break;
    }

    //printf("\n%d\n\n", count);
    printf("%d", count);

    return 0;
}

#include<stdio.h>

int main() {

    int i, count, flag, n, t;
    float lower, upper, quant;
    scanf("%d%d", &n, &t);

    quant = t;

    count = 0;
    flag = 1;
    i=1;
    while(quant > 0 && flag > 0) {
        //Level number i had quant enterred into it
        flag = 0;
        if(i==1) {
            //Ratio 1
            if(quant >= 1 ) {
                quant -= 1;
                count += 1;
                i++;
                flag = 1;
            }
        } else if(i==2) {
            //Ratio 1:1
            if(quant >= 2 ) {
                quant -= 2;
                count += 2;
                i++;
                flag = 1;
            }
        } else {
            //Ratio 1:2:.....:2:1 i.e., (i-2) times 1 and 2 times half
            lower = quant*0.5/(i-1);
            upper = quant*1/(i-1);
            if(lower >= 1) {
                quant -= i;
                count += i;
                i++;
                flag = 1;
            } else if(upper >= 1) {
                quant -= i-2;
                count += i-2;
                i++;
                flag = 1;
            }
        }

        if(i>n)
            break;
    }

    printf("%d", count);

    return 0;
}

#include<stdio.h>

int main() {

    int i, count, flag, n, t;
    float lower, upper, quant, quantc;

    scanf("%d%d", &n, &t);

    quant = t;
    //for(quantc = 2; quantc<=t; quantc++) {
    //quant = quantc;

    count = 0;
    flag = 1;
    i=1;
    while(quant > 0 && flag > 0) {
        //Level number i had quant enterred into it
        flag = 0;
        if(i==1) {
            //Ratio 1
            if(quant >= 1 ) {
                quant -= 1;
                count += 1;
                i++;
                flag = 1;
            }
        } else if(i==2) {
            //Ratio 1:1
            if(quant >= 2 ) {
                quant -= 2;
                count += 2;
                i++;
                flag = 1;
            }
        } else {
            //Ratio 1:2:.....:2:1 i.e., (i-2) times 1 and 2 times half
            lower = quant/(2*(i-1));
            upper = quant/(i-1);

            if(lower >= 0.999999) {
                quant -= i;
                count += i;
                i++;
                flag = 1;
            } else if(upper >= 0.999999) {
                quant -= i-2;
                count += i-2;
                i++;
                flag = 1;
            }
        }

        if(i>n)
            break;
    }

    //printf("\n%d\n", count);
    printf("%d", count);

    return 0;
}

#include<stdio.h>

int main() {

    int i, count, flag, n, t;
    float lower, upper, quant, quantc;

    scanf("%d%d", &n, &t);

    quant = t;
    //for(quantc = 2; quantc<=t; quantc++) {
    //quant = quantc;

    count = 0;
    flag = 1;
    i=1;
    while(quant > 0 && flag > 0) {
        //Level number i had quant enterred into it
        flag = 0;
        if(i==1) {
            //Ratio 1
            if(quant >= 1 ) {
                quant -= 1;
                count += 1;
                i++;
                flag = 1;
            }
        } else if(i==2) {
            //Ratio 1:1
            if(quant >= 2 ) {
                quant -= 2;
                count += 2;
                i++;
                flag = 1;
            }
        } else {
            //Ratio 1:2:.....:2:1 i.e., (i-2) times 1 and 2 times half
            lower = quant/(2*(i-1));
            upper = quant/(i-1);

            if(lower >= 0.99) {
                quant -= i;
                count += i;
                i++;
                flag = 1;
            } else if(upper >= 0.99) {
                quant -= i-2;
                count += i-2;
                i++;
                flag = 1;
            }
        }

        if(i>n)
            break;
    }

    //printf("\n%d\n", count);
    printf("%d", count);

    return 0;
}

//题目是有趣的
//就是说在t秒的时候有多少是满的！
#include<stdio.h>
#define N 10
double absu(double a){
    if(a>0)return a;
    else return -a;
}
int main() {
    int n,t;
    scanf("%d%d",&n,&t);
    double pyramid[N][N];
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            pyramid[i][j]=0.0;
        }
    }
    while(t--){
        pyramid[0][0]+=1;
        for(i=0;i<n-1;i++){
            for(j=0;j<=i;j++){
                if(pyramid[i][j]>1){
                    double more=pyramid[i][j]-1;
                    pyramid[i][j]=1;
                    pyramid[i+1][j]+=(more/2.0);
                    pyramid[i+1][j+1]+=(more/2.0);
                }
            }
        }
    }
    int glass=0;
    double error=0.000000001;
    for(i=0;i<n;i++){
        for(j=0;j<=i;j++){
            //printf(" %f",pyramid[i][j]);
            if(absu(pyramid[i][j]-1.0)<=error){
                glass++;
            }
        }
        //printf("\n");
    }

    printf("%d\n",glass);
	return 0;
}

//题目是有趣的
//就是说在t秒的时候有多少是满的！
#include<stdio.h>
#define N 10
double absu(double a){
    if(a>0)return a;
    else return -a;
}
int main() {
    int n,t;
    scanf("%d%d",&n,&t);
    double pyramid[N][N];
    int i,j;
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            pyramid[i][j]=0.0;
        }
    }
    while(t--){
        pyramid[0][0]+=1;
        for(i=0;i<n-1;i++){
            for(j=0;j<=i;j++){
                if(pyramid[i][j]>1){
                    double more=pyramid[i][j]-1;
                    pyramid[i][j]=1;
                    pyramid[i+1][j]+=(more/2.0);
                    pyramid[i+1][j+1]+=(more/2.0);
                }
            }
        }
    }
    int glass=0;
    double error=0.0000000001;
    for(i=0;i<n;i++){
        for(j=0;j<=i;j++){
            //printf(" %f",pyramid[i][j]);
            if(absu(pyramid[i][j]-1.0)<=error){
                glass++;
            }
        }
        //printf("\n");
    }

    printf("%d\n",glass);
	return 0;
}

#include <stdio.h>
int main(void)
{
        int level, time, tot_glass;
        int i, glass;
        scanf("%d%d",&level,&time);
        tot_glass = (level*(level+1))/2;
        glass = 1;
        for(i=2;i<=level;i++)
        {
                if( i <= time - glass )
                        glass = glass + i;
                else break;
        }
        if(tot_glass>time)
        {
                time = time-glass;
                glass = glass + ((time+1)/i);
        }
        //      printf("(i-th)%d (remain_time)%d (full_glass)%d \n",i,time,glass);
        printf("%d\n",glass);
        return 0;
}

#include <stdio.h>
int main(void)
{
        int level, time, tot_glass;
        int i, glass;
        scanf("%d%d",&level,&time);
        if(time ==0 )
        {
                printf("0\n");
        }
        else
        {
                tot_glass = (level*(level+1))/2;
                glass = 1;
                for(i=2;i<=level;i++)
                {
                        if( i <= time - glass )
                                glass = glass + i;
                        else break;
                }
                if(tot_glass>time)
                {
                        time = time-glass;
                        glass = glass + ((time+1)/i);
                }
                //      printf("(i-th)%d (remain_time)%d (full_glass)%d \n",i,time,glass);
                printf("%d\n",glass);
        }return 0;
}

#include <stdio.h>
int main(void)
{
        int level, time, tot_glass;
        int i, glass;
        scanf("%d%d",&level,&time);
        tot_glass = (level*(level+1))/2;
        glass = 1;
        for(i=2;i<=level;i++)
        {
                if( i <= time - glass )
                        glass = glass + i;
                else break;
        }
        time = time-glass;
//      printf("(i-th)%d (remain_time)%d (full_glass)%d \n",i,time,glass);
        glass = glass + ((time+1)/i);
        printf("%d\n",glass);
        return 0;
}

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

int cup[1000][1000];
int main (){
  int h , t ;
  scanf("%d%d",&h,&t);
  cup[0][0] = t;
  int cnt = 0;
  for (int i = 0;i<t;i++){
    for (int j = 0;j<t;j++){
        if(cup[i][j]>=1){
          int x = cup[i][j] - 1;
          cup[i][j] = 1;
          cup[i+1][j]+= x/2;
          cup[i+1][j+1]+= x/2;
          cnt++;
        }
    }
  }
  printf("%d\n",cnt);
  return 0;
}

#include <stdio.h>

int main(void) {
	int i, j;
	int n, t;
	int amount[55] = {0}, ans;

	scanf("%d %d", &n, &t);

	while (t--) {
		amount[0] += 512;
		for (i = 0; i < n - 1; i++) {
			const int cur = i * (i + 1) / 2, next = (i + 1) * (i + 2) / 2;
			for (j = cur; j < next; j++)
				if (amount[j] > 512) {
					amount[next + j - cur] += (amount[j] - 512) / 2;
					amount[next + j - cur + 1] += (amount[j] - 512) / 2;
					amount[j] = 512;
				}
		}
	}

	ans = 0;
	for (i = 0; i < n * (n + 1) / 2; i++)
		ans += amount[i] == 512;

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

	return 0;
}

#include<stdio.h>

int main()
{
	float t;
	int n;
	float arr[15][15];
	int i,j;
	scanf("%d %f",&n,&t);
	for(i=0;i<=n;i++)
		for(j=0;j<=n;j++)
			arr[i][j] = 0;
	int flag = 0;
	if(t >= 1)
		arr[1][1] = 1;
	else
	{
		flag = 1;
	}
	double over = t;
	arr[1][1] = 1;
	i = 1;
	over = (over-1)/2;
	while(i<=n && over > 0)
	{
		for(j=1;j<=i;j++)
		{
			arr[i+1][j] += over;
			arr[i+1][j+1] += over;
		}
		over = (over-1)/2;
		if(over > 0)
		{
			for(j=1;j<=i;j++)
				arr[i][j] = 1;
		}
		i = i + 1;
	}
	int ans=0;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=i;j++)
		{
			if(arr[i][j] >= 1)
				ans = ans + 1;
		}
	}
	if(flag == 0)
		printf("%d\n",ans);
	else
		printf("0\n");
	return 0;
}

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

#define pi 3.1415926
#define rep(x,ini,end) for(x=(ini);x<(end);x++)
#define per(x,ini,end) for(x=(end-1);x>=(ini);x--)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

double a[11][11] = {0};

int main()
{
    int n, t;
    scanf("%d %d", &n, &t);

    int i, j;
    a[0][0] = t - 1;
    rep(i,1,n)
    {
        a[0][i] = a[0][i-1] / 2 - 1;
        a[i][0] = a[0][i];
    }
    rep(i,1,n)
    {
        rep(j,1,n)
        {
            a[i][j] = (a[i-1][j] + a[i][j-1])/2 - 1;
        }
    }
    int results = 0;
    rep(i,0,n)
    {
        rep(j,0,n)
        {
            if (a[i][j]>=0)
            {
                results++;
            }
        }
    }

    printf("%d", results);
    return 0;
}

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

#define pi 3.1415926
#define rep(x,ini,end) for(x=(ini);x<(end);x++)
#define per(x,ini,end) for(x=(end-1);x>=(ini);x--)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

double a[15][15] = {-1};

int main()
{
    int n, t;
    scanf("%d %d", &n, &t);

    int i, j;
    a[0][0] = t - 1;
    rep(i,1,n)
    {
        a[0][i] = a[0][i-1] / 2 - 1;
        a[i][0] = a[0][i];
    }
    rep(i,1,n)
    {
        rep(j,1,n)
        {
            a[i][j] = (a[i-1][j] + a[i][j-1])/2 - 1;
        }
    }
    int results = 0;
    rep(i,0,n)
    {
        rep(j,0,n-i)
        {
            //printf("%.1f ", a[i][j]);
            if (a[i][j]>=0)
            {
                results++;
            }
        }
        //printf("\n");
    }

    printf("%d", results);
    return 0;
}

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

#define pi 3.1415926
#define rep(x,ini,end) for(x=(ini);x<(end);x++)
#define per(x,ini,end) for(x=(end-1);x>=(ini);x--)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

double a[11][11] = {0};

int main()
{
    int n, t;
    scanf("%d %d", &n, &t);

    int i, j;
    a[0][0] = t - 1;
    rep(i,1,n)
    {
        a[0][i] = a[0][i-1] / 2 - 1;
        a[i][0] = a[0][i];
    }
    rep(i,1,n)
    {
        rep(j,1,n)
        {
            a[i][j] = (a[i-1][j] + a[i][j-1])/2 - 1;
        }
    }
    int results = 0;
    rep(i,0,n)
    {
        rep(j,0,n-i)
        {
            if (a[i][j]>=0)
            {
                results++;
            }
        }
    }

    printf("%d", results);
    return 0;
}

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

#define pi 3.1415926
#define rep(x,ini,end) for(x=(ini);x<(end);x++)
#define per(x,ini,end) for(x=(end-1);x>=(ini);x--)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

double a[15][15] = {-1};

int main()
{
    int n, t;
    scanf("%d %d", &n, &t);

    int i, j;
    a[0][0] = t - 1;
    rep(i,1,n)
    {
        a[0][i] = a[0][i-1] / 2 - 1;
        a[i][0] = a[0][i];
    }
    rep(i,1,n)
    {
        rep(j,1,n)
        {
            a[i][j] = (a[i-1][j] + a[i][j-1])/2 - 1;
        }
    }
    int results = 0;
    rep(i,0,n)
    {
        rep(j,0,n-i)
        {
            //printf("%.1f ", a[i][j]);
            if (a[i][j]>=1e-9)
            {
                results++;
            }
        }
        //printf("\n");
    }

    printf("%d", results);
    return 0;
}

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

#define pi 3.1415926
#define rep(x,ini,end) for(x=(ini);x<(end);x++)
#define per(x,ini,end) for(x=(end-1);x>=(ini);x--)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

double a[11][11] = {0};
double b[11][11] = {0};

int main()
{
    int n, t;
    scanf("%d %d", &n, &t);

    int i, j;

    rep(i,0,n)
    {
        rep(j,0,n)
        {
            if (i == 0)
            {
                a[i][j] = pow(2,j); b[i][j] = 1/a[i][j]; continue;
            }
            if (j == 0)
            {
                a[i][j] = pow(2,i); b[i][j] = 1/a[i][j]; continue;
            }
            b[i][j] = b[i][j-1]/2 + b[i-1][j]/2;
            a[i][j] = 1/b[i][j];
        }
    }

    double sum = 0;
    int results;
    int count;
    rep(i,0,n)
    {
        if (t<sum)
        {
            break;
        }
        sum = sum + a[0][i];
    }
    count = i - 1; results = (1 + count) * count / 2;
    int count2 = 0;
    rep(i,0,count+1)
    {
        rep(j,count-i,count-i+1)
        {
            if (a[i][j] + results <= t)
            {
                count2++;
            }
        }
    }
    results = results + count2;
    printf("%d",results);

    return 0;
}

#include <stdio.h>
#define G 45
float glasses[45];

void fillchildren(int pos, int level, float amount) {
    float tmp = 0;
    if (pos + level < G)
        glasses[pos+level] += amount/2.0;
    if (pos + level + 1 < G)
        glasses[pos+level+1] += amount/2.0;
    if (glasses[pos+level] > 1) {
        tmp = glasses[pos+level]-1;
        glasses[pos+level] = 1;
        fillchildren(pos+level, level+1, tmp);
    }
    if (glasses[pos+level+1] > 1) {
        tmp = glasses[pos+level+1]-1;
        glasses[pos+level+1] = 1;
        fillchildren(pos+level+1, level+1, tmp);
    }
}

int main () {
    int n , t, i, k;
    scanf("%d %d", &n, &t);
    if (t == 0)
        printf("0");
    else {
        glasses[0] += 1;
        fillchildren(0,1, t-1);
        for(i = 0; i < G; i++){
            // printf("%g ", glasses[i]);
            if (glasses[i] >= 1)
                k++;
        }
        printf("%d\n", k);
    }
}

#include <stdio.h>

int main () {
    int n , t, i, sum=0;
    scanf("%d %d", &n, &t);
    if (t == 0)
        printf("0");
    else {
        sum = 0;
        int count = 1;
        for (i = 1; i <= n+1; i++) {
            sum += i;
            if (t <= sum) {
                if (t == sum) {
                    printf("%d", sum);
                    break;
                }
                else if (t < sum) {
                    printf("%d", sum - i);
                    break;
                }
            }
            count++;
        }
    }
}

#include <stdio.h>
#define G 45
int glasses[10][10];
int powers[10];

void printmarix(int matrix[][10], int n, int m) {
    int i,j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main () {
    int n , t, i, j, volume, remaining, answer = 0;
    powers[0] = 1;
    powers[1] = 2;
    powers[2] = 4;
    powers[3] = 8;
    powers[4] = 16;
    powers[5] = 32;
    powers[6] = 64;
    powers[7] = 128;
    powers[8] = 256;
    powers[9] = 512;
    powers[10] = 1024;

    scanf("%d %d", &n, &t);
    volume = powers[n];
    glasses[0][0] = t*volume;
    for (i = 0; i < n; i++)
        for (j = 0; j < i+1; j++) {
            printmarix(glasses, 10, 10);
            if (glasses[i][j] > volume) {
                remaining = glasses[i][j] - volume;
                glasses[i][j] = volume;
                glasses[i+1][j] += remaining/2;
                glasses[i+1][j+1] += remaining/2;
            }
        }

    for (i = 0; i < n; i++)
        for (j = 0; j < i+1; j++) {
            if (glasses[i][j] == volume)
                answer++;
    }
    printf("%d\n", answer);
    return 0;
}

#include <stdio.h>
#define G 45
float glasses[45];

void fillchildren(int pos, int level, float amount) {
    float tmp = 0;
    if (pos + level < G)
        glasses[pos+level] += amount/2.0;
    if (pos + level + 1 < G)
        glasses[pos+level+1] += amount/2.0;
    if (glasses[pos+level] > 1) {
        tmp = glasses[pos+level]-1;
        glasses[pos+level] = 1;
        fillchildren(pos+level, level+1, tmp);
    }
    if (glasses[pos+level+1] > 1) {
        tmp = glasses[pos+level+1]-1;
        glasses[pos+level+1] = 1;
        fillchildren(pos+level+1, level+1, tmp);
    }
}

int main () {
    int n , t, i, k;
    scanf("%d %d", &n, &t);
    if (t == 0)
        printf("0");
    else {
        glasses[0] += 1;
        fillchildren(0,1, t-1);
        for(i = 0; i < n*(n-1)/2; i++){
            // printf("%g ", glasses[i]);
            if (glasses[i] >= 1)
                k++;
        }
        printf("%d\n", k);
    }
}

#include <stdio.h>

int main(void) {
	// your code goes here
	float arr[1000][19];
	int i,j,k,l,m,n,t;
	for(i=0;i<10;i++)for(j=0;j<19;j++){arr[i][j]=0;}

	scanf("%d%d",&n,&t);
	arr[0][10]=t;
int	c=10;int count=0;

	    int temp=1;
	    for(i=0;i<1000;i++)
	    {

	       for(j=0;j<19;j++)
	       {
	           if(arr[i][j]-1>1)
	           {float f=arr[i][j]-1;
	               arr[i+1][j-1]=arr[i+1][j-1]+f/2;
	               arr[i+1][j+1]=arr[i+1][j+1]+f/2;
	               count++;
	               if(count>=55)break;
	               //printf("\n%f",f);
	               }

	               } if(count>=55)break;

	    }

	    printf("%d",count);

	return 0;
}

#include <stdio.h>
int n,arr[10][10]={0},c=0;
float t;

void add(int x,int y,float value,int k)
{
    float diff;
    if(value==0||n==k)
    {
        return;
    }
    else if(arr[x][y]==1)
    {
        add(x+1,y,value/2,k+1);
        add(x+1,y+1,value/2,k+1);
    }
    else if(arr[x][y]+value<1)
    {
    arr[x][y]=arr[x][y]+value;
    return;
    }
    else
    {
        diff=arr[x][y]+value-1;
        arr[x][y]=1;
        c++;
        add(x+1,y,diff/2,k+1);
        add(x+1,y+1,diff/2,k+1);
    }
}

int main(void) {
    scanf("%d%f",&n,&t);
	add(0,0,t,0);
	printf("%d",c);
	return 0;
}


An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one — to the 1-st and the 3-rd ones, the 3-rd one — only to the 2-nd one. The transitions are possible only between the adjacent sections.The spacecraft team consists of n aliens. Each of them is given a rank — an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B.At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo m.

The first line contains two space-separated integers: n and m (1 ≤ n, m ≤ 109) — the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.

Print a single number — the answer to the problem modulo m.

NoteIn the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.To briefly describe the movements in the second sample we will use value , which would correspond to an alien with rank i moving from the segment in which it is at the moment, to the segment number j. Using these values, we will describe the movements between the segments in the second sample: , , , , , , , , , , , , , , , , , , , , , , , , , ; In total: the aliens need 26 moves. The remainder after dividing 26 by 8 equals 2, so the answer to this test is 2.

#include <stdio.h>

long long f(long long p, int n, int m)
{
    if (n == 1) {
        return p;
    } else if (n % 2 == 0) {
        return f(p * p % m, n / 2, m);
    } else {
        return f(p, n - 1, m) * p % m;
    }
}

int main()
{
    int n, m;
    long long x;

    scanf("%d %d", &n, &m);

    x = f(3, n, m);

    printf("%d\n", (int)((x - 1 + m) % m));

    return 0;
}

#include <stdio.h>

typedef __int64 LL;

LL F(LL A,LL N,LL M)
{
    int ret=1;
    while(N)
    {
        if(N&1)
        {
            ret=(ret%M*A%M)%M;
        }
        A=(A*A)%M;
        N>>=1;
    }
    return ret;
}

int main()
{
    LL N,M;
    scanf("%I64d %I64d",&N,&M);
    printf("%I64d\n",(F(3,N,M)+M-1)%M);
    return 0;
}

#include<stdio.h>
 long int power(long long int x,long long int n,long long int m)
  { long long int p;
     if(n==1)
     return(x%m);
     else
     { p=(power(x,n/2,m))%m;
       if(n%2==0)
        return((p*p)%m);
        else
        return((x*p*p)%m);}
        }
int main()
{
 long long int n,m,c;
   scanf("%I64d%I64d",&n,&m);
     c=power(3,n,m)%m;
     if(c==0)
     printf("%I64d",m-1);
     else
     printf("%I64d",c-1);
     return(0);
}

/*
   nash007 (Nishant Agrawal)
   IIIT Hyderabad
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

#define MAX 1000000007
#define swap(a,b) a=a+b-(b=a)
#define abs(a) (a>0)?a:(-1*a)

//a -> 97; A -> 65; 0 -> 48

int cmp(const void *a,const void *b)
{
  return (*(int*)a - *(int*)b);
}
long long int mod;
void f(long long int p)
{
  long long ans=1,n=3;
  while(p)
  {

    if(p&1)ans=(ans*n)%mod;
    n=(n*n)%mod;
    p=p>>1;
  }
  ans--;
  while(ans<0)ans+=mod;
  printf("%lld",ans);
}

int main()
{
  long long int n;
  scanf("%lld%lld",&n,&mod);
f(n);

  return 0;
}

#include<stdio.h>
long long int power(long long int x,long long int y,long long int m)
{
    long long int e;
    if(y==1)
    return(x%m);
    e=power(x,y/2,m);
    if(y%2==1)
    return((x*e*e)%m);
    else
    return((e*e)%m);
}
main()
{
    long long int n,m,e;
    scanf("%I64d%I64d",&n,&m);
    e=power(3,n,m);
    if(e==0)
    e=m-1;
    else
    e--;
    printf("%I64d",e);
    return(0);
}

#include<stdio.h>
long long MOD;
long long int pow(long long int a,long long int b)
{
    long long int x=1,y=a;
    while(b > 0LL)
    {
        if(b%2 == 1)
        {
            x=(x*y);
            if(x>MOD) x%=MOD;
        }
        y = (y*y);
        if(y>MOD) y%=MOD;
        b /= 2;
    }
    return x;
}
int main()
{
    long long int n,i,ans;
    scanf("%lld%lld",&n,&MOD);
    ans=pow(3,n);
    if(ans)
        printf("%lld",ans-1);
    else
        printf("%lld",MOD-1);
    return 0;
}

#include <stdio.h>

long long power(long long x, long long p, long long mod){
  if (p == 0ll)
    return 1ll;
  long long a = power(x, p / 2ll, mod);
  long long res = a * a;
  res %= mod;
  if (p % 2)
    res *= x;
  res %= mod;
  return res;
}

int main(int argc, char** argv) {
  long long n, m;

  scanf("%I64d%I64d", &n, &m);
  printf("%I64d\n", (power(3ll, n, m) + m - 1) % m);

  return 0;
}

#include<stdio.h>
int power(int n,int x)
{
	long long ans=1,mult;
	mult=3;
	while(n)
	{
		if(n&1)ans=(ans*mult)%x;
		mult=(mult*mult)%x;
		n/=2;
	}
	if(ans)
	return (int)ans-1;
	else return x-1;
}
int main()
{
	int n,x,i;
//	long long ans=3;
	scanf("%d%d",&n,&x);

//	for(i=1;i<n;i++)
//		ans =(ans*3)%x;
	printf("%d",power(n,x));
	return 0;
}

#include <stdio.h>
#include <math.h>

int n,m;

int modpow(int d,int x,int M){
    long long res=1,temp=d;
    while(x){
        if(x&1){res*=temp; res%=M;}
        temp=(temp*temp)%M;
        x>>=1;
    }
    return (int)res;
}

main(){
    scanf("%d%d",&n,&m);
    int ans = (modpow(3,n,m)+m-1)%m;
    printf("%d\n",ans);
    return 0;
}

#include <stdio.h>

int main()
{
	int n, m, res, t;
	scanf("%d%d", &n, &m);
	for ( res = 1, t = 3; n; n >>= 1 )
	{
		if ( n & 1 )
			res = res * (long long) t % m;
		t = t * (long long) t % m;
	}
	if ( --res < 0 )
		res += m;
	printf("%d\n", res);
	return 0;
}

#include<stdio.h>

int m;

int make3pow(int n){
  unsigned long long tmp,prod=1;
  int i,j;
  for(i=0;(n>>i)>0;i++){
    if(((n>>i)&1)){
      tmp=3;
      for(j=0;j<i;j++){
	tmp*=tmp;
	tmp%=m;
      }
    prod*=tmp;
    prod%=m;
    }
  }
  return prod%m;
}

int main(void){
  int n,k;
  scanf("%d %d",&n,&m);
  k=(int)make3pow(n);
  printf("%d\n",k?(k-1):(m-1));
  return 0;
}

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SORT(a,n) qsort(a,n,sizeof(int),intcmp)
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%I64d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)
#define fill(a,v)                   memset(a, v, sizeof(a))
int intcmp(const void *f,const void *s)
{
   return (*(int *)f-*(int *)s);
}
int gcd(int a,int b){ return ((b==0)?a:gcd(b,a%b));}

#define MAX
#define MODBY 1000000007

typedef long long int lld;
typedef long double Lf;

int mypow(int a, int b,int m)
{
	long long int ans=1,mult=a;
	while(b){
		if(b&1)
			ans=(ans*mult)%m;
		mult*=mult;
                mult%=m;
		b>>=1;
	}
	return (int)ans;
}
int main()
{
   int cases;
   int i,j,n,m;
   scanf("%d",&n);
   scanf("%d",&m);
   printf("%d\n",(mypow(3,n,m)-1+m)%m);
   return 0;
}

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

long long int power(int x, int y, int m)
{
	if (y==1) return x % m;
	if (y==0) return 1;
	long long int t = power(x, y / 2, m);
	if (y & 1)
		return (((t*t) % m) * x) % m;
	else return (t*t) % m;
}

int main()
{
	int i,j,x,y,n,m;
	scanf("%d %d", &n, &m);

	long long int res = (power(3, n, m) - 1 + m) % m;
	printf("%I64d\n", res);
	return 0;
}

#include<stdio.h>

long long int modPow(long long int e, long long int n,long long int m);

int main()
{
    long long int n,m,ans;
    scanf("%I64d %I64d",&n,&m);

    ans= ((modPow(3,n,m)-1+m)%m);
    printf("%I64d",ans);

    return 0;
}

long long int modPow(long long int e, long long int n,long long int m)
{
    if(n==1)
    {
        return e;
    }
    else
    {
        long long int ans=((modPow(e,n/2,m))%m);
        if((n%2) == 0)
        return ((ans*ans)%m);
        else
        return ((ans*ans*e)%m);
    }
}

#include <stdio.h>

int main()
{
    long long int n, m, res=3, sob=1;
    scanf(" %I64d %I64d", &n, &m);
    while(1)
    {
        if(n==1)
            break;
        if(n%2>0)
        {
            sob=(sob*res)%m;
            n-=1;
        }
        res = (res*res) % m;

        n/=2;
    }
    if((sob*res)%m)
        printf("%I64d", (sob*res)%m-1);
    else
        printf("%I64d", m-1);
    return 0;
}

#include <stdio.h>

int main()
{
    long n, m;
    scanf("%ld %ld", &n, &m);
    long long ans = 1;
    long long a = 3;
    while(n)
    {
    if(n & 1)
    {
        ans *= a;
        ans %= m;
    }
    a *= a;
    a %= m;
    n >>= 1;
    }
    ans--;
    if(ans < 0)
    {
    ans += m;
    }
    printf("%lld", ans);
    return 0;
}

#include<stdio.h>
 long int power(long int n,long int m)
  { long int p;
     if(n==1)
     return(3);
     else
     { p=power(n/2,m);
       if(n%2==0)
        return((p*p)%m);
        else
        return((3*p*p)%m);}
        }
int main()
{
 long int n,m,c;
   scanf("%ld%ld",&n,&m);
     c=power(n,m);
     if(c==0)
     printf("%ld",m-1);
     else
     printf("%ld",c-1);
     return(0);
}

/*
   nash007 (Nishant Agrawal)
   IIIT Hyderabad
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

#define MAX 1000000007
#define swap(a,b) a=a+b-(b=a)
#define abs(a) (a>0)?a:(-1*a)

//a -> 97; A -> 65; 0 -> 48

int cmp(const void *a,const void *b)
{
  return (*(int*)a - *(int*)b);
}
long long int mod;
void f(long long int p)
{
  long long ans=1,n=3;
  while(p)
  {

    if(p&1)ans=(ans*n)%mod;
    n=(n*n)%mod;
    p=p>>1;
  }
  ans--;
//  while(ans<0)ans+=mod;
  printf("%lld",ans);
}

int main()
{
  long long int n;
  scanf("%lld%lld",&n,&mod);
f(n);

  return 0;
}

#include<stdio.h>
int power(int n,int x)
{
	long long ans=1,mult;
	mult=3;
	while(n)
	{
		if(n&1)ans=(ans*mult)%x;
		mult=(mult*mult)%x;
		n/=2;
	}
	return (int)ans-1;
}
int main()
{
	int n,x,i;
//	long long ans=3;
	scanf("%d%d",&n,&x);

//	for(i=1;i<n;i++)
//		ans =(ans*3)%x;
	printf("%d",power(n,x));
	return 0;
}

#include <stdio.h>

int main()
{
	unsigned n, m, res, t;
	scanf("%d%d", &n, &m);
	for ( res = t = 1; n; n >>= 1 )
	{
		t = 3 * t % m;
		if ( n & 1 )
			res = res * t % m;
	}
	printf("%d\n", (res + m - 1) % m);
	return 0;
}

#include <stdio.h>

int main()
{
	int n, m, res, t;
	scanf("%d%d", &n, &m);
	for ( res = t = 1; n; n >>= 1 )
	{
		t = 3 * t % m;
		if ( n & 1 )
			res = res * t % m;
	}
	printf("%d\n", (res + m - 1) % m);
	return 0;
}

#include <stdio.h>

int main()
{
	unsigned n, m, res, t;
	scanf("%d%d", &n, &m);
	for ( res = t = 1; n; n >>= 1 )
	{
		t = 3 * t % m;
		if ( n & 1 )
			res = res * (long long) t % m;
	}
	printf("%d\n", (res + m - 1) % m);
	return 0;
}

#include<stdio.h>

int m;

int make3pow(int n){
  unsigned long long tmp,prod=1;
  int i,j;
  for(i=0;(n>>i)>0;i++){
    if(((n>>i)&1)){
      tmp=3;
      for(j=0;j<i;j++){
	tmp*=tmp;
	tmp%=m;
      }
    prod*=tmp;
    prod%=m;
    }
  }
  return prod%m;
}

int main(void){
  int n,k;
  scanf("%d %d",&n,&m);
  k=(int)make3pow(n);
  printf("%d\n",k?(k-1):((n-1)%m));
  return 0;
}

#include<stdio.h>

int m;

int make3pow(int n){
  unsigned long long tmp,prod=1;
  int i,j;
  for(i=0;(n>>i)>0;i++){
    if((n>>i)&1){
      tmp=3;
      for(j=0;j<i;j++){
	tmp*=tmp;
	tmp%=m;
      }
    }
    prod*=tmp;
    prod%=m;
  }
  return prod%m;
}

int main(void){
  int n,k;
  scanf("%d %d",&n,&m);
  k=(int)make3pow(n);
  printf("%d\n",k?(k-1):(n-1));
  return 0;
}

#include <stdio.h>

int F(int A,int N,int M)
{
    int ret=1;
    while(N)
    {
        if(N&1)
        {
            ret=(ret%M*A%M)%M;
        }
        A=(A%M*A%M)%M;
        N>>=1;
    }
    return ret;
}

int main()
{
    int N,M,Ans;
    scanf("%d %d",&N,&M);
    Ans=(F(3,N,M)-1)%M;
    if(Ans<0)
    {
        Ans+=M;
    }
    printf("%d\n",Ans);
    return 0;
}

#include <stdio.h>

int F(int A,int N,int M)
{
    int ret=1;
    while(N)
    {
        if(N&1)
        {
            ret=(ret%M*A%M)%M;
        }
        A=(A%M*A%M)%M;
        N>>=1;
    }
    return ret;
}

int main()
{
    int N,M,Ans;
    scanf("%d %d",&N,&M);
    Ans=(F(3,N,M)-1%M)%M;
    if(Ans<0)
    {
        Ans+=M;
    }
    printf("%d\n",Ans);
    return 0;
}

#include <stdio.h>

int modExp(int n,int p,int m)
{
    int k=1;
    n%=m;
    while(p!=1)
    {
        if(0!=(p&1))
            k=(k*n)%m;
        n=(n*n)%m;
        p>>=1;
    }
    return(n*k)%m;
}

int main()
{
    int N,M,Ans;
    scanf("%d %d",&N,&M);
    Ans=(modExp(3,N,M)-1%M)%M;
    if(Ans<0)
    {
        Ans+=M;
    }
    printf("%d\n",Ans);
    return 0;
}

#include <stdio.h>

int modExp(int n,int p,int m)
{
    int k=1;
    n%=m;
    while(p!=1)
    {
        if(0!=(p&1))
        {
            k=(k*n)%m;
        }
        n=(n*n)%m;
        p>>=1;
    }
    return(n*k)%m;
}

int main()
{
    int N,M,Ans;
    scanf("%d %d",&N,&M);
    Ans=(modExp(3,N,M)-1)%M;
    if(Ans<0)
    {
        Ans+=M;
    }
    printf("%d\n",Ans);
    return 0;
}

#include <stdio.h>

int modExp(int n,int p,int m)
{
    int k=1;
    n%=m;
    while(p!=1)
    {
        if(0!=(p&1))
        {
            k=(k*n)%m;
        }
        n=(n*n)%m;
        p>>=1;
    }
    return(n*k)%m;
}

int main()
{
    int N,M;
    scanf("%d %d",&N,&M);
    printf("%d\n",modExp(3,N,M)-1);
    return 0;
}

#include <stdio.h>

int modExp(int n,int p,int m)
{
    int k=1;
    n%=m;
    while(p!=1)
    {
        if(0!=(p&1))
        {
            k=(k*n)%m;
        }
        n=(n*n)%m;
        p>>=1;
    }
    return(n*k)%m;
}

int main()
{
    int N,M;
    scanf("%d %d",&N,&M);
    printf("%d\n",(modExp(3,N,M)-1)%M);
    return 0;
}

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

int main()
{
	int i,j,x,y,n,m;
	scanf("%d %d", &n, &m);

	long long int res = ((((n * n) % m) * n) % m - (n / 3) % m + m) % m;
	printf("%I64d\n", res);
	return 0;
}

#include <stdio.h>

int main()
{
	int n, m, res=3, sob=1;
	scanf(" %d %d", &n, &m);
	while(1)
	{
		if(n==1)
			break;
		if(n%2>0)
		{
			sob=(sob*res)%m;
			n-=1;
		}
		res = (res*res) % m;

		n/=2;
	}
	if((sob*res)%m)
		printf("%d", (sob*res)%m-1);
	else
		printf("%d", m-1);
	return 0;
}

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

long long m;

long binpow (long long a, long long n)
{
    long long res = 1;
    while (n)
    {
        if (n & 1)
        {
            res *= a;
            res %= m;
        }
        a *= a;
        a %= m;
        n >>= 1;
    }
    return res;
}

int main()
{
    long long n;
    scanf("%lld %lld", &n, &m);
    printf("%lld", binpow(3, n) - 1);
    return 0;
}

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

long long m;

long binpow (long long a, long long n)
{
    long long res = 1;
    while (n)
    {
        if (n & 1)
        {
            res *= a;
            res %= m;
        }
        a *= a;
        a %= m;
        n >>= 1;
    }
    return res;
}

int main()
{
    long long n;
    scanf("%lld %lld", &n, &m);
    printf("%lld", (binpow(3, n) + m - 1) % m);
    return 0;
}

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

long long m;

long long binpow (long long a, long long n)
{
    long long res = 1;
    while (n)
    {
        if (n & 1)
        {
            res *= a;
            res %= m;
        }
        a *= a;
        a %= m;
        n >>= 1;
    }
    return res;
}

int main()
{
    long long n;
    scanf("%lld %lld", &n, &m);
    long long ans = binpow(3, n) - 1;
    printf("%lld", (ans < 0) ? (ans + m) : ans );
    return 0;
}

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

long long m;

long binpow (long long a, long long n)
{
    long long res = 1;
    while (n)
    {
        if (n & 1)
        {
            res *= a;
            res %= m;
        }
        a *= a;
        a %= m;
        n >>= 1;
    }
    return res;
}

int main()
{
    long long n;
    scanf("%lld %lld", &n, &m);
    long long ans = binpow(3, n) - 1;
    printf("%lld", (ans < 0) ? ans + m : ans );
    return 0;
}