codeforces链接:https://codeforces.com/problemset/problem/742/B

CF742B Arpa’s obvious problem and Mehrdad’s terrible solution

题目描述

There are some beautiful girls in Arpa’s land as mentioned before.

Once Arpa came up with an obvious problem:

Given an array and a number $ x $ , count the number of pairs of indices $ i,j $ ( $ 1<=i<j<=n $ ) such that , where is bitwise xor operation (see notes for explanation).

Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.

输入格式

First line contains two integers $ n $ and $ x $ ( $ 1<=n<=10{5},0<=x<=10 $ ) — the number of elements in the array and the integer $ x $ .

Second line contains $ n $ integers $ a_{1},a_{2},...,a_{n} $ ( $ 1<=a_{i}<=10^{5} $ ) — the elements of the array.

输出格式

Print a single integer: the answer to the problem.

输入输出样例 #1

输入 #1

2 3
1 2

输出 #1

1

输入输出样例 #2

输入 #2

6 1
5 1 2 3 4 1

输出 #2

2

说明/提示

In the first sample there is only one pair of $ i=1 $ and $ j=2 $ . so the answer is $ 1 $ .

In the second sample the only two pairs are $ i=3 $ , $ j=4 $ (since ) and $ i=1 $ , $ j=5 $ (since ).

A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is $ 1 $ if only the first bit is $ 1 $ or only the second bit is $ 1 $ , but will be $ 0 $ if both are $ 0 $ or both are $ 1 $ . You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.

思路:

这道题没有思路,题目很容易读懂,主要是要了解异或的一个性质

A⨁(A⨁B)=B。

我们可以在输入的时候统计输入的数字的个数,答案则是累加每次输入的时候num[i]^x的数字的数量

至于为什么是便输入边计算,是因为题目说明了选择的两个数字ai和aj,i<j的,我们将输入的数字当成aj

这样的话才可以保证最后答案的正确性,统计的cnt的数量才是前面的数量

题解

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,x;
int num[N];
int cnt[2*N];
ll ans=0;
int main()
{
cin>>n>>x;
for(int i=1;i<=n;i++)
{
cin>>num[i];
ans+=cnt[num[i]^x];
cnt[num[i]]++; }
cout<<ans<<endl;
return 0;
}

CF742B Arpa’s obvious problem and Mehrdad’s terrible solution (数论)的更多相关文章

  1. CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解

    Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...

  2. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution

    B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...

  3. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或

    题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...

  4. Arpa’s obvious problem and Mehrdad’s terrible solution 思维

    There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious ...

  5. B. Arpa’s obvious problem and Mehrdad’s terrible solution

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  6. 【codeforces 742B】Arpa’s obvious problem and Mehrdad’s terrible solution

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  8. 枚举 || CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution

    给出N*M矩阵,对于该矩阵有两种操作: 1.交换两列,对于整个矩阵只能操作一次 2.每行交换两个数. 交换后是否可以使每行都递增. *解法:N与M均为20,直接枚举所有可能的交换结果,进行判断 每次枚 ...

  9. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  10. CF 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths [dsu on tree 类似点分治]

    D. Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths CF741D 题意: 一棵有根树,边上有字母a~v,求每个子树中最长的边,满 ...

随机推荐

  1. TVM:使用调度模板和AutoTVM优化算子

    本节学习如何使用TVM 张量表达式(TE)语言来编写调度模板,这些模板可以被autoTVM搜索到,以找到最佳调度.这个过程称为auto-Tuning,它有助于优化张量计算的自动化过程. 本节建立在如何 ...

  2. 一种基于偏移流和纯字符串流来存储和读取字符串列表的方法【C#】

    字符串的存储长度是可变的,在C#中,BinaryWriter和BinaryReader在Write,ReadStirng的时候,都在单个流中字符串的二进制数组前面加了一个二进制数组的长度信息,方便读取 ...

  3. 通过COM,用Python调用C#库

    1.C#配置 (1)类库 (2)COM互操作打勾 (3)代码中类必须要有无参构造函数,否则不会注册成功!!! using System; using System.Runtime.InteropSer ...

  4. eclipse修改默认的工作空间路径

    搜索Workspaces -->勾选Prompt for workspace on startup

  5. C#实现自己的MCP Client

    市面上,有很多免费Client客户端. 虽然说,这些Client客户端可以满足我们大部分的需求,但是在实际企业业务场景中,免费的Client无法全部满足我们的需求. 下面我们用C# 实现MCP Cli ...

  6. 浅谈Spring、Spring MVC、Spring Boot和Spring Cloud的关系和区别

      Spring 框架就像一个家族,有众多衍生产品,例如 boot.security.jpa等等.但它们的基础都是Spring的IOC和AOP等.IOC提供了依赖注入的容器,AOP解决了面向横切面编程 ...

  7. mysql中compact行的存储结构

    mysql中行的格式类型包括:Compact.redundant.dynamic.compressed这四种,行和行之间是通过一个单向链表的形式来连接的,而我在实际工作中最常用到的是compact类型 ...

  8. vue的a-tree-select选择父节点回显的是子节点

    正常来说当选择父节点时候,我们回显的应该就是父节点的信息比如:  可是现在我想回显子节点的信息如何处理? 很简单:在 a-tree-select组件里面去掉这一句:  这样回显的就是我们想要的结果了 ...

  9. 现在的AI工具已经能够创作音乐了?

    本文由 ChatMoney团队出品 随着科技的不断进步,音乐是人类文明的一部分,它在社会.文化.艺术和娱乐领域发挥着重要作用.随着AI技术的发展,AI技术的应用正在以惊人的速度改变音乐创作.演奏.传播 ...

  10. From Small Not Perfect

    自己想实现一个共享文档,然后统计每个人每周做题的数量,然后还想到每个月的统计,每年的统计,哇,好复杂哈 所以我想先做一个Excel,然后开始使用,中间发现了问题,然后调整,修改. 当我做了这个Exce ...