<span>hihocoder 1296 数论三·约瑟夫问题</span>

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;
}

 

全部评论

相关推荐

风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
双非有机会进大厂吗
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务