首页 > 其他 > 详细

hihocoder 1296 数论三·约瑟夫问题

时间:2016-04-26 14:05:37      阅读:323      评论:0      收藏:0      [点我收藏+]

http://hihocoder.com/problemset/problem/1296

/* ***********************************************
Author        :devil
Created Time  :2016/4/26 12:57:34
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
int solve(int n,int k)
{
    if(n==1) return 0;
    if(n<k)
    {
        int ans=0;
        for(int i=2;i<=n;i++)
            ans=(ans+k)%i;
        return ans;
    }
    int ans=solve(n-n/k,k),tmp=n%k;
    if(ans<tmp) ans=ans-tmp+n;
    else ans=ans-tmp+(ans-tmp)/(k-1);
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t,n,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&k);
        printf("%d\n",solve(n,k));
    }
    return 0;
}

 

hihocoder 1296 数论三·约瑟夫问题

原文:http://www.cnblogs.com/d-e-v-i-l/p/5434675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!