poj2886 Who Gets the Most Candies?
思路:
先打反素数表,即可确定因子最多的那个数。然后模拟踢人的过程确定对应的人名。模拟的过程使用线段树优化加速。
实现:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; const int MAXN = ; int antiprime[] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , };
int fac[] = {, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , }; int tree[MAXN << ], num[MAXN], n, k;
char name[MAXN][]; void build(int num, int l, int r)
{
if (l == r) { tree[num] = ; return; }
int m = l + r >> ;
build(num << , l, m);
build(num << | , m + , r);
tree[num] = tree[num << ] + tree[num << | ];
} int kick(int num, int l, int r, int x) // 在[l, r]范围内踢掉第x个人
{
if (l == r) { tree[num] = ; return l; }
int m = l + r >> , tmp = tree[num << ], ans = -;
if (tmp >= x) ans = kick(num << , l, m, x);
else ans = kick(num << | , m + , r, x - tmp);
tree[num] = tree[num << ] + tree[num << | ];
return ans;
} int main()
{
while (scanf("%d %d", &n, &k) != EOF)
{
for (int i = ; i <= n; i++) scanf("%s %d", name + i, num + i);
build(, , n);
int now = k, out = -, cnt = n;
int tmp = lower_bound(antiprime, antiprime + , n) - antiprime;
int w = antiprime[tmp] > n ? tmp - : tmp;
for (int i = ; i < antiprime[w]; i++)
{
out = kick(, , n, now);
cnt--;
if (!cnt) break;
if (num[out] > ) now = ((now + num[out] - ) % cnt + cnt) % cnt;
else now = ((now + num[out]) % cnt + cnt) % cnt;
now = !now ? cnt : now;
}
printf("%s %d\n", name[out], fac[w]);
}
return ;
}
poj2886 Who Gets the Most Candies?的更多相关文章
- POJ-2886 Who Gets the Most Candies?---线段树+约瑟夫环
题目链接: https://cn.vjudge.net/problem/POJ-2886 题目大意: N个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人(如果他手上拿的数为正数,从他左边数 ...
- POJ-2886 Who Gets the Most Candies?(线段树+模拟)
题目大意:n个小孩按顺时针站成一圈,每次会有一个小孩出队(第一个出队的小孩已知),在他出队时会指定下一个出队的小孩,直到所有的小孩全部出队游戏结束.第p个出队的小孩会得到f(p)个糖果,f(p)为p的 ...
- POJ2886 Who Gets the Most Candies? 线段树 反素数
题意:有一群小朋友围成一个环,编号1,2,3…N.每个人手上握着一个非0的数字,首先第K个人出列,然后看他手上的数字,假设为m,则从下一个开始第m个人出列,一直如此.并设i为小于等于N的最大反素数,问 ...
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- 《完全版线段树》——notonlysuccess
转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
- 【转载】完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...
随机推荐
- 【CODEFORCES】 C. Dreamoon and Strings
C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...
- C语言细节笔记1
/******************************************************************************* ——笔记 1. 函数申明的书写. 可以 ...
- ym——优化你的Java代码(新)
转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 1.面向对象的3要素. 2.面向对象开发的6大原则. 1.单一职责原则 应该有且仅有一 ...
- SpringBoot项目报错Cannot determine embedded database driver class for database type NONE
原因: Cannot determine embedded database driver class for database type NONE 这是因为spring boot默认会加载org.s ...
- 当你使用LINQ做底层时,最好设计一个工厂,不要把LINQ的动作暴露给业务层
1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: ...
- 【bzoj4401】块的计数
首先,块的大小确定的话,可以发现方案最多只有1种 然后就可以O(nsqrt(n))搞,不过会TLE 接着我们又发现,一个节点可以作一个块的根,当且仅当该节点的size能被块的大小整除 然后就可以O(n ...
- Posting array of JSON objects to MVC3 action method via jQuery ajax
Does the model binder not suport arrays of JSON objects? The code below works when sending a single ...
- list:[::5]
0-99的数列 L = [0, 1, 2, 3, ..., 99] 所有数,每5个取一个 >>> L[::5] [0, 5, 10, 15, 20, 25, 30, 35, 40, ...
- IJ:IntelliJ IDEA安装
ylbtech-IJ:IntelliJ IDEA安装 响应速度快 1.返回顶部 1. 2. 3. 4. 5. 6. 7. 2. 接受协议,激活IJ返回顶部 1. 2. 3. 4. 5. 6.0. 6. ...
- ExtJS 4 MVC Viewport和card布局
http://ext4all.com/post/a-little-bit-strange-navigation 效果图: app/view/Viewport.js Ext.define('App.v ...