当前位置:网站首页>C - XOR to all (binary topic)
C - XOR to all (binary topic)
2022-07-05 06:16:00 【whitewall_ nine】
https://atcoder.jp/contests/arc135/tasks/arc135_c
#include<iostream>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fir first
#define pb push_back
#define sec second
#define sortall(x) sort((x).begin(),(x).end())
inline int read () {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f?-x:x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x/10);
putchar(x % 10 + '0');
}
const int N = 35;
int cnt[N];
int a[300005];
int n;
void solve() {
cin >> n;ll ans = 0;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
int t= a[i];
ans += t;
for (int j =0; j <= 30; j ++)
cnt[j] += (t >> j & 1);
}
for (int i = 1; i <= n; i++) {
ll res = 0;
for (int j = 0; j <= 30; j++)
res += ((a[i] >> j & 1) == 1? n - cnt[j] : cnt[j]) * ((ll)1 << j);
ans = max (ans, res);
}
cout << ans << endl;
}
int main () {
int t;
t =1;
while (t --) solve();
return 0;
}
The difficulty lies in how to efficiently count the sum after XOR . We observe binary , For each selected value , We have found , If the binary bit of the current value is 0, Then the binary of the same position of all other numbers is upper 1 Will be preserved , And contribution equals 1 Multiply the number of by the weight of the position . Otherwise, it counts 0 The number of contributions . Pay attention to the synthesis of the initial state . I've been thinking about how to make a sum , Numbers are not considered as binary bits , So we can't find the law . Next time you encounter this problem, you should consider binary representation .
Some properties of XOR
For sequence A, B,B It's from A The result after XOR operation .
a[i]^a[j] = b[i]^b[j];
边栏推荐
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- 11-gorm-v2-03-basic query
- LeetCode 0107. Sequence traversal of binary tree II - another method
- Navicat连接Oracle数据库报错ORA-28547或ORA-03135
- LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
- Leetcode recursion
- LeetCode 1200. Minimum absolute difference
- 1.14 - assembly line
- C Primer Plus Chapter 15 (bit operation)
- [rust notes] 16 input and output (Part 1)
猜你喜欢

Data visualization chart summary (II)

Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition

MySQL advanced part 1: stored procedures and functions

Dynamic planning solution ideas and summary (30000 words)

Appium自动化测试基础 — Appium测试环境搭建总结

1.14 - assembly line

Open source storage is so popular, why do we insist on self-development?
![Introduction to LVS [unfinished (semi-finished products)]](/img/72/d5a943a8d6d71823dcbd7f23dda35b.png)
Introduction to LVS [unfinished (semi-finished products)]

MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
随机推荐
Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
leetcode-556:下一个更大元素 III
QQ电脑版取消转义符输入表情
Multi screen computer screenshots will cut off multiple screens, not only the current screen
SPI 详解
Flutter Web 硬件键盘监听
【Rust 笔记】15-字符串与文本(上)
MySQL advanced part 1: index
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
SQLMAP使用教程(二)实战技巧一
LVS简介【暂未完成(半成品)】
[rust notes] 17 concurrent (Part 2)
1.14 - 流水线
6. Logistic model
1039 Course List for Student
Binary search template
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
wordpress切换页面,域名变回了IP地址
【LeetCode】Easy | 20. Valid parentheses