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

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 500100
using namespace std; typedef long long ll;
const int prime[]= {,,,,,,,,,,,,,,,};
int n,k;
struct node
{
int l,r,num;
} tree[maxn*]; ll maxsum,bestnum;
char name[maxn][];
int a[maxn]; void getantiprime(ll num, ll k,ll sum,int limit)
{
//num:当前枚举到的数,k:枚举到的第k大的质因子;sum:该数的约数个数;limit:质因子个数上限;
int i;
ll temp;
if(sum > maxsum)
{
maxsum = sum;
bestnum = num; //如果约数个数更多,将最优解更新为当前数;
}
if(sum==maxsum && bestnum > num)
bestnum = num; //如果约数个数相同,将最优解更新为较小的数;
if(k > )
return;
temp = num;
for(i=; i<=limit; i++) //开始枚举每个质因子的个数;
{
if(temp*prime[k] > n)
break;
temp = temp * prime[k]; //累乘到当前数;
getantiprime(temp, k+, sum*(i+), i); //继续下一步搜索;
}
} void build(int i,int l,int r)//建树
{
tree[i].num=r-l+;
tree[i].l=l;
tree[i].r=r;
if(l<r)
{
int mid=(l+r)/;
build(i+i,l,mid);
build(i+i+,mid+,r);
}
} int search1(int num,int i)//查找原始序号
{
tree[i].num--;
if(tree[i].l==tree[i].r)
{
return tree[i].l;
}
if(num<=tree[i+i].num)
return search1(num,i+i);
return search1(num-tree[i+i].num,i+i+);
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
getantiprime(,,,);//找到n以内反素数最大的;
build(,,n);
for(int i=; i<=n; i++)
{
scanf("%s %d",name[i],&a[i]);
}
int id;
for(int i=; i<bestnum; i++)//约瑟夫原理
{
n--;
id=search1(k,);
if(n==) break;
if(a[id]>)
k=(k-+a[id]-)%n+;
else
k=((k-+a[id])%n+n)%n+;
}
printf("%s %lld\n",name[id],maxsum);
}
return ;
}

poj 2886Who Gets the Most Candies?的更多相关文章

  1. POJ 2886Who Gets the Most Candies?(线段树)

    POJ 2886 题目大意是说有n个人围成一圈,游戏的起点是k,每个人持有一个数字(非编号)num,每次当前的人退出圈,下一个人是他左边的第num个(也就是说下一个退出的是k+num, k可以为负数, ...

  2. POJ3159 Candies —— 差分约束 spfa

    题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submiss ...

  3. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  4. (poj)3159 Candies

    题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...

  5. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  6. 线段树(单点更新) POJ 2886 Who Gets the Most Candies?

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  7. POJ——T 3159 Candies

    http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   ...

  8. 图论--差分约束--POJ 3159 Candies

    Language:Default Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 43021   Accep ...

  9. poj 3159 Candies 差分约束

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22177   Accepted: 5936 Descrip ...

随机推荐

  1. DataGridView绑定BindingList<T>带数据排序的类

    本文章转载:http://yuyingying1986.blog.hexun.com/30905610_d.html DataGridView绑定List<T>类时候,不会自动的进行排序. ...

  2. 漫步支持向量机(svm)之一

    设输入为$x$,表示训练集的特征向量,输出为$y=\{1,-1\}$,这些向量都属于两类中的其中一类,假设这些向量是线性可分的,现在要找一个最优的平面(在二维的时候为一条直线),将这些特征向量正确分类 ...

  3. [Javascrip] Logging Timing Data to the Console

    Learn to use console.time with console.timeEnd to get accurate timings of operations in javascript. ...

  4. Qt 学习之路:存储容器

    存储容器(containers)有时候也被称为集合(collections),是能够在内存中存储其它特定类型的对象,通常是一些常用的数据结构,一般是通用模板类的形式.C++ 提供了一套完整的解决方案, ...

  5. c++逆向 vector

    最近弄Android c/c++方面的逆向,发现c++的类,stl模板,在逆向的时候相比c语言都带来了不小的困难. 今天自己写了个小程序,然后逆向分析了一下 vector<int> arr ...

  6. NSIndexPath初始化

    在UITableView中经常用到这个类,但一直不知道怎么初始化,网上抄录的代码如下,果然好用 NSIndexPath *index = [NSIndexPath indexPathForRow:0 ...

  7. centos 安装nginx

    centos 安装nginx 安装依赖 更换源 yum install http://mirrors.163.com/centos/6.8/extras/x86_64/Packages/epel-re ...

  8. My way to Python - Day05 - 面向对象

    思维导图

  9. Rx 入门 示例

    首先写一个观察者模式 public interface Watcher { public void update(String str); } public class ConcreteWatcher ...

  10. Android中的BroadCast静态注册与动态注册

    1.静态注册 新建MyBroadcast类继承BroadcastReceiver,实现onReceive方法 /** * Author:JsonLu * DateTime:2015/9/21 16:4 ...