题目链接: https://vjudge.net/problem/POJ-2886
题目大意:
N 个人围成一圈第一个人跳出圈后会告诉你下一个谁跳出来跳出来的人 (如果他手上拿的数为正数, 从他左边数 x 个, 反之, 从他右边数 x 个), 如果一个人是第 i 个跳出来的, 他所得糖数为 i 的所有因子个数, 求最先出序列且得到糖最多的那个人的名字和糖的数量.
分析:
最先出序列且得到糖最多的那个人的跳出序号就是不大于 N 的最大反素数, 由于此题数据量不大, 可以自行打表. 然后就是模拟跳出圈子操作, 与 POJ2828 如出一辙.
代码如下:
- #pragma GCC optimize("Ofast")
- #include <bits/stdc++.h>
- using namespace std;
- #define INIT() std::iOS::sync_with_stdio(false);std::cin.tie(0);
- #define Rep(i,n) for (int i = 0; i <(n); ++i)
- #define For(i,s,t) for (int i = (s); i <= (t); ++i)
- #define rFor(i,t,s) for (int i = (t); i>= (s); --i)
- #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
- #define rForLL(i, t, s) for (LL i = LL(t); i>= LL(s); --i)
- #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
- #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
- #define pr(x) cout <<#x << "=" << x << " "
- #define prln(x) cout << #x << "=" << x << endl
- #define LOWBIT(x) ((x)&(-x))
- #define ALL(x) x.begin(),x.end()
- #define INS(x) inserter(x,x.begin())
- #define ms0(a) memset(a,0,sizeof(a))
- #define msI(a) memset(a,inf,sizeof(a))
- #define msM(a) memset(a,-1,sizeof(a))
- #define MP make_pair
- #define PB push_back
- #define ft first
- #define sd second
- template<typename T1, typename T2>
- istream &operator>>(istream &in, pair<T1, T2> &p) {
- in>> p.first>> p.second;
- return in;
- }
- template<typename T>
- istream &operator>>(istream &in, vector<T> &v) {
- for (auto &x: v)
- in>> x;
- return in;
- }
- template<typename T1, typename T2>
- ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
- out <<"[" << p.first << "," << p.second << "]" << "\n";
- return out;
- }
- typedef long long LL;
- typedef unsigned long long uLL;
- typedef pair< double, double> PDD;
- typedef pair<int, int> PII;
- typedef pair<string, int> PSI;
- typedef set<int> SI;
- typedef vector<int> VI;
- typedef map<int, int> MII;
- typedef pair<LL, LL> PLL;
- typedef vector<LL> VL;
- typedef vector<VL> VVL;
- const double EPS = 1e-10;
- const int inf = 1e9 + 9;
- const LL mod = 1e9 + 7;
- const int maxN = 5e5 + 7;
- const LL ONE = 1;
- const LL evenBits = 0xaaaaaaaaaaaaaaaa;
- const LL oddBits = 0x5555555555555555;
- int N, K;
- PSI player[maxN];
- #define lson l , mid , rt <<1
- #define rson mid + 1 , r , rt << 1 | 1
- struct SegmentTree{
- int st[maxN << 2];
- inline void pushUp(int rt) {
- st[rt] = st[rt << 1] + st[rt << 1 | 1];
- }
- inline void pushDown(int rt) { }
- inline void build(int l, int r, int rt) {
- if(l>= r) {
- st[rt] = 1;
- return;
- }
- int mid = (l + r)>> 1;
- build(lson);
- build(rson);
- pushUp(rt);
- }
- // 把相应位置设为 0
- inline void update(int x, int l, int r, int rt) {
- if(l>= r) {
- st[rt] = 0;
- return;
- }
- int mid = (l + r)>> 1;
- if(x <= mid) update(x, lson);
- else update(x, rson);
- pushUp(rt);
- }
- // 查找从 1 开始和为 x 的区间 [1, r] 的右端点 r
- inline int querySum(LL x, int l, int r, int rt) {
- if(l>= r) return r;
- int mid = (l + r)>> 1;
- if(st[rt <<1]>= x) return querySum(x, lson);
- else return querySum(x - st[rt <<1], rson);
- }
- };
- SegmentTree segTr;
- // 反素数与对应因子个数
- int antiPrimes[36] = {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,500001};
- int factor[36] = {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,1314521};
- int main(){
- INIT();
- while(cin>> N>> K) {
- int ans = upper_bound(antiPrimes, antiPrimes + 36, N) - antiPrimes - 1;
- // 第 antiPrimes[ans] 个人能拿到最多糖
- int newN = N;
- For(i, 1, N) cin>> player[i];
- segTr.build(1, N, 1);
- int tmp;
- while(antiPrimes[ans]--) {
- tmp = segTr.querySum(K, 1, N, 1);
- segTr.update(tmp, 1, N, 1);
- // 计算下一个 K
- K += player[tmp].sd;
- if(player[tmp].sd> 0) --K;
- if(--newN == 0) break;
- --K;
- K = ((K % newN) + newN) % newN;
- ++K;
- }
- cout << player[tmp].ft << " " << factor[ans] << endl;
- }
- return 0;
- }
- View Code
来源: http://www.bubuko.com/infodetail-3043493.html