CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(easy) » Johnny and the Beanstalk

Johnny and the Beanstalk

Problem code: MARCHA2

  • Submit
  • All Submissions

All submissions for this problem are available.

A tutorial for this problem is available here.

One evening Johnny found some funny looking beens in his grandfather's garden shed, and decided to plant one of them. Next morning, to his surprise he found an enormous beanstalk growing in his back yard. Undaunted by its size, he decided to count its leaves.

You must know that beanstalks in Byteland grow in a very special way. At the lowest (1st) level, there is exactly one stem. At any level(including the 1st), a stem can end (forming exactly one leaf), or branch into exactly two stems which grow into the next level, following the same rules.

Johnny believes he has managed to count the number of leaves at each of the levels of the beanstalk. However, you must know that before he began to count, Johnny ate one or two of the other beans he found in his grandfather's shed, and that's why he is not quite sure of his results. Please verify whether Johnny's results may possibly be correct, at least in theory.

Input

The input starts with a line containing integer t, the number of test cases (1<= t <= 20). The descriptions of exactly t test cases follow.

Each test case starts with an integer k, representing the number of levels of the beanstalk (1<= k<=106). The next k non-negative space-separated integers (not greater than 106) represent the number of leaves of the beanstalk at successive levels, starting from level 1.

Output

For each test case, output a line containing exactly one of the words 'Yes' or 'No', depending on whether a beanstalk having the stated leaf counts can grow in accordance with the Bytelandian rules.

Example

Input:
2
3
0 1 2
3
0 0 3

Output:
Yes
No


Author: admin
Date Added: 17-03-2009
Time Limit: 2 - 7 sec
Source Limit: 50001 Bytes
Languages: ADA, ASM, BASH, C, C99 strict, CPP 4.0.0-8, CPP 4.3.2, CS2, D, FORT, ICON, JAR, JAVA, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PYTH, PYTH 3.1.2, RUBY, SCM guile, SCM qobi, ST


  • Submit

Comments

  • Login or Register to post a comment.

technofreak @ 7 Jun 2009 01:29 AM

When it results in a runtime error, it would be great if I can see the error somewhere. It would be of great help to find where and why the error comes.

chang @ 28 Jun 2009 10:23 PM

Wanted to confirm : johnny can eat from any level except the last one...am i rit ?

atulbansal128 @ 2 Aug 2009 05:11 AM

we need to make sure when k==1 leaves at level1 = 1;

Kushagra: Johnny doesn't eat

dwitkowski @ 20 Aug 2009 05:46 PM
Kushagra: Johnny doesn't eat any leaves. He eats some beans which presumably affect his ability to determine if his results are correct, hence the exercise. It does not affect the results and may be disregarded.

Why does it say I "can't

dwitkowski @ 21 Aug 2009 06:25 PM
Why does it say I "can't submit an answer in this language, please try link" when I am submitting a Perl program? It's clearly marked as an accepted language for this problem.

This is fixed. Please check

admin @ 21 Aug 2009 06:34 PM

This is fixed. Please check now.

I need more test cases...

codemonk @ 21 Sep 2009 04:51 PM
I need more test cases...

Is there a trick to do this

QuickCoder @ 28 Sep 2009 06:27 AM

Is there a trick to do this in Java? BigInteger performs terribly when trying to manipulate a number 301030 digits long. That's with the simple test case of having a stalk with 999,999 levels with 0 leaves and then 2^1000000 leaves at the millionth level. It's far worse when I need to process large numbers at ever level up to the millionth level.

 

You don't need big integers

triplem @ 28 Sep 2009 06:35 AM

You don't need big integers for th.is problem in any language. See Tutorial for Johnny and the Bean Stalk for solution methods.

i have successfully submitted

abhinavabhi2 @ 24 Oct 2009 11:32 AM

i have successfully submitted this problem but my prog has runtime of 12.36sec whereas thr r other submissions havng runtime of just 0.2sec in the same language c++, so how is that possible?can anyone tell? 

after submission its showing

Zahid1991 @ 2 Nov 2009 06:50 PM

after submission its showing accepted for all testcases where max(k)=1000000 but gives wrong answer when max(k)=6 only...can any1 help me out on what could i be missing??

