当前位置:网站首页>[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;
}
边栏推荐
- LeetCode 6097. Match after replacing characters (Dictionary)
- Online debugging tool Arthas Foundation
- Yolov5 model evaluation
- acwing 790. The third root of a number (dichotomy)
- C language: recursive function to realize Hanoi Tower
- Resolve importerror:lib*** so--cannot open shared object file: No such... (pycharm/clion reports an error but the shell does not)
- Exception handling operation
- C language: deep understanding of character functions and string functions (1)
- [pytorch environment installation]
- LeetCode 343. 整数拆分
猜你喜欢
Consolas-with-Yahei
Exploitation of competitive loopholes in attacking and defending world PWN play conditions
VGA common resolution and calculation method
[51nod p3216] Award [bit operation]
[pytorch environment installation]
Trees and binary trees: Construction of binary trees
1-2 24:00 (20 points) [CSP certification true question]
(bfs) acwing 844. Labyrinth
(dfs) acwing 842. Arrange numbers
(bfs) acwing 847. Hierarchy of points in the graph
随机推荐
Batch read all voice files under the folder
Acwing785. quick sort (sort+ quick sort + merge sort)
Delete soft link
Standard template library (STL)
全新BMW i3的操控,能符合对3系之名产品的期待吗?
Instruction level parallelism (?)
C language: shortcut keys commonly used in VS
SQL ROW_ The number() function uses
Lecture par lots de tous les fichiers vocaux sous le dossier
LeetCode 6097. Match after replacing characters (Dictionary)
Exploitation of competitive loopholes in attacking and defending world PWN play conditions
Yolov5 model evaluation
LeetCode 1143. Longest common subsequence
LeetCode 6096. 咒语和药水的成功对数(二分查找)
(dp+ memory) acwing 901 skiing
Tree and binary tree: operation and storage structure of tree
BGP Federation +community
I set up a blog
Classes and objects -- object model and this pointer
Summary of random number learning