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

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.

Input

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.

Output

Print a single integer: the answer to the problem.

Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note

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 ;
}
C. Arpa's loud Owf and Mehrdad's evil plan
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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).

Input

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.

Output

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.

Examples
Input
4
2 3 1 4
Output
3
Input
4
4 4 4 4
Output
-1
Input
4
2 1 4 3
Output
1
Note

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的更多相关文章

  1. 数据库设计范式2——BC范式和第四范式

    我在很久之前的一篇文章中介绍了数据库模型设计中的基本三范式,今天,我来说一说更高级的BC范式和第四范式. 回顾 我用大白话来回顾一下什么是三范式: 第一范式:每个表应该有唯一标识每一行的主键. 第二范 ...

  2. BC之Claris and XOR

    http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...

  3. bc:linux下命令行计算器

    在linux下,存在一个命令行的计算器:bc.该程序一般随发行版发布. bc计算器能够执行一些基本的计算,包括+,-,×,\,%. 这些计算不经针对十进制,还可以使用二进制,八进制,十六进制,并且可以 ...

  4. bc#29 做题笔记

    昨天的bc被坑惨了= = 本来能涨rating的大好机会又浪费了...大号已弃号 A:第一反应是高精度,结果模板找不到了= =,然后现学现卖拍了个java的BigInteger+快速幂,调了好半天不说 ...

  5. shell命令bc

    简介 bc支持浮点数的精度运算(Bash不支持浮点数运算) 运行方式 一.CLI 二.PIPE 示例 一.浮点数运算 变量scale:设置小数点后面的位数  # 默认scale=0 echo &quo ...

  6. windbg-bp、 bm、 bu、 bl、 bc、 ba(断点、硬件断点)

    bp bp 命令是在某个地址下断点, 可以 bp 0x7783FEB 也可以 bp MyApp!SomeFunction . 对于后者,WinDBG 会自动找到MyApp!SomeFunction 对 ...

  7. [推荐] BC/Beyond Compare(差异比较软件)

    Beyond Compare 前一段时间,介绍过用Total Commander来完成文件夹同步的时候,一位朋友留言推荐了Beyond Compare--一个强大的超越了文件差异比较的工具.Beyon ...

  8. echo "scale=100; a(1)*4" | bc -l 输出圆周率

    突然看到echo "scale=100; a(1)*4" | bc -l可以输出圆周率,很惊奇,后来发现很简单. 首先bc是“basic calculator”的缩写,就是初级的计 ...

  9. bc.34.B.Building Blocks(贪心)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. Ubuntu16.04安装Atom

    转自:http://blog.csdn.net/q1302182594/article/details/51304401 sudo add-apt-repository ppa:webupd8team ...

  2. SQL Server 从数据库中查询去年的今天的数据的sql语句

    因为最近的项目的一个小功能需要实现当前数据和历史的今天做一个对比.在网上也查了很久,很多都是实现一个月内的,一年内的所有数据,昨晚突然就找到了下面的实现方法,在SQL Server2008中试了一下, ...

  3. Java常用的技术网站

    学习Java,我会去的网站: 1.开源项目网站:https://github.com/和http://www.codeproject.com/,可以在这里搜索到别人上传的一些代码和项目 2.咨询问题的 ...

  4. Dcloud HTML5 监听蓝牙设备 调用 原生安卓实现

    最近一直搞Dcloud ,这是HTML5版本的开发,打包时候,可以打包成 apk 和ipa 分别运行在安卓和ios 机器上面, 但是这里面的资料很少,遇到问题,之后只能自己钻研总结, 现在有这么一个需 ...

  5. C语言 指针小结

    指针 -->指针变量 类型名 *变量名 int *point1; char *point2; 注意:*p可以直接使用,它代表指针p指向的变量,*p可以当做被指向的变量使用!~~~~ 一个变量的地 ...

  6. JS多线程(web work)

    JS多线程JS多线程不允许操作DOM 1. 引用Concurrent Thread.js库用法:Concurrent.Thread.Create(function(){};) 2. Web Workh ...

  7. JavaScript代码优化指南

    1. 将脚本放在页面的底部 <script src="./jquery.min.js"></script> <script src="./i ...

  8. 剑指Offer-【面试题02:实现Singleton 模式——七种实现方式】

    题目:设计一个类,我们只能生成该类的一个实例 package com.cxz.demo02; /** * Created by CXZ on 2016/9/13. */ public class Si ...

  9. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  10. 用 IIS 实现请求转发

    最近部门要开发一个简单的APP,部分数据是现有项目已经存在的,为了方便维护,希望只提供一个交互的入口,并且协议的规则不变. 基于这个需求,有两套解决方案: 1.用代码将现有的api封装一层,对请求数据 ...