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

 

全部评论

相关推荐

Cassifa:发的字比你都多的一律视为骗子或者想白嫖压榨实习生的
点赞 评论 收藏
分享
01-02 21:17
已编辑
西安理工大学 后端
程序员小白条:项目不太重要,你的优势的算法竞赛,然后多背相关的八股文,项目可以不作为重点考虑,面试可能就简单带过项目就行了,你可以直接写简历,背项目相关的八股文就行,也不用自己做,时间紧张的情况下,性价比最高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务