hdu5643 King's Game(约瑟夫环+线段树)
counterclockwise in a circle, in label 1,2,3...n.
The first round, the first person with label 1 counts
off, and the man who report number 1 is
out.
The second round, the next person of the person who is out in the last round counts off, and the man who report number 2 is
out.
The third round, the next person of the person who is out in the last round counts off, and the person who report number 3 is
out.
The N - 1 round, the next person of the person who is out in the last round counts off, and the person who report number n−1 is
out.
And the last man is survivor. Do you know the label of the survivor?
the number of the testcases.
For each test case, there are only one line, containing one integer n,
representing the number of players.
For each test case, print the label of the survivor.
2
3
2
Hint:
For test case #1:the man who report number $1$ is the man with label $1$, so the man with label $2$ is survivor.
For test case #1:the man who report number $1$ is the man with label $1$, so the man with label 1 is out. Again the the man with label 2 counts $1$, the man with label $3$ counts $2$, so the man who report number $2$ is the man with label $3$. At last the man with label $2$ is survivor.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 6006
int a[maxn];
void init()
{
int i,j;
a[1]=1;
for(i=2;i<=5000;i++){
int num=0;
//for(j=i-1;j>=1;j--){
for(j=1;j<=i;j++){
num=(num+i-j+1)%j;
}
a[i]=num+1;
}
}
int main()
{
int n,m,i,j,T;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%d\n",a[n] );
}
return 0;
}
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 5006
struct node{
int l,r,num;
}b[4*maxn];
int n;
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
b[th].num=r-l+1;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,th*2);
build(mid+1,r,th*2+1);
}
void update(int num,int cishu,int th)
{
int mid;
if(b[th].l==b[th].r){
b[th].num=0;
if(cishu==n)printf("%d,",b[th].l);
return;
}
if(b[th*2].num>=num){
update(num,cishu,th*2);
}
else{
update(num-b[th*2].num,cishu,th*2+1 );
}
b[th].num=b[th*2].num+b[th*2+1].num;
}
int main()
{
int t,m,i,j,T,k;
freopen("o.txt","w",stdout);
for(n=1;n<=5000;n++)
{
build(1,n,1);
int p=1;
int tot=n;
for(k=1;k<=n;k++){
p=(p+k-1)%tot; //这里算这回合杀死的人的编号的空格数
if(p==0)p=tot;
update(p,k,1);
if(p==tot)p=1; //这里算出这回合杀死人后,下一回合第一个数数的编号,也可以看做是下一回合站第几个空位
tot--; //每次总人数减1
}
}
}
hdu5643 King's Game(约瑟夫环+线段树)的更多相关文章
- HDU 5643 King's Game | 约瑟夫环变形
经典约瑟夫环 }; ; i<=n; i++) { f[i] = (f[i-] + k) % i; } 变形:k是变化的 #include <iostream> #include &l ...
- (简单) POJ 2750 Potted Flower,环+线段树。
Description The little cat takes over the management of a new park. There is a large circular statue ...
- POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
题意 n个人顺时针围成一圈玩约瑟夫游戏 每一个人手上有一个数val[i] 開始第k个人出队 若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人 val[k ...
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- cf1278D——树的性质+并查集+线段树/DFS判环
昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...
- 线段树求后继+环——cf1237D
/* 首先开三倍消环(两倍是不够的),倒序求值,线段树找一下后继即可 */ #include<bits/stdc++.h> using namespace std; #define N 3 ...
- HPU组队赛J:Ball King(线段树)
时间限制 1 Second 内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...
随机推荐
- LeetCode198--打家劫舍问题
题目 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个 ...
- JavaScript中的原型、原型链、原型模式
今天,咱来聊聊JavaScript中的原型跟原型链 原型跟原型模式 这一块的知识,主要是设计模式方面的. 首先,我们知道JavaScript是面向对象的.既然是面向对象,那它自然也有相应的类跟对象等概 ...
- node爬虫 -- 网页图片
相信大家都听说过爬虫,我们也听说过Python是可以很方便地爬取网络上的图片,但是奈何本人不会Python,就只有通过 Node 来实践一下了. 接下来看我如何 板砖 ! !!
- python3.8.1安装cx_Freeze
按照官网的提示命令python -m pip install cx_Freeze --upgrade安装,不成功,报了一个错误,说cx_Freeze找不到需要的版本,还有一些警告说PIP需要升级,没理 ...
- 第2章_神经网络入门_2-5&2-6 数据处理与模型图构建
目录 神经元的TF实现 安装 神经网络的TF实现 神经元的TF实现 安装 版本: Python 2.7 tf 1.8.0 Linux 略 demo 神经网络的TF实现 # py36 tf 2.1. # ...
- CSS响应式布局学习笔记(多种方法解决响应式问题)
在做web开发的工作中,会遇到需要我给页面根据设计的要求,进行响应式布局,这里跟大家分享下我对于响应式布局的解决方法: 我主要利用的是CSS3 媒体查询,即media queries,可以针对不同的媒 ...
- 【Windows】Win10家庭版启用组策略gpedit.msc
[前言] 大家都认为,Windows 10家庭版中并不包含组策略,其实不然,它是有相关文件的,只是不让你使用而已.那么我们让系统允许你使用就好了. [操作步骤] 1.首先你需要在桌面上新建一个txt文 ...
- apache状态显示报错AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdo...is message
今天启动apache查看状态发现报错,说不能确认服务器完全确认域名,以下是报错内容: [root@localhost ~]# service httpd status Redirecting to / ...
- U盘制作系统启动盘方法
1.下载一个UltralSO用来把CentOS系统镜像写入U盘作为启动安装盘 U盘用一个空U盘,会格式化的. 下载下来,使用试用版就行 刻录完成.
- 详解SpringMVC
详解SpringMVC 一.什么是MVC? MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范.就是将业务逻辑.数据.显示分离的方法来组织代码. ...