Saturday, 4 August 2012

IN DANGER


Saturday, 4 August 2012

 IN DANGER SPOJ 

Problem Set (classical) 1786.

 In Danger Problem code: DANGER Flavius Josephus and 40 fellow rebels were trapped by the Romans. His companions preferred suicide to surrender, so they decided to form a circle and to kill every third person and to proceed around the circle until no one was left. Josephus was not excited by the idea of killing himself, so he calculated the position to be the last man standing (and then he did not commit suicide since nobody could watch). We will consider a variant of this "game" where every second person leaves. And of course there will be more than 41 persons, for we now have computers. You have to calculate the safe position. Be careful because we might apply your program to calculate the winner of this contest! Input Specification The input contains several test cases. Each specifies a number n, denoting the number of persons participating in the game. To make things more difficult, it always has the format "xyez" with the following semantics: when n is written down in decimal notation, its first digit is x, its second digit is y, and then follow z zeros. Whereas 0<=x,y<=9, the number of zeros is 0<=z<=6. You may assume that n>0. The last test case is followed by the string 00e0. Output Specification For each test case generate a line containing the position of the person who survives. Assume that the participants have serial numbers from 1 to n and that the counting starts with person 1, i.e., the first person leaving is the one with number 2. For example, if there are 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 is staying alive. Sample 
Input 
05e0 
01e1 
42e0 
66e6 
00e0 
Sample Output 
21 
64891137


SOLUTION-------------------------------------------------------------------------

#include
#include
int function(long long int);
int main(){        
int x,y,z;        
char str[6];        
scanf("%s",str);        
x=str[0]-'0';        
y=str[1]-'0';        
z=str[3]-'0';        
//printf("%d %d %d",x,y,z);        
long long int temp,value,value1,value2,k,ans;                 while(x!=0||y!=0||z!=0)        
 {                 
temp=pow(10,z);                 
value=(x*10+y)*temp;                 
value1=function(value);                 
value2=value-value1;                 
ans=value2*2+1;                 
printf("%lld\n",ans);                                  scanf("%s",str);                 
x=str[0]-'0';                 
y=str[1]-'0';                 
z=str[3]-'0';           
  }         
return 0; }
       
int function( long long int x)    
{    
long long int y,p,h=1;    
p=pow(2,h);    
while(p<=x)    
{   
 h=h+1;    
p=pow(2,h);    
}    
y=pow(2,h-1);        
return y;    

No comments: