【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan

时间:2022-09-17 14:19:17

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

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

【题目链接】:http://codeforces.com/contest/742/problem/C

【题解】



先用个while 搞出所有的环的长度;

需要注意的就是;

环的长度如果小于等于2;则t为什么值都是可以的;所以不用管这种环;

然后啊长度大于2的环搞出来就好;

当然对于长度大于2的环还有特殊的情况;

就是这个长度为偶数;

那么这个人可以从最左边到中间,然后再到最右边(然后又回到1);这种情况我一开始漏掉了;真的是蠢死了;

显然这样可以让t更小一点;

所以对于长度大于2的偶数直接除2;

然后对上面需要处理的长度;求他们的最小公倍数就可以了;

每次都差一点,大概就是我的缺陷吧。坚持不下去?



【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second typedef pair<int,int> pii;
typedef pair<LL,LL> pll; void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} const int MAXN = 100+30;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n;
int a[MAXN];
bool flag[MAXN];
vector <int> g;
map <int,int> dic; int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d",&n);
rep1(i,1,n)
cin>>a[i];
memset(flag,false,sizeof(flag));
rep1(i,1,n)
if (!flag[i])
{
flag[i] = true;
int s = i,t = a[i],dd = 1;
while (!flag[t])
{
flag[t] = true;
t = a[t];
dd++;
}
if (t!=s)
{
puts("-1");
return 0;
}
if (dd>2)
{
if ((dd&1)==0)
dd/=2;
g.pb(dd);
}
}
LL ans; int len = g.size();
if (len==0)
ans = 1;
else
{
LL temp = g[0];
rep1(i,1,len-1)
{
LL temp1 = g[i];
LL gg = __gcd(temp,temp1);
temp = (temp*temp1)/gg; }
ans = temp;
}
cout << ans << endl;
return 0;
}

【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan的更多相关文章

  1. Codeforces 741A:Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan(LCM&plus;思维)

    http://codeforces.com/problemset/problem/741/A 题意:有N个人,第 i 个人有一个 a[i],意味着第 i 个人可以打电话给第 a[i] 个人,所以如果第 ...

  2. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  3. Codeforces Round &num;383 &lpar;Div&period; 2&rpar;C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  4. code forces 383 Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan&lpar;有向图最小环&rpar;

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  5. Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  6. C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

  7. Codeforces Round &num;383 &lpar;Div&period; 2&rpar; C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan(dfs&plus;数学思想)

    题目链接:http://codeforces.com/contest/742/problem/C 题意:题目比较难理解,起码我是理解了好久,就是给你n个位置每个位置标着一个数表示这个位置下一步能到哪个 ...

  8. C&period; Arpa&&num;39&semi;s loud Owf and Mehrdad&&num;39&semi;s evil plan DFS &plus; LCM

    http://codeforces.com/contest/742/problem/C 首先把图建起来. 对于每个a[i],那么就在i --- a[i]建一条边,单向的. 如果有一个点的入度是0或者是 ...

  9. 【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 ...

随机推荐

  1. redhat自定义安装必选

    redhat自定义安装必选 1.桌面 ked桌面 x 窗口系统 2.应用程序 编辑器 基于文本的互联网 图形互联网 3.服务器 服务器配置工具 万维网服务器 Windows文件 FTP服务器

  2. 使用RESTClient插件进行数据模拟&lpar;GET&comma;POST&rpar;提交

    1:在Firefox中下载RESTClient插件安装 2:安装好后的界面 (chrome://restclient/content/restclient.html) 3:选择GET/POST,输入U ...

  3. MSBI BigData demo—sqoop import

    --sp_readerrorlog 读取错误的信息记录 exec sys.sp_readerrorlog 0, 1, 'listening'查看端口号 首先hadoop环境要配置完毕,并检验可以正常启 ...

  4. 【Backbone】简介

    1.Model 2.Collection 3.View 4.Router 5.History 6.Events http://addyosmani.github.io/backbone-fundame ...

  5. Android应用中-更新提示显示红点的方案

    什么是红点更新提示? 红点更新提示类似微信朋友圈有新的朋友消息 时会在“发现”tab上显示红点,表示有新的消息. 目前三种显示方式: 1.显示具体数字 2.只显示红点 3.显示省略,表示数量很多 方案 ...

  6. 等待事件--db file sequential read

    对于最小化db file sequential read 事件所带来的影响,你可以做的另一件事情是减少AVERAGE_WAIT时间. 这是会话必须等待从磁盘提取单块的平均时间,这些信息可以从v$ses ...

  7. MySQL GROUP BY多个字段分组用法详解

    mysql语句中group by 很容易理解 是分组查询.比如 select sum(score) from user group by name 意思是查询每个人的分数总和但是, select su ...

  8. 字节转化为结构体BytesToStruct

    //结构体转字节数组 public byte[] StructToBytes (object structObj) { int size = Marshal.SizeOf (structObj);// ...

  9. C&num; winform(计算器)

  10. commonJS、AMD、es模块化 区别(表格比较)

    commonJS.AMD.es6模块化 区别(表格比较): table th:first-of-type { } table th:nth-of-type(3) { width: 150px; } t ...