1368D - AND OR and square sum - CodeForces Solution


bitmasks greedy math *1700

Please click on ads to support us..

C++ Code:

//#include"pch.h"
#include<bits/stdc++.h>
using namespace std;
#define Mbappe {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
#define ll long long
#define vc vector
#define str string
#define e "\n"
#define all(v) (v).begin() , (v).end() 
#define File freopen("problem name", "r", stdin);
#define pi 3.14159265359
#define MEM(x , y) memset(x , y , sizeof(x))
const int mod = 1e9 + 7;
const int MOD = 32768;
const int oo = 0x3f3f3f3f;
bool ok = true, go = true;
int dx[8] = { 0, 0, 1, -1, -1, -1, 1, 1 };
int dy[8] = { -1, 1, 0, 0, -1, 1, -1, 1 };
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
//---------------------------- [ We need a trick ] -------------------------</end>
/*
const int N = 100;
vc<vc<int>>adj(N);// about graph repressentation
vc<vc<int>>ccs;// collect components
vc<int> instack(N);// for chieck this node exist in ancestor or not
vc<int>DFS_num(N);// numbring the nodes with DFS number
vc<int>lwlink(N);// numbring the nodes with low link number
vc<int>any_comp(N);// the node exist in any component
stack<int>stk;// for get all nodes in one component
int ini = 1;// inatail value of both DFS_num and lowlink number
void Uncle_Tarjan_Ccs(int node) {
	DFS_num[node] = ini; lwlink[node] = ini; instack[node] = 1; stk.push(node); ini++;// intailize
	for (auto u : adj[node]) {
		if (!DFS_num[u]) {// not visted
			Uncle_Tarjan_Ccs(u);
			lwlink[node] = min(lwlink[node], lwlink[u]);
		}
		else if (instack[u]) {// visted
			lwlink[node] = min(lwlink[node], lwlink[u]);
		}
	}
	// this node finished all his childs
	if (lwlink[node] == DFS_num[node]) {
		int x = -1;
		ccs.push_back(vc<int>());
		while (x!= node)
		{
			x = stk.top(); stk.pop(); instack[x] = 0;
			ccs.back().push_back(x);
			any_comp[x] = ccs.size();
		}
	}
}

const int N = 2e5+5;
vc<vc<int>>adj(N);
vc<int>DFS_num(N), lw(N);
vc<pair<int, int>>bridges;
int ini = 1;
void Uncle_Tarjan_Bridge(int node , int par) {
	DFS_num[node] = ini; lw[node] = ini; ini++;
	for (auto u : adj[node]) {
		if (!DFS_num[u]) {
			Uncle_Tarjan_Bridge(u, node);
			lw[node] = min(lw[node], lw[u]);
		}
		else if (u != par) {
			lw[node] = min(lw[node], lw[u]);
		}
	}
	if (DFS_num[node] == lw[node] && par != -1 ) {
		//cout << "exist bridge here from " << par << " to " << node << e;
		bridges.push_back({ par , node });
	}
}
*/
int main() {
	Mbappe;
	int n; cin >> n;
	ll ans = 0;
	map<int, int>mp;
	for (int a = 0; a < n; a++) {
		int x; cin >> x;
		for (int z = 0; z <= 20; z++) {
			if (x & (1 << z)) {
				int num = (1 << z);
				mp[num]++;
			}
		}
	}
	ok = true;
	while (ok)
	{
		ok = false;
		ll cur = 0;
		for (auto u : mp) {
			if (mp[u.first]) {
				cur += u.first;
				mp[u.first]--;
				ok = true;
			}
		}
		ans += (cur * cur);
	}
	cout << ans;
}


Comments

Submit
0 Comments
More Questions

1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo
208D - Prizes Prizes more Prizes
659A - Round House
1492C - Maximum width
171B - Star
1512B - Almost Rectangle
831B - Keyboard Layouts
814A - An abandoned sentiment from past
268C - Beautiful Sets of Points
1391C - Cyclic Permutations
11A - Increasing Sequence
1406A - Subset Mex
1365F - Swaps Again
50B - Choosing Symbol Pairs
1719A - Chip Game
454B - Little Pony and Sort by Shift
1152A - Neko Finds Grapes
1719B - Mathematical Circus
1719C - Fighting Tournament
1642A - Hard Way
285C - Building Permutation
1719E - Fibonacci Strings
1696C - Fishingprince Plays With Array
1085A - Right-Left Cipher
1508B - Almost Sorted