约瑟夫序列 线段树 O(nlogn)

42 篇文章 1 订阅
11 篇文章 0 订阅

约瑟夫序列
我们目前最常见的几种约瑟夫求法是通过暴力模拟所执行的。就连python 的数组切片复杂度也远远达不到O(nlogn)如果情况足够糟糕,那么切片做法复杂度可以到达O(n^2)和暴力做法没什么区别。
那么我们用线段树来统计个数的方法,来记录下标。这样可以快速的查询。

Accepted Code

//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a > 9)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
#define read read()
const int N = 1e5 + 7;
ll tree[N * 4];
void push_up(int rt)
{
	tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
int Get_id(int val, int l, int r, int rt)
{
	if (l == r)return l;
	int mid = l + r >> 1;
	if (val <= tree[rt << 1])return Get_id(val, l, mid, rt << 1);
	return Get_id(val - tree[rt << 1], mid + 1, r, rt << 1 | 1);
}
void update(int pos, int l, int r, int rt)
{
	if (l == r)tree[rt] = 0;
	else
	{
		int mid = l + r >> 1;
		if (pos <= mid)update(pos, l, mid, rt << 1);
		else update(pos, mid + 1, r, rt << 1 | 1);
		push_up(rt);
	}
}
void creat(int l, int r, int rt)
{
	if (l == r)
	{
		tree[rt] = 1;
		return;
	}
	int mid = l + r >> 1;
	creat(l, mid, rt << 1);
	creat(mid + 1, r, rt << 1 | 1);
	push_up(rt);
}
queue<int>q;
int main()
{
	int n = read, m = read;
	creat(1, n, 1);
	int now = 0;
	while (tree[1])
	{
		now = (now + m) % tree[1];
		if (now == 0)now = tree[1];
		int pos = Get_id(now, 1, n, 1);
		update(pos, 1, n, 1);
		now--;
		q.push(pos);
	}
	out(q.front());
	q.pop();
	while (!q.empty())
	{
		putchar(' ');
		out(q.front());
		q.pop();
	}
	puts("");
}

By-Round Moon

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值