Akuna Capital笔试第二题, 找出异或大于与操作的对数量
作者:互联网
题意很简单,就是给一个数组,找出有多少个对是x ^ y > x & y
首先这题cf味道很冲,感觉当个div2b不是问题,那么我们就来考虑下高位讨论
所以我们可以计算出来每个ai的高位是多少,然后等差数列求和一下就行,最后用总对数减掉高位相同的对数,就是我们要的答案
#include <bits/stdc++.h> using namespace std; #define limit (1000000 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-9 #define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0); #define ff(a) printf("%d\n",a ); #define pi(a, b) pair<a,b> #define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i) #define MOD 998244353 #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next) #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin) #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout) typedef long long ll; typedef unsigned long long ull; char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; inline ll read() { #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) ll sign = 1, x = 0; char s = getchar(); while (s > '9' || s < '0') { if (s == '-')sign = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = (x << 3) + (x << 1) + s - '0'; s = getchar(); } return x * sign; #undef getchar }//快读 void print(ll x) { if (x / 10) print(x / 10); *O++ = x % 10 + '0'; } void write(ll x, char c = 't') { if (x < 0)putchar('-'), x = -x; print(x); if (!isalpha(c))*O++ = c; fwrite(obuf, O - obuf, 1, stdout); O = obuf; } int n, m, k; int a[limit]; void solve() { cin>>n; rep(i,1,n){ cin>>a[i]; } int bits[32] = {0}; rep(i,1,n){ per(j, 0, 31){ int bit = 1<<j; if(a[i] & bit){ bits[j]++; break; } } } ll ans = 0; ll res = 0; rep(i,0,30){ res += bits[i] * (bits[i] - 1) >> 1; } ans = n * (n - 1) >> 1; cout<<ans - res<<endl; } int32_t main() { #ifdef LOCAL FOPEN; // FOUT; #endif FASTIO // int kase; // cin >> kase; // while (kase--) solve(); cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }
标签:typedef,cout,int,rep,long,异或,Capital,Akuna,define 来源: https://www.cnblogs.com/tiany7/p/16462913.html