前言 具体分析请见http://blog.csdn.net/hnust_v/article/details/51746013
//C
#include <bits/stdc++.h>
using namespace std;
int A[50500]={};
int main()
{
//freopen("E:\\test.txt","r",stdin);
//freopen("E:\\tsst.txt","w",stdout);
int n;
while(cin>>n&&n)
{
queue<int> Que;
for(int i=1;i<=n;i++) Que.push(i);
int Count=0;
while(Que.size()!=1)
{
++Count;
if(!(Count%2)) Que.pop();
else {int a=Que.front();Que.pop();Que.push(a);}
}
printf("%d\n",Que.front());
}
return 0;
}
//D
#include <bits/stdc++.h>
using namespace std;
int Cal(int n)
{
if(n==1) return 1;
if(!(n%2)) return Cal(n/2)*2-1;
else return Cal((n-1)/2)*2+1;
}
int main(void)
{
int n;
while(cin>>n) cout<<Cal(n)<<endl;
}
//I
#include <cstdio>
using namespace std;
typedef long long LL;
LL Cal(LL n, LL m, LL k)
{
LL ans = 1;
if(m == 1) return n;
for(LL i = 1; i <= n;)
{
if((ans+m) < i)
{
LL x = (i-1-ans)%(m-1)? (i-1-ans)/(m-1): (i-1-ans)/(m-1)-1;
if(i+x > n) { ans += (n+1-i)*m; break;}
else { ans += x*m;i += x;}
}
else { ans = (ans+m)%i?(ans+m)%i:i; i++;}
}
return ans;
}
int main()
{
// freopen("f:\\test.txt","r",stdin);
long long n,m;
while(~scanf("%lld %lld",&n,&m))
printf("%lld\n",Cal(n,m,1));
}