当前位置:网站首页>[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;
}
边栏推荐
- VGA常用分辨率及计算方法
- 马斯克的「元宇宙」梦
- Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
- LeetCode 202. 快乐数
- Dynamic display of analog clock using digital clock in turtle Library
- @Value does not take effect and extend/implement other classes cannot inject beans manually
- 英国出台粮食安全计划抵御粮食供应危机
- Solov2 source code analysis
- go-zero微服务实战系列(三、API定义和表结构设计)
- 【 ssl2 ⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶83
猜你喜欢

Jenkins accédant à l'authentification de l'utilisateur openldap

(bfs) acwing 847. Hierarchy of points in the graph

C language: minesweeping

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

C language: deep understanding of character functions and string functions (2)

Jenkins access openldap user authentication

VGA common resolution and calculation method

Trees and binary trees: traversal of binary trees

LeetCode 1. Sum of two numbers

BGP Federation +community
随机推荐
[pytorch environment installation]
LeetCode 201. Digit range bitwise AND
[ssl1280] full arrangement
C language: timer principle
Exercise 7-7 string replacement (15 points)
C language: file operation
C language: minesweeping
C language: dynamic memory management
LeetCode 1143. 最长公共子序列
Summary of random number learning
(topological sorting +bfs) acwing 848 Topological sequence of digraph
[implementation of depth first search]
LeetCode 6096. 咒语和药水的成功对数(二分查找)
@Value不生效及extend/implement其他类无法注入bean的手动注入
LeetCode 5270. Minimum path cost in grid (dynamic programming)
Amadahl's Law (a little thought)
[51nod p3216] Award [bit operation]
ROS2之OpenCV人脸识别foxy~galactic~humble
acwing 789. Range of numbers (dichotomy + suitable for understanding dichotomy boundary)
LeetCode 6096. Success logarithm of spells and potions (binary search)
