湘潭市赛 Josephus Problem 线段树

http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1149

Josephus Problem

Accepted : 44Submit : 334
Time Limit : 5000 MSMemory Limit : 65536 KB

Josephus Problem

Do you know the famous Josephus Problem? There arenpeople standing in a circle waiting to be executed. The counting out begins at the first people in the circle and proceeds around the circle in the counterclockwise direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

In traditional Josephus Problem, the number of people skipped in each round is fixed, so it's easy to find the people executed in the i-th round. However, in this problem, the number of people skipped in each round is generated by a pseudorandom number generator:

x[i+1] = (x[i] * A + B) % M.

Can you still find the people executed in the i-th round?

Input

There are multiple test cases.

The first line of each test cases contains six integers 2 ≤ n ≤ 100000, 0 ≤ m ≤ 100000, 0 ≤ x[1], A, B < M ≤ 100000. The second line contains m integers 1 ≤ q[i] < n.

Output

For each test case, output a line containing m integers, the people executed in the q[i]-th round.

Sample Input

2 1 0 1 2 3


1


41 5 1 1 0 2


1 2 3 4 40


Sample Output

1


2 4 6 8 35



#include<iostream>
#include<stdio.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1


int ans[100001];
int xw[100001];
const int maxn=200000;
int w,sum[maxn<<2];


void build(int l,int r,int rt){///建立线段树

sum[rt] = r - l + 1;
if(l == r) return ;
int m = (l+r) >> 1;
build(lson);
build(rson);
}
int update(int p,int l,int r,int rt){///更新单个节点

sum[rt]--;
if(l == r) return l ;
int m = (l + r) >> 1;
if(p <= sum[rt<<1])
return update(p,lson);
else
return update(p-sum[rt<<1],rson);

}


int main()
{
int n,m,i;
int x,A,B,M;


while(scanf("%d%d%d%d%d%d",&n,&m,&x,&A,&B,&M)!=EOF)
{
build(1,n,1);
int z = 1;
for(i = 1; i <= n; i++)
{
z = ((int)x+z)%sum[1];
if(z == 0) z = sum[1];
int s = update(z,1,n,1);
ans[i] = s;
x = (int)(((__int64)x * A + B) % M);
}
for(i = 0; i < m; i++)
scanf("%d",&xw[i]);


for(i=0;i<m-1;i++)
printf("%d ",ans[xw[i]]);
if(m!=0) printf("%d",ans[xw[m-1]]);
printf("\n");
}
return 0;
}


jsf和jpa 期末大作业 <%@ page language="java" import="java.util.*,dao.*,entitybean.*" pageEncoding="utf-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <f:view> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> </style> </head> <body > <div style="position:absolute; width:100%; height:100%; z-index:-1;background-color:#2894FF;background-repeat : no-repeat;background-size:90% 90%;opacity:1"> <img src="image/re2.jpg"> <h3>人事信息管理</h3> <p style="text-align:center"> <h:outputLink value="/faces/add.jsp">添加一条</h:outputLink> <h:form> <h:dataTable value="#{loginBean.persons}" var="person" border="5" cellpadding="5" cellspacing="2"> <h:column> <f:facet name="header"> <h:outputText id="headerText1" value="职工号"/> </f:facet> <h:outputText value="#{person.id}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText2" value="姓名"/> </f:facet> <h:outputText value="#{person.name}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText3" value="性别"/> </f:facet> <h:outputText value="#{person.gender}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText4" value="年龄"/> </f:facet> <h:outputText value="#{person.age}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText5" value="国籍"/> </f:facet> <h:outputText value="#{person.address_country}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText6" value="详细地址"/> </f:facet> <h:outputText value="#{person.address_detail}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText7" value="邮编"/> </f:facet> <h:outputText value="#{person.address_zip}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText8" value="入职时间"/> </f:facet> <h:outputText value="#{person.entry_time}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText9" value="所属部门"/> </f:facet> <h:outputText value="#{person.depertment}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText10" value="职位"/> </f:facet> <h:outputText value="#{person.position}"/> </h:column> <h:column> <f:facet name="header"> <h:outputText id="headerText11" value="工资"/> </f:facet> <h:outputText value="#{person.salary}"/> </h:column> <h:column> <h:outputLink value="faces/edit.jsp" > <h:outputText value="编辑"></h:outputText> <f:param name="p_id" value="#{person.id}"/> </h:outputLink> </h:column> <h:column> <h:outputLink value="delete.jsp" > <h:outputText value="删除"></h:outputText> <f:param name="p_id" value="#{person.id}"/> </h:outputLink> </h:column> </h:dataTable> </h:form> </p> <img src="image/re.jpg"> </div> </body> </html> </f:view>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值