POJ 2886 Who Gets the Most Candies? 线段树

题目: http://poj.org/problem?id=2886

左右转的果断晕,题目不难,关键是准确的转啊转。因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了。还有利用反素数的做法,太专业了,还是暴力预处理吧。。。

 #include <stdio.h>
#include <string.h>
#include <math.h> const int MAXN = ; struct Tree_Node
{
int left, right, sum;
Tree_Node(){}
Tree_Node(int left, int right, int sum)
{
this->left = left;
this->right = right;
this->sum = sum;
}
}tree[MAXN<<]; void build(int left, int right, int step)
{
tree[step] = Tree_Node(left, right, right-left+);
if(tree[step].left == tree[step].right)
return;
int mid = left + (right-left) / ;
build(left, mid, step<<);
build(mid+, right, step<<|);
} int query(int num, int step)
{
tree[step].sum--;
if(tree[step].left == tree[step].right)
{
return tree[step].left;
}
if(num <= tree[step<<].sum)
return query(num, step<<);
else
return query(num - tree[step<<].sum, step<<|);
} int div[MAXN];
void cnt_divisible()
{
int x = sqrt(MAXN);
for(int i = ; i <= x; i++)
{
int y = MAXN / i;
for(int j = i+; j <= y; j++)
div[i*j] += ;
div[i*i]++;
}
} char name[MAXN][];
int val[MAXN];
int main()
{
cnt_divisible();
int n, k;
while(scanf("%d %d", &n, &k) != EOF)
{
build(, n, );
for(int i = ; i <= n; i++)
scanf("%s %d", name[i], &val[i]);
int ans = , num = , cnt = ;
while(n > )
{
n--;
int x = query(k, );
if(div[++cnt] > ans)
{
ans = div[cnt];
num = x;
}
if(n == )
break;
if(val[x] > )
k = (k + val[x] - ) % n + ;
else
k = ((k + val[x] - ) % n + n) % n + ;
}
printf("%s %d\n", name[num], ans);
}
return ;
}
上一篇:echarts图表第一个案例


下一篇:springMVC 访问404