It says it accepts answers in

hiptobecubic @ 23 Nov 2009 12:38 AM

It says it accepts answers in C++ but refuses my submission and tells me that the it won't take answers in this language for this problem.

This is fixed. It should work

admin @ 23 Nov 2009 02:27 AM

This is fixed. It should work now.

Hi , for some reason I keep

maximbu @ 27 Nov 2009 05:02 AM

Hi ,

for some reason I keep on failing on the first test (the one with 6 levels) . Any hint about what testcase might fail my prog ?

Thanks !

same here , i keep failing

gunjanbansal @ 13 Dec 2009 09:04 PM

same here , i keep failing for 1st case with 6 levels..

any hints plz ..

 

got it at last , we were not

gunjanbansal @ 13 Dec 2009 09:18 PM

got it at last ,

we were not taking all levels as input , wich were passed on to next test cases..

I swear the people on this

Dudio @ 28 Dec 2009 12:38 AM

I swear the people on this site suck at writing descriptions for their problems.  ;o

plz

shaurya @ 22 Jan 2010 06:17 PM

plz help.

#include<stdio.h>
int main()
{
long int t,k,cur,n,i,no_of_stems,next;
int flag ;
scanf("%d",&n);
t=0;
while(t<n)
{
scanf("%d",&k);
flag=1;
i=0;
//scanf("%d",&cur);

no_of_stems=1;
while(i<k)
{
scanf("%d",&next);
if(no_of_stems<next)
{
flag=0;           
break;
}
no_of_stems=2*(no_of_stems-next);
i++;
}
/*if(flag!=0)
{
scanf("%d",&next);
if(no_of_stems*2!=next)
flag=0;                
}*/
if(no_of_stems!=0||flag==0)
{
while(i<k)
{          scanf("%d",&next);               
i++;
}
printf("No");
}
else if(flag==1)
printf("Yes");
t++;
}
return 0;
}                
why this code returns wrong answer  for max(k)=6

Please read the FAQ. Testing

triplem @ 23 Jan 2010 04:21 AM

Please read the FAQ.

Testing your code properly will reveal multiple easy to fix errors.

isn't the second test case

shubh09 @ 5 Mar 2010 04:20 PM

isn't the second test case given in the problem statement wrong?? shouldn't it give 'Yes' as the answer??

This is my line of reasoning:

1st level: 0 leaves i.e. 2 stems

2nd level: 0 leaves i.e. 2x2=4 stems

3rd level: can have maximum of 4 leaves i.e. 3 leaves is a permissible soln.

Correct me if i am wrong..

What do you plan to do with

triplem @ 5 Mar 2010 04:29 PM

What do you plan to do with the 4th stem? You can't just ignore it entirely.

hey, can somebody plz xplain

nikhila @ 24 May 2010 01:01 PM

hey, can somebody plz xplain y its not workin for max(k)=6

plz..plz

well my code is working fine

sukhmeet_23 @ 28 May 2010 03:54 PM

well my code is working fine for all except one for max(k)=100000 but how can this be...

can any one explain its not working for test case 3:

please help..!!!

Why does it say "Time Limit

DiDiDiKid @ 11 Jun 2010 03:49 AM

Why does it say "Time Limit Exceeded" on Test #6 when supposedly it only took 0.08s for my code to complete the test?

Somebody please tell me why

javadecoder @ 10 Jul 2010 12:54 AM

Somebody please tell me why am I getting a wrong answer for max(k)=6 ,

http://www.codechef.com/viewsolution/285066

i had many troubles with this

rene128x @ 12 Jul 2010 12:01 PM

i had many troubles with this one.

as you go counting the stems, it should never go below zero (even if the total is positive), that's why my correct-algorithm did not pass the 3rd test case.

Can someone help me with my

handtemple @ 5 Aug 2010 12:03 PM

Can someone help me with my code? Among the 6 test cases, it passed 4. Only Test #1 (k=6) and Test #3(k=1000000) output wrong answer. My code follows the 2nd solution the tutorial provides. Thanks in advance !!!

 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

#include <iostream>
#include <math.h>
using namespace std;

