您的位置:首页 > 其它

zoj3870-Team Formation(异或运算)

2017-04-05 15:08 387 查看
题目来源


题意:每组n个数字,对于一组中任意两个数A,B满足A ⊕ B > max{A,
B},则A,B为满足条件的一对数。求满足条件的总对数。

思路:对于异或,就要用二进制去算。

          一个数要想变大就要把它二进制里的某个0变成1,。

          我们先对数字进行排序,按位数从小到大来。对于一个数的某个0所在的位,如果前面有数在此位上是1,那么前面那个数可以使这个数变大,可是仔细思考后,这个想法是不对的,如果前面那个数某个高位上有1,这个数对应的位上也是1,那么这两个数异或不符合条件。所以我们只要找最高位为1即可,定义一个数组表示在第i位上1的数量,每次从前往后迭代即可。

代码:

/// He renders landscapes with great skill and artistry.

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iterator>
#include <cctype>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <functional>
#include <ctime>
#include <bitset>

//#pragma comment(linker, "/STACK:102400000, 102400000")
#define debug puts("+******************************************************+")
#define Min(a, b) ( (a < b) ? a : b )
#define Max(a, b) ( (a > b) ? a : b )
#define lc o<<1
#define rc o<<1|1
#define lson L, M, lc
#define rson M + 1, R, rc
#define mem0(x) memset(x, 0, sizeof x)
#define mem1(x) memset(x, -1, sizeof x)
#define memf(x) memset(x, false, sizeof x)
#define pb push_back
#define pf push_front
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define fr(x) freopen("x", "r", stdin )
#define fw(x) freopen("x", "w" , stdout)
#define all(a) a.begin(), a,end()
#define X first
#define Y second
#define MP make_pair
#define Abs(x) ( x >= 0 ) ? x : ( -x )
#define MAXS 50000 + 8
#define MAXT 10000 + 8
#define MAXL 500000 + 8
#define INF 0x3f3f3f3f
#define INFL 1000000000000000000
#define inf -(1<<30)
#define EPS 1e-10
#define PI acos(-1.0)
#define sqr(x) (x * x)

using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef double DB;
typedef long double LD;
typedef pair<int , int > pii;
typedef pair<LL, LL> pll;

const int MOD = 1e9;
const int N = 1e5 + 8;
const int maxn = 1e3 + 8;
const int dx[] = { -1, 1, 0, 0 };
const int dy[] = { 0, 0, -1, 1 };

struct Node{
int x, y;
int id;
bool operator < ( const Node & n) const {
return x < n.x;
}
}no
;

int t, n;
LL a
;
LL cnt[100];

void geter(LL x, vector<int> &v, int &num )
{
int cur = 0;
while ( x > 0 ) {
if ( x & 1) {
num = cur;
//debug;
} else {
v.pb( cur );
}
cur++; x >>= 1;
}
}

vector<int> ling;
int main()
{
//freopen("codecoder.in", "r", stdin);
// freopen("codecoder.out", "w", stdout);
//ios::sync_with_stdio(true);
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0;i < n; i++) scanf("%lld", &a[i]);
LL ans = 0;
mem0(cnt);
sort(a, a + n);
for (int i = 0; i < n; i++) {
ling.clear();
int num = -1;
geter(a[i], ling, num );
//printf("%d++++\n", num);
for (int i = 0;i < ling.size(); i++) {
int u = ling[i];
ans += cnt[u];
}
if ( num >= 0 ) cnt[num]++;
}

printf("%lld\n", ans );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  异或 ACM