标签:
Problem Statement |
|||||||||||||
You are given an integer sequence A and an int N that is a power of 2. All elements of A are between 0 and N-1, inclusive. You can now choose an integer B which is between 0 and N-1, inclusive. This integer determines a new sequence C defined as follows: For each valid i, C[i] = (A[i] xor B). Given the sequence C, we will count the pairs of indices (i,j) such that both i<j and C[i]<C[j]. Compute and return the largest result we can obtain. You are given the int N. You are also given ints sz, A0, A1, P, Q, and R. Use the following pseudocode to generate the sequence A: A[0] = A0; A[1] = A1; for (i = 2; i < sz; i++) { A[i] = (A[i - 2] * P + A[i - 1] * Q + R) modulo N; } |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | Watch out for integer overflow when generating the sequence A. | ||||||||||||
Constraints |
|||||||||||||
- | N will be between 2 and 1,073,741,824 (2^30), inclusive. | ||||||||||||
- | N will be a power of 2. | ||||||||||||
- | sz will be between 2 and 131,072, inclusive. | ||||||||||||
- | A0 will be between 0 and N-1, inclusive. | ||||||||||||
- | A1 will be between 0 and N-1, inclusive. | ||||||||||||
- | P will be between 0 and N-1, inclusive. | ||||||||||||
- | Q will be between 0 and N-1, inclusive. | ||||||||||||
- | R will be between 0 and N-1, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
按位搞,把每个数的每一位都拿出来搞,各种技巧,好题啊
#include <iostream> #include <cstring> #include <cstdio> #include <vector> using namespace std; long long B[40][2]; void go(int pos, vector<long long> I) { if(pos == -1) return; int n = I.size(); vector<long long> F,S; long long res1 = 0, res2 = 0, cnt1 = 0, cnt2 = 0; for(int i=0; i<n; i++) { if(I[i] & (1<<pos)) { res1 += cnt1; cnt2++; F.push_back(I[i]); } else { res2 += cnt2; cnt1 ++; S.push_back(I[i]); } } B[pos][0] += res1; B[pos][1] += res2; if(F.size() > 0) go(pos-1, F); if(S.size() > 0) go(pos-1, S); } class XorSequence { public: long long getmax(int N, int sz, int A0, int A1, int P, int Q, int R) { vector<long long> A(sz); memset(B, 0, sizeof(B)); A[0] = A0; A[1] = A1; for (int i = 2; i < sz; i++) { A[i] = ((long long)A[i - 2] * P + (long long)A[i - 1] * Q + R) % N; } go(29, A); long long res = 0; for(int i=0; i<40; i++) res += max(B[i][0], B[i][1]); return res; } };
标签:
原文地址:http://www.cnblogs.com/ltwy/p/4286804.html