Codeforces Round #383 (Div. 2)C.Arpa's loud Owf and Mehrdad's evil plan【思维+LCM】
来源:互联网 发布:淘宝垃圾短信 编辑:程序博客网 时间:2024/05/14 14:40
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).
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.
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 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.
题目大意:
一共有N个数,对应需要找一个最小周期t,使得每一个x都满足:
f【f【f【x】】】(t次)=y
f【f【f【y】】】(t次)=x
思路:
1、因为n最大才100,那么我们暴力处理每个元素回到本身这个数的时候需要用的次数,记做t【i】;因为我们要最小的周期,使得其满足所有数值都满足这个条件。那么我们需要求t【i】的LCM(最小公倍数);
2、考虑到寻求最小,那么如果t【i】是偶数,我们可以对其进行除2的操作。剩下的部分就是求LCM了。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;#define ll __int64ll gcd(ll x,ll y){ if(x%y==0)return y; else return gcd(y,x%y);}int a[500];ll t[500];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } int flag2=0; for(int i=1;i<=n;i++) { int sum=0; int flag=0; int cnt=0; int now=i; while(1) { cnt++; now=a[now]; if(now==i) { flag=1; break; } if(cnt>10000)break; } if(flag==0) { flag2=1; break; } t[i]=(ll)cnt; if(t[i]%2==0)t[i]=t[i]/2; } if(flag2==1)printf("-1\n"); else { ll output=t[1]; for(int i=2;i<=n;i++) { ll tmp=gcd(output,t[i]); output=output/tmp*t[i]; } printf("%I64d\n",output); } }}
- Codeforces Round #383 (Div. 2)C.Arpa's loud Owf and Mehrdad's evil plan【思维+LCM】
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan(dfs&lcm)
- 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) 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 #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 dfs+最小公倍数
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
- Codeforces Round #383 (Div. 1) 741A Arpa's loud Owf and Mehrdad's evil plan
- Codeforces Round#383C. 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 741A Arpa's loud Owf and Mehrdad's evil plan(思维)
- 【Codeforces 741A. 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
- mac系统下使用git,包括给github上传文件
- 深入理解 Laravel Eloquent(三)——模型间关系(关联)
- 大数据管理系统-工程日志
- 人文学科 问题积累
- DroidPlugin代码分析(四) 进程管理
- Codeforces Round #383 (Div. 2)C.Arpa's loud Owf and Mehrdad's evil plan【思维+LCM】
- 筛选菜单
- linux命令大全——文档编辑
- markDown学习笔记
- 【9931】火车票
- Eclipse C++出现Launch failed,Binary not found问题安装MinGW编译器解决方案(Win7)
- 从指定文件(字节数组)获取内容以及获取长度
- 大数加法
- 前端技术-布局解决方案