当前位置: 代码迷 >> 综合 >> Codeforces Round #383 (Div. 1) 741A Arpa's loud Owf and Mehrdad's evil plan【模拟】
  详细解决方案

Codeforces Round #383 (Div. 1) 741A Arpa's loud Owf and Mehrdad's evil plan【模拟】

热度:47   发布时间:2023-11-11 11:08:35.0

题目传送门:

A. 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 numbercrushi.

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 callscrushx and says: "Oww...wwf" (the letterw is repeated t times) and cuts off the phone immediately. Ift?>?1 then crushx callscrushcrushx and says: "Oww...wwf" (the letterw 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 smallestt (t?≥?1) such that for each personx, if x starts some round andy becomes the Joon-Joon of the round, then by starting fromy, x would become the Joon-Joon of the round. Find sucht 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 ofi-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 ifx 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 whenx is 4.

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.


其实这题需要模拟。看这个数列是否有环。比如第一组数据:2——3——1 和 4——4两个环

然后求一下这些环的长度,取他们的最小公倍数。结果就是t。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M(a) memset(a,0,sizeof(a))
int num[105];
int vis[105];
LL ans=1;
int flag=0;
LL sum;
LL temp;
LL gcd(LL a,LL b)
{LL MAX=max(a,b);LL MIN=min(a,b);LL test;while(MIN>0){test=MAX%MIN;MAX=MIN;MIN=test;}return MAX;
}
int main()
{int n;while(~scanf("%d",&n)){flag=1;ans=1;M(num);M(vis);for(int i=1; i<=n; i++){scanf("%d",&num[i]);}for(int i=1; i<=n; i++){if(vis[i]){continue;}vis[i]=1;sum=1;temp=num[i];while(temp!=i){vis[temp]=1;temp=num[temp];sum++;///sum为环的长度if(sum>n){flag=0;break;}}if(!flag){break;}if(sum%2==0){sum/=2;///长度是偶数的话,需要除2。奇数不用变}ans=ans/gcd(ans,sum)  * sum;///最小公倍数}if(flag==0){printf("-1\n");}else{printf("%lld\n",ans);}}return 0;
}


  相关解决方案