当前位置:网站首页>[51nod 2493] sum of binary distances [bit operation]
[51nod 2493] sum of binary distances [bit operation]
2022-06-13 09:35:00 【Ayane.】
analysis :
n 2 n^2 n2 Violence is more You can count two x o r xor xor See how many there are 1 1 1
Complexity O ( n 2 l o g n ) O(n^2logn) O(n2logn)
You can also take apart each number binary If this bit has c n t 1 cnt_1 cnt1 individual 1 1 1 c n t 0 cnt_0 cnt0 individual 0 0 0 In the end c n t 0 × c n t 1 cnt_0\times cnt_1 cnt0×cnt1 Different
Complexity O ( n l o g n ) O(nlogn) O(nlogn)
CODE:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define reg register
using namespace std;
typedef long long ll;
const int N=1e4+5;
int n,a[N],ans,cnt0[N],cnt1[N];
void calc(int x)
{
for(int i=0;i<=30;i++)
((x>>i)&1)?cnt1[i]++:cnt0[i]++;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
calc(a[i]);
}
for(int i=0;i<=30;i++)
ans+=cnt1[i]*cnt0[i];
printf("%d",ans);
return 0;
}
边栏推荐
- Heap
- LeetCode 343. 整数拆分
- C language: deep understanding of pointers and arrays
- Lecture par lots de tous les fichiers vocaux sous le dossier
- 计算两个时间相差的天数(支持跨月、跨年)
- Acwing 787. Merge sort
- (bfs+GOOD) acwing 845. Eight digit
- Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
- C language: callback function
- Leetcode points to offer 30 Stack containing min function
猜你喜欢

Solov2 nanny level tutorial (including environment configuration, training your own data set, code logic analysis, etc...) Updating ing

Heap

(dfs) acwing 842. Arrange numbers

BGP Federation +community

(Dijkstra + shortest circuit + by point n^2) acwing 849 Dijkstra finding the shortest path I
![1-4 message passing interface [CSP authentication]](/img/db/aecda548693cdfb0e740bcf1a1c823.jpg)
1-4 message passing interface [CSP authentication]

LeetCode 1. Sum of two numbers

Tree and binary tree: basic operation and implementation of binary tree

C language: recursive function to realize Hanoi Tower

Jenkins access openldap user authentication
随机推荐
[ssl1280] full arrangement
【pytorch环境安装搭建】
MOOC week 8 programming exercise 1
LeetCode 201. 数字范围按位与
I set up a blog
C language: deep understanding of character functions and string functions (1)
LeetCode 5259. 计算应缴税款总额
Dynamic display of analog clock using digital clock in turtle Library
【最全面详细解释】背包问题详解
C language: data storage in memory
C language: Simulated Implementation of library function strcpy
SpEL表达式 简单使用
Trees and binary trees: the concept of binary trees
Classes and objects -- polymorphic
Batch read all voice files under the folder
VGA common resolution and calculation method
turtle库的使用数字时钟模拟时钟动态显示
C language: timer principle
Acwing785. quick sort (sort+ quick sort + merge sort)
C language: deep understanding of character functions and string functions (2)
