[NUDT2018银河之光ACM程序设计竞赛初赛] G: T28128 Josephus problem

题目描述

In computer science and mathematics, the Josephus problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game.People are standing in a circle waiting to be executed. Counting begins at a specified point in the circle and proceeds around the circle in a specified direction. After a specified number of people are skipped, the next person is executed. The procedure is repeated with the remaining people, starting with the next person, going in the same direction and skipping the same number of people, until only one person remains, and is freed.The problem — given the number of people, starting point, direction, and number to be skipped — is to choose the position in the initial circle to avoid execution. ——Wikipedia.

    But now, you are not going to solve normal Josephus Problem. We change the skipping number into a dynamic number which means each round the number will change. For convenience, the skipping number t=f(x). x is the round number(starts at 1), and f is a linear function.

输入输出格式

输入格式:

There are many test cases.For each case, there are three numbers in a line.n,k,b. n is the number of people(numbered from 1 to n). k and b represent the linear function.1<n<1000,0<=k<50,0<=b<100

输出格式:

For each case print your answer.

输入输出样例

输入样例#1: 复制
8 1 0
输出样例#1: 复制
8




题意:一个约瑟夫环问题的变种。比原版约瑟夫环,只是多了个更新d(出队标号)的步骤。需要注意的是,题目里的round,是每出队一个人加一,而不是喊一圈加一。

关于原版约瑟夫含问题的详解如下: 点击打开链接  @ tingyun_say


下面是本题AC代码:
#include<bits/stdc++.h>
using namespace std;
/*
    @第七只狐狸
*/
int main()
{
    int n,k,b;
    while(cin>>n>>k>>b)
    {
        int round=n-1,ans=0;
        int d=round*k+b;
        for(int i=2;i<=n;i++)
        {
            ans=(ans+d)%i;
            //更新d;因为是从后往前逆推的,所以是round--
            round--;
            d=round*k+b;
        }
        cout<<ans+1<<endl;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值