标签:
Problem: 2886 User: shu_dayang Memory: 11104K Time: 1125MS Language: C++ Result: Accepted
#include<iostream> #include<cstdio> #include<cstring> #define MID(a,b) ((a + b) >> 1) using namespace std; const int MAXN = 500005; int sum[MAXN << 2]; char name[MAXN][10]; int val[MAXN]; int RPrime[]={//反素数 1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120, 20160,25200,27720,45360,50400,55440,83160,110880,166320,221760,277200,332640,498960, 554400 }; int fact[]={//反素数约数个数 1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,64,72,80,84,90,96,100,108,120,128, 144,160,168,180,192,200,216 }; void PushUp(int rt) { sum[rt] = sum[rt << 1] + sum[(rt << 1) + 1]; } void build(int l, int r, int rt) { if(l == r) { sum[rt] = 1; return; } int mid = MID(l, r); build(l, mid, rt << 1); build(mid +1, r, (rt << 1) + 1); PushUp(rt); } int update(int p, int l, int r, int rt) { int res; if(l == r) { sum[rt] --; return l; } int mid = MID(l, r); if(p <= sum[rt << 1]) res = update(p, l, mid, rt << 1); else res = update(p - sum[rt << 1], mid + 1, r, rt << 1 | 1); PushUp(rt); return res; } int main() { int N,K,&mod = sum[1]; while(~scanf("%d%d",&N,&K)) { int luck; for(int i = 0; i < N; i++) if(N < RPrime[i]) { luck = i - 1; break; } build(1,N,1); int count = 0; for(int i = 1; i <= N; i++) { scanf("%s%d",name[i],&val[i]); } int k = K; int pos = 0; for(int i=0;i<RPrime[luck];i++) { if(val[pos]>0) k=((k+val[pos]-2)%mod+mod)%mod+1; else k=((k+val[pos]-1)%mod+mod)%mod+1; pos=update(k,1,N,1); } printf("%s %d\n",name[pos],fact[luck]); } }
poj 2886 Who Gets the Most Candies?
标签:
原文地址:http://www.cnblogs.com/yong-hua/p/4665809.html