A. Arpa's loud Owf and Mehrdad's evil plan--java实现
来源:互联网 发布:大麦盒怎么用电信网络 编辑:程序博客网 时间:2024/05/29 19:26
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 ttimes) and cuts off the phone immediately. If t > 1 then crushx calls crushcrushx and says: "Oww...wwf" (the letter w is repeated t - 1times) 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.
42 3 1 4
3
44 4 4 4
-1
42 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个整数,x→a[x]→a[a[x]]→.... 进行t次,“owwwf”可以不看= =,当终点为y,且y进行t次也能得到终点为x 则在数组里存在一个环。 求数组中环的最小公倍数,不存在输出-1。其实可以看作求这些数组中环长度的最小公倍数。通俗点讲按第一个例子来,当x=2,a[2]=3,注意这里数组的a[0]不使用,实际程序自己可以处理,此时x2=3,a[3]=1,此时x3=1,a[1]=x=2,形成一个环。写的有点粗糙,也借鉴了别人的思路进行了改进。
import java.util.*;public class M{ static Scanner sc; public static void swap(long a, long b){ long tmp = a;a = b;b = tmp; } public static long gcd(long a,long b) {if(b>a) swap(a,b); long c;while(b>0){ c=a%b; a=b; b=c;}return a; } public static void main(String []args){ sc = new Scanner(System.in); int n=sc.nextInt(); int num=1; int []a=new int[n+1]; int []b=new int[n+1]; while (sc.hasNext()){ a[num]=sc.nextInt(); num++; } long ans=1;int flag=0;for(int i=1;i<=n;++i){if(b[i]==1)continue;long temp=1;int y=a[i];b[i]=1;while(y!=i){b[y]=1;y=a[y];temp++;if(temp>n){ flag=1;break;}}if(flag==1)break;if(temp%2==0)temp/=2;ans = ans/gcd(ans,temp)*temp; }if(flag==1)System.out.print("-1\n");else System.out.print(ans); }}
- A. Arpa's loud Owf and Mehrdad's evil plan--java实现
- Codeforces 741A Arpa's loud Owf and Mehrdad's evil plan(思维)
- 【Codeforces 741A. Arpa's loud Owf and Mehrdad's evil plan 】+ 思维
- Codeforces 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
- Codeforces Round #383 (Div. 1) Problem A. Arpa's loud Owf and Mehrdad's evil plan 解题报告
- 【codeforces 742C】Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)-C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)C-Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round #383 (Div. 2)B -Arpa's loud Owf and Mehrdad's evil plan
- CodeForces 742C Arpa's loud Owf and Mehrdad's evil plan
- codefores741A Arpa's loud Owf and Mehrdad's evil plan(图找环)
- Codeforces Round#383C. Arpa's loud Owf and Mehrdad's evil plan
- codeforces #383 div 2 C. Arpa's loud Owf and Mehrdad's evil plan
- Codeforces 742C C. Arpa's loud Owf and Mehrdad's evil plan
- codeforces 742c Arpa's loud Owf and Mehrdad's evil plan
- android NDK开发遇到warning:deprecated conversion from string constant to 'char *'解决方案
- TortoiseGit(乌龟git)保存用户名密码的方法
- 负数的二进制表示
- elasticsearch的基本概念
- C语言基础总结之琐碎知识点
- A. Arpa's loud Owf and Mehrdad's evil plan--java实现
- /var/run/yum.pid 已被锁定,PID 为 24455 的另一个程序正在运行解决办法
- poj1664-1
- CSS 定位详解
- C#委托与事件
- 任意进制转换(转化器,算法,类)
- HTML5中常用标签总结
- HQL构建DTO对象来来执行查询
- 计算机图形学(一)坐标变换和第一个OpenGL