POJ 1781 In Dangerジョセフリング(log(m)テンプレート)


Link:http://poj.org/problem?id=1781
In Danger
Time Limit: 1000 MS
 
メモリLimit: 30000 K
Total Submissions: 3589
 
Acceepted: 1873
Description
Flaavius Josephus and 40 fellow rebels wers trapped by the Romans.Hiscompone preferred suicide to surrender,so they ded to form a circele and to kill everry third person.His to proceed arnd the ciceders the warcent wance。so he calculated the position to be the last man standing(and then he did not comit suicide since nobody could watch) 
Weウィルconsider a variant of this“game”where every second person leaves.And of course there will be more 41 persons,for we now have computers.You have to callate the safe position.Be careful bers cause bers the provener。 
Input
The input contains several test cases.Each speciiifes a numben、denoting the number of personsparticiptititititititititititititititititing.To makes more difficult、it always has the format“xyez”with the the fith the fith the didididididididididididididiins the ins the ininins the ins the ininininininins s the the the ininininininininininininininininininininininininins the the the the theフォロワーズZERO.Whears 0<=x,y<=9,the number of zros is 0<=6.You may asume that n>0.The last test case is followed by the string 00 e 0.
Output
Asume that the participnts have serial numbers from 1 to n and that the couninininininininininstststststststststinininves.Asususume that the paparticipnts have serial numbersfrom 1 to n and that the counininininininststststststwith with persoson 1,i i 1,i i i i.ininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininininin4,1,5 and person 3 is staying alive.
Sample Input
05e0
01e1
42e0
66e6
00e0
Sample Output
3
5
21
64891137
ソurce
Ulm Local 2004
嗳include萼include葃include using namespace std;番号は0,1,2から、・・n-1//nは人数で、mは出周歩幅で、sは起起使報数位置(スタート位置は必ずしも出周しない)int Josephus(int n,int m,ints)です。    if(m=1)        return(s+n-1)%n;    for(int i=2;i<=n;i++)    {         s=(s+m)%i        //if(i=n)break;        if(s+m<i)        {             int x=(i-s)/(m-1)//s+m*x<=i+x;            if(i+x<n)            {                 i=i+x;                s=(s+m*x)%i            }             else{s=(s+m*(n-i)%)nbreak;        }     }     return;最後の一人の位置に戻る}int Jose(intn,int m,int s){    for(int i=2;i<=n;i++)        s=(s+m)%i    return;s;int main(){    char str[10];    while(scanf("%s",str)    {         int t 0=str[0]-'0';        int t 1=str[1]-'0';        int t 3=str[3]-'0';        if(t 0+t 1+t 3=0)break;        int num=1        while(t 3--)num*=10;        num=t 0*num*10+t 1*num;        //printf(「num=%d」num);        intess=Josephus(num、2、0)        //int res 1=Jose(num、2、0)        printf("%d",res+1)    }     return 0;