码迷,mamicode.com
首页 > 其他好文 > 详细

TC SRM 649 500分题

时间:2015-02-11 20:16:30      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

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

    
Class: XorSequence
Method: getmax
Parameters: int, int, int, int, int, int, int
Returns: long long
Method signature: long long getmax(int N, int sz, int A0, int A1, int P, int Q, int R)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

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)  
    
4
6
3
2
0
1
3
Returns: 8
Using the provided pseudocode you should compute that A={3,2,1,0,3,2}. For B=3 we then get C={0,1,2,3,0,1}. For this C there are 8 pairs (i,j) such that i<j and C[i]<C[j]. These are the 8 pairs: (0,1), (0,2), (0,3), (0,5), (1,2), (1,3), (2,3), and (4,5). No other choice of B produces more than 8 pairs.
1)  
    
8
8
2
5
3
1
4
Returns: 13
A={2,5,7,2,3,5,2,5}.
2)  
    
8
7
3
0
1
2
4
Returns: 12
A={3,0,7,2,7,4,3}.
3)  
    
32
15
7
9
11
2
1
Returns: 60
A={7,9,0,4,9,31,2,26,11,21,4,16,13,11,6}.
4)  
    
131072
131072
7
7
1
0
0
Returns: 0
All elements of A are equal to 7. Regardless of the value of B you choose, all elements in C will be equal as well. Thus, the number of pairs we seek is always zero.
5)  
    
131072
131070
411
415
398
463
9191
Returns: 4302679760
 

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;
        }
};

 

TC SRM 649 500分题

标签:

原文地址:http://www.cnblogs.com/ltwy/p/4286804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!