CF2.BC
1 second
256 megabytes
standard input
standard output
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 ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x.
Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the elements of the array.
Print a single integer: the answer to the problem.
2 3
1 2
1
6 1
5 1 2 3 4 1
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.
题意:
n个数,问这n个数中有几对数异或运算后结果是x。
代码:
//c=a^b,a=c^b,b=a^c.用一个数组记录每个数出现的次数,每输入一个数将其与x异或运算得到的新数是否是前面输入过的,
//如果是,答案就加上该数出现的次数
#include<bits\stdc++.h>
using namespace std;
int a[],b[];
int main()
{
int n,x;
memset(b,,sizeof(b));
long long ans=;
scanf("%d%d",&n,&x);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
int tem=a[i]^x;
if(tem>) continue;//不会出现大于100000的数
ans+=b[tem];
b[a[i]]++;
}
printf("%lld\n",ans);
return ;
}
1 second
256 megabytes
standard input
standard output
As you have noticed, there are lovely girls in Arpa’s land.
People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi.

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person x wants to start a round, he calls crushx and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crushi = i).
The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.
The second line contains n integers, i-th of them is crushi (1 ≤ crushi ≤ n) — the number of i-th person's crush.
If there is no t satisfying the condition, print -1. Otherwise print such smallest t.
4
2 3 1 4
3
4
4 4 4 4
-1
4
2 1 4 3
1
In the first sample suppose t = 3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
题意:
大小为n的数组a,a[i]表示从i点能一步到a[i]点,问有没有这样的一个最小的数t使得每一个a[i]满足从a[i]点走t步到a[j]点,从a[j]点走t步到达a[i]点。a[i]==i是允许的
代码:
//枚举起点,如果从该点出发向下找,回不到到改点即在改点后面的某几个点之间循环说明无答案,如果从该点出发经过x步之后
//又回到了改点说明改点合法,如果x是奇数说明循环的点就是改点,如果x是偶数说明循环的点是改点和x/2处的某一点
//找出每个点需要的循环步数取最小公倍数就是答案。
#include<bits\stdc++.h>
using namespace std;
int gcd(int x,int y)
{
if(y==) return x;
return gcd(y,x%y);
}
int lcm(int x,int y)
{
return (x/gcd(x,y))*y;
}
int main()
{
int n,a[],vis[],ans=;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
if(n==&&a[]!=) //只有一个点的情况特殊考虑
{ans=-;break;}
memset(vis,,sizeof(vis));
int x=i,tem=;
while(!vis[x])
{
vis[x]=;
x=a[x];
tem++;
}
if(x!=i) //如果从i点回到的是i后面的点就说明i点没有与之对应的点
{ans=-;break;}
if(tem&) ans=lcm(tem,ans);
else ans=lcm(tem/,ans);
}
printf("%d\n",ans);
return ;
}
CF2.BC的更多相关文章
- 数据库设计范式2——BC范式和第四范式
我在很久之前的一篇文章中介绍了数据库模型设计中的基本三范式,今天,我来说一说更高级的BC范式和第四范式. 回顾 我用大白话来回顾一下什么是三范式: 第一范式:每个表应该有唯一标识每一行的主键. 第二范 ...
- BC之Claris and XOR
http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...
- bc:linux下命令行计算器
在linux下,存在一个命令行的计算器:bc.该程序一般随发行版发布. bc计算器能够执行一些基本的计算,包括+,-,×,\,%. 这些计算不经针对十进制,还可以使用二进制,八进制,十六进制,并且可以 ...
- bc#29 做题笔记
昨天的bc被坑惨了= = 本来能涨rating的大好机会又浪费了...大号已弃号 A:第一反应是高精度,结果模板找不到了= =,然后现学现卖拍了个java的BigInteger+快速幂,调了好半天不说 ...
- shell命令bc
简介 bc支持浮点数的精度运算(Bash不支持浮点数运算) 运行方式 一.CLI 二.PIPE 示例 一.浮点数运算 变量scale:设置小数点后面的位数 # 默认scale=0 echo &quo ...
- windbg-bp、 bm、 bu、 bl、 bc、 ba(断点、硬件断点)
bp bp 命令是在某个地址下断点, 可以 bp 0x7783FEB 也可以 bp MyApp!SomeFunction . 对于后者,WinDBG 会自动找到MyApp!SomeFunction 对 ...
- [推荐] BC/Beyond Compare(差异比较软件)
Beyond Compare 前一段时间,介绍过用Total Commander来完成文件夹同步的时候,一位朋友留言推荐了Beyond Compare--一个强大的超越了文件差异比较的工具.Beyon ...
- echo "scale=100; a(1)*4" | bc -l 输出圆周率
突然看到echo "scale=100; a(1)*4" | bc -l可以输出圆周率,很惊奇,后来发现很简单. 首先bc是“basic calculator”的缩写,就是初级的计 ...
- bc.34.B.Building Blocks(贪心)
Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- C++开始前篇,深入编译链接(补充2)
在开始链接之前,我们先了解几个概念: 一>符号的概念. 我们知道,链接的最重要的是"对符号的重定位",而且上面提到了符号表,那什么是符号呢,在链接中,我们将函数和变量统称为符 ...
- Webbench性能测试
1.下载安装:立即下载 官网:http://home.tiscali.cz/~cz210552/webbench.html 2.解压缩:tar -zxvf webbench-1.5.tar.gz 3 ...
- 使用pyInstaller发布PathMerge的exe版本(py转换成exe)
前言 PathMerge是用python写的一个辅助文件夹合并的小工具,它的特点是不用担心合并后文件会丢失,旧文件会创建副本保存下来,除非你手动删除. 详情见:python开发目录合并小工具 Path ...
- CozyRSS开发记录22-界面退化
CozyRSS开发记录22-界面退化 1.问题1-HtmlTextBlock 找的这个HtmlTextBlock有很严重的bug,有时候显示不完全,有时候直接就崩了.然后看了下代码,完全是学生仔水平写 ...
- HDU 1848 SG函数博弈
Fibonacci again and again Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1 ...
- Design and Implementation of the Sun Network File System
Introduction The network file system(NFS) is a client/service application that provides shared file ...
- 【Hibernate框架】关联映射(多对多关联映射)
按着我们的总结行进计划,接下来,就是有关于多对多映射的总结了. 我们来举个例子啊,很长时间以来,房价暴涨不落,但是还有很多人拥有很多套房产,假如说,一个富豪拥有九套房产,家里人么准去住哪一套,我们就以 ...
- retrofit2的get和post
get: 例: @GET("room/question_focus") Call<BaseResponseEntity> followQuestion(@Query(& ...
- httpclient 无信任证书使用https
1.当不需要使用任何证书访问https网页时,只需配置信任任何证书 HttpClient http = new HttpClient(); String url = "https://pay ...
- WPF中使用DynamicResource实现换肤
这篇将介绍使用DynamicResource实现动态的界面切换功能.熟悉WPF的园友应该已经猜到了实现方式,简而言之就是动态替换DataTemplate,ControlTemplate,Style等等 ...