约瑟夫环(线性写法)

1073 约瑟夫环

  1. 1 秒
  2.  
  3. 131,072 KB
  4.  
  5. 0 分
  6.  
  7. 基础题

N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数。问最后剩下的人的编号。

例如:N = 3,K = 2。2号先出列,然后是1号,最后剩下的是3号。

 收起

输入

2个数N和K,表示N个人,数到K出列。(2 <= N, K <= 10^6)

输出

最后剩下的人的编号

输入样例

3 2

输出样例

3
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=2e5+5;

int main(){
    int n,k,s=0;
    scanf("%d %d",&n,&k);
    ko();
    printf("%d\n",s+1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/83689067