Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 8171 | Accepted: 2456 | |
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
Source
思路:对于 n 个孩子 ,最后拿到最多的糖果就是 小于等于 n 的最大 反素数。但是 是哪个孩子并不知道,那么就要进行模拟
对于 k 位置的 孩子,他的 数字是 +num 那么因为他自己本身是要被踢走的,所以相对位置 为k= k+num-1
如果数字是 -num,那么按正着数就没影响,k=k-num。线段树存储当前区间共有多少个人,每一次找到第k (前面有k-1个)个孩子,经过的区间都要 -1,然后记录被踢走的孩子编号
求解原始序号时维护一棵线段树,每个节点为该区间段的人数,则要在(l,r)区间踢出第p个人,若p小于等于tree[rt<<1],则在左半区间踢第p个人,否则在右半区间踢第(p-tree[rt<<1])个人(tree[rt<<1]为左半区间人数),如此向下处理,直到踢出该人。#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <iomanip> using namespace std; //#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1|1 #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) #define mk make_pair const int inf = 1 << 30; const int MAXN = 500010 + 50; const int maxw = 30000 + 20; const int MAXNNODE = 1000 +10; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 20071027 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pii; const D ee = 2.718281828459; const int antiprime[]={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,665280}; const int factorNum[]={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,224}; int tree[MAXN << 2]; struct Node { int val; char name[20]; }c[MAXN]; void build(int l , int r , int rt) { tree[rt] = r - l + 1; if(l == r)return ; int mid = (l + r) >> 1; build(lson) , build(rson); } int update(int p , int l , int r , int rt) { tree[rt]--; if(l == r)return l; int mid = (l + r) >> 1; if(p <= tree[rt << 1])return update(p , lson); else return update(p - tree[rt << 1] , rson); } int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge int n , k , &MOD = tree[1]; while(scanf("%d%d" , &n , &k)== 2) { FORR(i , 1 , n)scanf("%s%d" , c[i].name , &c[i].val); build(1 , n , 1); int cnt = 0; while(cnt < 35 && antiprime[cnt] <= n)cnt++; cnt--;///最大反素数 int pos = 0; c[pos].val = 0; FOR(i ,0 , antiprime[cnt]) { if(c[pos].val > 0)k = ((k + c[pos].val - 2) % MOD + MOD) % MOD + 1; else k = ((k + c[pos].val - 1) % MOD + MOD) % MOD + 1; pos = update(k , 1 , n , 1); } printf("%s %d\n" , c[pos].name , factorNum[cnt]); } return 0; }