• hihocoder 1296 数论三·约瑟夫问题


    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
    ",solve(n,k));
        }
        return 0;
    }
  • 相关阅读:
    MySQL length函数
    MySQL between ... and ...
    MySQL Group By使用
    MySQL 聚合函数/分组函数
    MySQL where与like
    MySQL order by与distinct
    城市网络
    滑动窗口
    合并回文子串(NC13230)
    NC50439
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/5434675.html
Copyright © 2020-2023  润新知