题意:中文题。
分析:约瑟夫问题,用树状数组或者线段树维护一下就行了。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=30010;
const int MAX=151;
const int mod=100000000;
const int MOD1=100000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
int n,f[N];
void add(int x,int y) {
for (;x<=n;x+=x&(-x)) f[x]+=y;
}
int getsum(int x) {
int ret=0;
for(;x;x-=x&(-x)) ret+=f[x];
return ret;
}
int ask_k(int k) {
int i,w=0;
for (i=20;i>=0;i--)
if (w+(1<<i)<=n&&f[w+(1<<i)]<k) {
k-=f[w+(1<<i)];w+=1<<i;
}
return w+1;
}
int main()
{
int i,m,w=0;
scanf("%d%d", &n, &m);
memset(f,0,sizeof(f));
for (i=1;i<=n;i++) add(i,1);
for (i=1;i<=n;i++)
if (getsum(n)-getsum(w)>=m) {
w=ask_k(getsum(w)+m);
add(w,-1);printf("%d ", w);
} else {
w=ask_k((m-getsum(n)+getsum(w)-1)%getsum(n)+1);
add(w,-1);printf("%d ", w);
}
printf("\n");
return 0;
}