int main(){
 int cases;
 scanf("%d",&cases);
 while(cases-->0){
  long length;
  scanf("%ld",&length);
  long arr[length];
  for(long i=0;i<length;i++) {
   scanf("%ld",&arr[i]);
  }

  long stems=0;
  int no=0;
  
  if(length==1){ // only one level one leaf
   if(arr[0]==1){
    printf("Yesn");
   }
   else{
    printf("Non");
   }
   break;
  }
  if(arr[length-1]> pow(2,length-1)){ // cannot exceed max
   printf("Non");
   break;
  }
  
  for(long i=length-1;i>0;i--){
   stems=arr[i]+stems;
   if(stems%2!=0){
    printf("Non");
    no=1;
    break;
   }
   stems/=2;
  }
  if(stems==1 && no==0){
   printf("Yesn");
  }   
 }
}

 

#include <iostream>#include

handtemple @ 5 Aug 2010 12:05 PM

#include <iostream>
#include <math.h>
using namespace std;

int main(){
 int cases;
 scanf("%d",&cases);
 while(cases-->0){
  long length;
  scanf("%ld",&length);
  long arr[length];
  for(long i=0;i<length;i++) {
   scanf("%ld",&arr[i]);
  }

  long stems=0;
  int no=0;
  
  if(length==1){ // only one level one leaf
   if(arr[0]==1){
    printf("Yesn");
   }
   else{
    printf("Non");
   }
   break;
  }
  if(arr[length-1]> pow(2,length-1)){ // cannot exceed max
   printf("Non");
   break;
  }
  
  for(long i=length-1;i>0;i--){
   stems=arr[i]+stems;
   if(stems%2!=0){
    printf("Non");
    no=1;
    break;
   }
   stems/=2;
  }
  if(stems==1 && no==0){
   printf("Yesn");
  }   
 }
}

@above pow(2,length) can be a

mukulbudania @ 4 Sep 2010 08:05 AM

@above pow(2,length) can be a huge number2^(10,6)

now whats the problem with this.. i m getting wrong ans..

[code]

#include<iostream>
using namespace std;
int main()
{
int t,i,j,l,flag=0;
int *a,*x;
//a stores leaves and x stores stems
cin>>t;
a = new int[t];
x = new int[t];
for(i=0;i<t;i++)
{
cin>>l;
for(j=0;j<l;j++)
{cin>>a[j];}
x[0]=1;
for(j=1;j<l;j++)
{
x[j]=x[j-1]*2-a[j];if(x[j]<0){flag=1;break;}
}
if( flag==0 && x[l-1]==0)
cout<<"Yesn";
else cout<<"Non";
}
return 0;
}
[/code]

@ a jhey.. don't flaunt ur

devanshukashyap @ 15 Sep 2010 09:31 PM

@ a jhey.. don't flaunt ur attitude here. The world doesn't go the way u think. This is a forum for discussions only

The time and memory for my

default1130 @ 9 Oct 2010 10:46 AM

The time and memory for my submission is showing 0 and 0 is this correct?

Hello admin,   I am getting

mtk @ 21 Oct 2010 08:22 PM

Hello admin,

 

I am getting error while submitting:

