首页 > 代码库 > Poj2886Who Gets the Most Candies?线段树
Poj2886Who Gets the Most Candies?线段树
约瑟夫环用线段数搞,一脸搞不出来的样子。反素数,太神了,先打表,然后就可以 O(1)找到因子数最多的。ps:哎。这题也是看着题解撸的。
#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#include<math.h>using namespace std;#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1int k;int sum[2222222];int chart[35][2] = { 498960, 200, 332640, 192, 277200, 180, 221760, 168, 166320, 160, 110880, 144, 83160, 128, 55440, 120, 50400, 108, 45360, 100, 27720, 96, 25200, 90, 20160, 84, 15120, 80, 10080, 72, 7560, 64, 5040, 60, 2520, 48, 1680, 40, 1260, 36, 840, 32, 720, 30, 360, 24, 240, 20, 180, 18, 120, 16, 60, 12, 48, 10, 36, 9, 24, 8, 12, 6, 6, 4, 4, 3, 2, 2, 1, 1 };void up(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 = (l + r) >> 1; build(lson); build(rson); up(rt);}void update(int key, int l, int r, int rt){ if (l == r){ sum[rt] = 0; k = l; return; } int mid = (l + r) >> 1; if (key <= sum[rt << 1]) update(key, lson); else update(key - sum[rt << 1], rson); up(rt);}int ask(int L, int R, int l, int r, int rt){ if (L <= l&&r <= R) return sum[rt]; int ans = 0; int mid = (l + r) >> 1; if (L <= mid) ans += ask(L, R, lson); if (R>mid) ans += ask(L, R, rson); return ans;}char str[555555][11];int a[555555];int main(){ int n; while (scanf("%d%d", &n, &k) != EOF){ int cnt = 0; while (n<chart[cnt][0]) cnt++; int t = chart[cnt][0]; for (int i = 1; i <= n; i++){ scanf("%s%d", str[i], &a[i]); } build(1, n, 1); int m = n; int now = k; for (int i = 0; i<t - 1; i++){ update(now, 1, n, 1); m--; if (a[k] % m == 0){ if (a[k]>0) a[k] = m; else a[k] = 1; } else{ a[k] %= m; if (a[k]<0) a[k] += m + 1; } int cc = ask(1, k, 1, n, 1); int tt = m - cc; if (a[k] <= tt) now = a[k] + cc; else now = a[k] - tt; } update(now, 1, n, 1);//m 最后为0 printf("%s %d\n", str[k], chart[cnt][1]); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。