what is the cause for the formula before it?
![:oops:](./images/smilies/icon_redface.gif)
![:oops:](./images/smilies/icon_redface.gif)
Moderator: Board moderators
Code: Select all
#include<stdio.h>
#include<math.h>
main()
{
float coconuts;
unsigned long temp_c,ok,i,j,max_people;
while(scanf(" %f",&coconuts)){
if(coconuts<0) break;
temp_c=coconuts; ok=0;
max_people=(unsigned)pow(coconuts,0.5)+2;
for(i=max_people;i>1;i--){
temp_c=coconuts;
for(j=i;j>0;j--){
if((temp_c-1)%i==0){
temp_c=temp_c-(temp_c-1)/i-1;
ok=1;
}
else{
ok=0;
break;
}
}
if(ok){
printf("%.0f coconuts, %lu people and 1 monkey\n",coconuts,i);
break;
}
}
if(!ok)
printf("%.0f coconuts, no solution\n",coconuts);
}
}
how did you come up with the upper bound? that is always my problem with this type of problem (ugh...)kmhasan wrote:simulate in range sqrt(coconuts)+1 down to 2.
Code: Select all
No. of coconuts: 3
No. of people: 2
The first guy wakes up, divides the 3 coconuts into two piles and gives the remaining coconut to the monkey. So, there's only 1 coconut left when the second guy wakes up. He divides the 1 coconut into 2 piles and gives the remaining coconut to the monkey. So there are 0 coconuts left in the morning. In the morning these 0 coconut/s are divided between the 2 people with each person getting 0 coconut/s.
Code: Select all
removed after getting ac
Code: Select all
24
36
74
-1
Code: Select all
24 coconuts, no solution
36 coconuts, no solution
74 coconuts, no solution
Code: Select all
I find my mistake and got Acc...Thanks!
Code: Select all
253
509
65539
16777209
Code: Select all
Removed after AC