1107: Josephus
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 35 Solved: 12
[ Submit][ Status][ Web Board]
Description
Josephus problem is named after Flavius Josephus, a Jewish historian living in the
1st century. According to Josephus' account of the siege of Yodfat, he and his 40
soldiers were trapped in a cave by Roman soldiers. They chose suicide over
capture, and settled on a serial method of committing suicide by drawing lots.
Josephus states that by luck or possibly by the hand of God, he and another man
remained until the end and surrendered to the Romans rather than killing
themselves. This is the story given in Book 3, Chapter 8, part 7 of Josephus' The
Jewish War (writing of himself in the third person).
In this case, we simplify the problem that the prisoners are label from 1 to n, in each
circle, we will kill every second remaining person until one survived. We want to you
to output the number label of remaining person.
Input
First line contains a single integer t, indicates the number of cases.
Followed by T lines indicates t strings contains of less than 100 characters. Indicates the binary
form of n.
form of n.
Output
For each test case output a single line indicates the label of the lucky man!
Sample Input
2
1010
10
Sample Output
5
1
HINT
Source
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define N 100;
char person[100];
long Bigsum[10]={0};
int main(){
int t,length,i,j;
cin>>t;
getchar();
while(t--){
for(i=0;i<10;i++){
Bigsum[i]=0;
}
gets(person);
length=strlen(person);
//printf("%d,%d,%d,%d\n",person[0]-'0',person[1]-'0','0','1');
j=0;
for(i=1;i<length;i++){
for(j=0;j<9;j++){
Bigsum[j]*=2;
//printf("chen%d\n",Bigsum[j]);
}
Bigsum[0]+=(person[i]-'0');
//printf("000%d\n",Bigsum[0]);
for(j=0;j<9;j++){
while(Bigsum[j]>=100000000){
//printf("Jin%d\n",Bigsum[j]);
Bigsum[j]-=100000000;
Bigsum[j+1]+=1;
}
}
}
for(j=0;j<9;j++){
Bigsum[j]*=2;
}
Bigsum[0]+=(person[0]-'0');
for(j=0;j<9;j++){
while(Bigsum[j]>=100000000){
Bigsum[j]-=100000000;
Bigsum[j+1]+=1;
}
}
//printf("%d\n",Bigsum[0]);
for(j=9;j>=0&&Bigsum[j]==0;j--){
//printf("%d",Bigsum[i]);
}
printf("%d",Bigsum[j--]);
for(;j>=0;j--)printf("%08d",Bigsum[j]);
printf("\n");
}
}