【hihocoder 1296】数论三·约瑟夫问题
【题目链接】:http://hihocoder.com/problemset/problem/1296
【题意】
【题解】
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
int t,n,k;
int ysf(int n,int k)
{
if (n==1)
return 0;
if (n<k)
{
int ret = 0;
rep1(i,2,n)
ret = (ret+k)%i;
return ret;
}
int ret = ysf(n-n/k,k);
if (ret<n%k)
ret = ret-n%k+n;
else
ret = ret-n%k+(ret-n%k)/(k-1);
return ret;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> t;
while (t--)
{
cin >> n >> k;
cout <<ysf(n,k)<<endl;
}
return 0;
}