poj 2886 线段树+反素数
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 12744 | Accepted: 3968 | |
Case Time Limit: 2000MS |
Description
N children are sitting in a circle to play a game.
The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the next child will be the (−A)-th child to the right.
The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets the most candies?
Input
Output
Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first.
Sample Input
4 2
Tom 2
Jack 4
Mary -1
Sam 1
Sample Output
Sam 3
/*
poj 2886 线段树+反素数
反素数:
(1)给定一个数,求一个最小的正整数,使得的约数个数为
(2)求出中约数个数最多的这个数 反素数以前也大概学习过,但是做这个题的时候并没有想到优化方法
问题很严重- - 给你n个数围成一圈,然后每个人对应一个数字
如果x是正数,则下一个人是左边第x个。如果x为负数,则下一个人是
右边第-x个 大致思路是没啥问题,但是开始是将所有数进行预处理,求出第i个数
的f[i],然后同过线段树判断左右两边的剩余人数,然后TLE 后来发现别人都是先反素数打表,rprim[0]表示有rprim[1]个因子时的最
大值,然后便能1-n中f[]最大值p的位置。然后只需从1模拟到p求出这时对
应的人即可 hhh-2016-03-23 23:50:13
*/
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn = 500550;
int rprim[35][2] =
{
498960,200,332640,192,277200,180,221760,168,166320,160,
110880,144,83160,128,55440,120,50400,108,45360,100,
27720,96,25200,90,20160,84,15120,80,10080,72,
7560,64,5040,60,2520,48,1680,40,1260,36,
840,32,720,30,360,24,240,20,180,18,
120,16,60,12,48,10,36,9,24,8,
12,6,6,4,4,3,2,2,1,1
};
struct node
{
int l,r;
int num;
int mid()
{
return ((l+r)>>1);
};
} tree[maxn<<2]; void update_up(int i)
{
tree[i].num = tree[lson].num + tree[rson].num;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r; if(l == r)
{
tree[i].num = 1;
return ;
}
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
update_up(i);
} void update_down(int i)
{ }
int cur;
void Insert(int i,int k)
{
if(tree[i].l == tree[i].r)
{
cur = tree[i].l;
tree[i].num = 0;
return ;
}
update_down(i);
int mid = tree[i].mid();
if(k <= tree[lson].num)
Insert(lson,k);
else
Insert(rson,k-tree[lson].num);
update_up(i);
} int query(int i,int l,int r)
{
if(tree[i].l >= l && tree[i].r <= r)
return tree[i].num;
update_down(i);
int mid = tree[i].mid();
int all = 0;
if(l <= mid)
all += query(lson,l,r);
if(r > mid)
all += query(rson,l,r);
return all;
} char nam[maxn][13];
int a[maxn]; int main()
{
int k,n,t,m,nex;
while(scanf("%d%d",&n,&k) != EOF)
{
t = 0;
while(n < rprim[t][0])
t++;
int x = rprim[t][0];
for(int i = 1; i <= n; i++)
{
scanf("%s%d",nam[i],&a[i]);
}
build(1,1,n);
cur = k,m = n;
for(int i = 1; i < x; i++)
{
//cout << cur << endl;
Insert(1,cur);
//cout << cur <<endl;
nex = a[cur];
m--;
if(nex%m == 0)
{
if(nex < 0) nex = 1;
else nex = m;
}
else
{
nex %= m;
if(nex < 0) nex += (m+1);
}
int leftnum = query(1,1,cur);
int rightnum = m - leftnum;
if(nex <= rightnum) cur = leftnum + nex;
else cur = nex-rightnum;
}
Insert(1,cur);
printf("%s %d\n",nam[cur],rprim[t][1]);
}
return 0;
}
poj 2886 线段树+反素数的更多相关文章
- poj 2886 (线段树+反素数打表) Who Gets the Most Candies?
http://poj.org/problem?id=2886 一群孩子从编号1到n按顺时针的方向围成一个圆,每个孩子手中卡片上有一个数字,首先是编号为k的孩子出去,如果他手上的数字m是正数,那么从他左 ...
- poj 2886 线段树的更新+反素数
Who Gets the Most Candies? Time Limit: 5000 MS Memory Limit: 0 KB 64-bit integer IO format: %I64d , ...
- POJ2886 Who Gets the Most Candies? 线段树 反素数
题意:有一群小朋友围成一个环,编号1,2,3…N.每个人手上握着一个非0的数字,首先第K个人出列,然后看他手上的数字,假设为m,则从下一个开始第m个人出列,一直如此.并设i为小于等于N的最大反素数,问 ...
- POJ 2886 线段树单点更新
转载自:http://blog.csdn.net/sdj222555/article/details/6878651 反素数拓展参照:http://blog.csdn.net/ACdreamers/a ...
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ...
- Who Gets the Most Candies?(线段树 + 反素数 )
Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
随机推荐
- Packet for query is too large (84 > -1).
windows下的resin配置连接mysql,常用的安全的做法是将数据库信息配置到conf目录下的resin.xml文件中. 因为resin连接mysql不是必须的,所以resin本身没有提供mys ...
- 原生ajax的请求函数
ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务器.发送请求.接收响应 ...
- NFS PersistentVolume - 每天5分钟玩转 Docker 容器技术(151)
上一节我们介绍了 PV 和 PVC,本节通过 NFS 实践. 作为准备工作,我们已经在 k8s-master 节点上搭建了一个 NFS 服务器,目录为 /nfsdata: 下面创建一个 PV mypv ...
- zookeeper入门系列 : 分布式事务
上一章我们了解了zookeeper到底是什么,这一章重点来看zookeeper当初到底面临什么问题?而zookeeper又是如何解决这些问题的? 实际上zookeeper主要就是解决分布式环境下的一致 ...
- JS实现页面内跳转
使用js($.ajax中)实现页面内跳转(即:描点平滑跳转)的方法(aa为跳转目的标签的id): 在网络上有很多资料所说的:animate方法我试了并不好使,不知道是啥原因,欢迎大家指正,附上网络方法 ...
- Hey,man,are you ok? -- 关于心跳、故障监测、lease机制
电话之于短信.微信的一个很大的不同点在于,前者更加及时,有更快速直接的反馈:而后面两个虽然称之为instant message,但经常时发出去了就得等对方回复,等多久是不确定的.打电话能明确知道对方在 ...
- vue-cli webpack3扩展多模块打包
场景 在实际的项目开发中会出现这样的场景,项目中需要多个模块(单页或者多页应用)配合使用的情况,而vue-cli默认只提供了单入口打包,所以就想到对vue-cli进行扩展 实现 首先得知道webpac ...
- Nginx原理和配置总结
一:前言 Nginx是一款优秀的HTTP服务器和反向代理服务器,除却网上说的效率高之类的优点,个人的切身体会是Nginx配置确实简单而且还好理解,和redis差不多,比rabbitmq好理解太多了: ...
- 新概念英语(1-69)The car race
新概念英语(1-69)The car race Which car was the winner in 1995 ? There is car race near our town every ye ...
- leetcode算法:Distribute Candies
Given an integer array with even length, where different numbers in this array represent different k ...