This problem is very strong, but also involved in number theory, but also a line tree.
The line tree maintains information about how many people in the interval did not jump out, and then calculates that the next person is the first of the remainder.
I am in this tune program for a long time, is that mold to die to make me dizzy.
But the question is really a good question, to praise.
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <string>5#include <algorithm>6#include <vector>7 using namespacestd;8 9 Const intMAXN =500000+Ten;Ten intN, K; One intSUM[MAXN <<2]; A - voidBuildintOintLintR) - { the if(L = = R) {Sum[o] =1;return; } - intM = (L + R)/2; -Build (o*2, L, M); -Build (o*2+1, m+1, R); +Sum[o] = sum[o*2] + sum[o*2+1]; - } + A intUpdateintOintLintRintp) at { - if(L = = R) {Sum[o] =0;returnL;} - intM = (L + R)/2; - intans; - if(sum[o*2] >= p) ans = update (o*2, L, M, p); - ElseAns = Update (o*2+1, m+1, R, p-sum[o*2]); inSum[o] = sum[o*2] + sum[o*2+1]; - returnans; to } + - Const intMAXP = -; the intPRIME[MAXP], pcnt =0; * BOOLVIS[MAXP]; $ Panax Notoginseng voidprime_table () - { the intm =sqrt (MAXP); + for(inti =2; I <= m; i++)if(!Vis[i]) A for(intj = i * I; J < Maxp; J + = i) vis[j] =true; the for(inti =2; i < MAXP; i++)if(!vis[i]) prime[pcnt++] =i; + } - $ intFintN) $ { - intAns =1; - for(inti =0; I < pcnt && n >1; i++)if(n% prime[i] = =0) the { - intc =0;Wuyi while(n% prime[i] = =0) {n/= prime[i]; C + +; } theAns *= C +1; - } Wu if(N >1) Ans <<=1; - returnans; About } $ - intNEXT[MAXN]; - Charstu[maxn][ One]; - A intMain () + { the //freopen ("In.txt", "R", stdin); - $ prime_table (); the the while(SCANF ("%d%d", &n, &k) = =2) the { theBuild1,0N1); - int_max =0, id; in for(inti =0; I < n; i++) {scanf ("%s%d", Stu[i], &Next[i]); } the the intpos = k-1; About for(inti =1; I <= N; i++) the { the intt = Update (1,0N1, POS +1); the intf =F (i); + if(F >_max) - { the_max =F;BayiID =T; the } the if(i = = N) Break; - intMOD = N-i; - if(Next[t] <0) next[t]++;//negative situation also have a little change, in this pit for a long time thepos = ((pos + next[t]-1)% (MoD) + MoD)%MOD; the } theprintf"%s%d\n", Stu[id], _max); the } - the return 0; the}
code June
POJ (line tree) who Gets the most candies?