:( internal error occurred in the system

what to do?

  When I attempt to submit a

neill @ 26 Nov 2010 04:18 AM

 

When I attempt to submit a haskell solution, the system refuses with "You can't submit in this language for this problem. Try link.".  The list of accepted languages includes HASK.  Thank you.

 

I resubmitted the solution

manoharsingh23 @ 15 Dec 2010 01:20 AM

I resubmitted the solution submitted by Rajesh Kumar and i got time 2.6 sec and Mem 2.5 ......... why this difference????

why do I always get answer

lethienhoa @ 17 Dec 2010 01:29 AM

why do I always get answer wrong with test levels 6 ?

Any hint?

To Admin: One thing that's

rajneesh2k10 @ 30 Dec 2010 08:35 AM

To Admin:

One thing that's conceptually shouldn't be there in this problem is- Once you have said the number of levels is 'K', then it should also be checked that there should be at least one stem reaching level 'K'. But I don't think its insured in here. You can give number of leaves at level 1 is 1 and then at rest all the levels number of leaves is 0. But does this imply that the height of the beanstalk is 'K'. No! Height of the beanstalk in this case is 1. So please look into it.

Thank you.

why this is giving wrong

abhinav8 @ 11 Feb 2011 10:01 AM

why this is giving wrong answer???????

 

 

#include<iostream>
using namespace std;
int main()
{
int t,j;
long int leaf,total_node,level;

cin>>t;
for(int i=0;i<t;++i)
{
cin>>level;
total_node=1;
int flag=1;
for(j=1;j<level;++j)//upto 2nd last level
{
cin>>leaf;
if( leaf>total_node )
{
flag=0;   
}
total_node=(total_node-leaf)*2;
}
cin>>leaf;//last level,all nodes should be leaf

if( leaf!=total_node )
{
flag=0;
}

if( 1==flag )
{
cout<<"nYES";
}
else
{
cout<<"nNO";
}
}
return 0;
}

Hi Guys, some points to

cooltodinesh @ 16 Mar 2011 01:37 AM

Hi Guys,

some points to understand this problem :

1. Stem can be converted into single leaf.

2. Stem can either be a leaf into next level else split into 2 stems

3. before starting problem, consider stem as 1 so from level 1 onwards you can do calculations like if leaf if 0 then you can make stems twice.

 

Problem description was not so clear to understand.

Why is this

janardhan7 @ 15 May 2011 01:33 PM

Why is this wrong:

#include<stdio.h>

int check(int [],int);

main()

{

int t,i;

scanf("%d",&t);

int m,lev[1000005],j;

for(i=1;i<=t;i++)

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

for(j=1;j<=m;j++)

scanf("%d",&lev[j]);

if(check(lev,m))

printf("Yesn");

else

printf("Non");

}

return 0;

}

int check(int lev[],int n)

{

if(n==1)

{ if(lev[1]==1)

return 1;

else

return 0;}

if(lev[n]%2!=0)

return 0;

int i=n;

while(i>1)

{ lev[i-1]+=lev[i]/2;

if(lev[n-1]%2!=0)

return 0;

i--;

}

if(lev[1]!=1)

return 0;

return 1;

}

Shouldn't we read all the

itreallyisme @ 23 Jun 2011 01:47 PM
Shouldn't we read all the leaves in a line even if we know in the middle that the input is wrong. I know this was given to be so somewhere at the top in the comments. However most of the submitted codes seem to not follow this. for eg:- try the following input 1 4 12 12 1 1 Most of them seem to give answer No Yes

I am getting wrong answer for

itreallyisme @ 23 Jun 2011 01:50 PM
I am getting wrong answer for the following code. It seems to fail for the 5th test case. Can someone please tell me what I am missing. http://www.codechef.com/viewsolution/582055

for Python users like me, the

cyberax @ 11 Oct 2011 05:13 AM
for Python users like me, the test data given in input to our program by the validation server are chosen in a way that AC validation seems to be impossible for bottom-to-top implementation, due to excessive execution time. Python users should consider using the top-to-bottom one instead. to prove it : bottom-to-top http://www.codechef.com/viewsolution/698921 , top-to-bottom http://www.codechef.com/viewsolution/698984 .

@admin : can u please tell me

nik_nik @ 26 Feb 2012 10:36 AM
@admin : can u please tell me y my code is nt getting accepted . wats d test case dat isnt giving correct output? my soln link is http://www.codechef.com/viewsolution/866819

All it takes to waste more

mohitter @ 14 Mar 2012 04:33 PM
All it takes to waste more than 2 hours on an easy problem is to read "Yes" as "YES" and "No" as "NO" :( Also, an input of 3 levels: 1 0 0 implies that the tree ended at the very first level, but the fact that the tree has 3 levels implies it should have a height of atleast 3 which is contradictory. Such a case is being considered correct by the given solution, please look into this or tell me if i am wrong.

I second to that!

mrdigerati @ 8 Apr 2012 07:32 PM
I second to that!

@admin...my solution

prawny92 @ 23 May 2012 11:39 PM
@admin...my solution http://www.codechef.com/viewsolution/1057454 works perfectly for 5 of the 6 test cases or dats wat m given to understand by the displayed table.....Nonetheless giving me a wrong answer... can u please look into it and comment!!..

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com