HihoCoder
来源:互联网 发布:小米note双卡设置网络 编辑:程序博客网 时间:2024/06/03 15:45
题目链接:https://hihocoder.com/problemset/problem/1330
1330 : Array Rearrangement
Time Limit:10000ms
Case Time Limit:1000ms
Memory Limit:256MB
Description
Little Ho has written an array-shuffle program. It will shuffle an input array by some certain rearrangement order. Little Hi wants to know that after how many times of shuffle the array will come back to its original order?
More precisely, given a permutation P of 1 - N, the program shuffles the array by putting the i-th element into the Pi-th position. Assuming P = (2, 3, 1) and the initial array is (1, 2, 3), after the 1st shuffle it becomes (3, 1, 2), after the 2rd shuffle it becomes (2, 3, 1) and after the 3rd shuffle it comes back to (1, 2, 3).
You may assume the elements of input array are distinct.
Input
The first line contains an integer N, the length of the array. (1 ≤ N ≤ 100)
The second line contains the permutation P which consists of N integers.
Output
Output the number of shuffles.
Sample Input
3
2 3 1
Sample Output
3
题意:给定一个长度n,按照P数组的序列调整,求回到原始序列的最小步骤。
思路:对于每一个点分别通过模拟得出最小步骤,再求每个最小步骤的最小公倍数。开始的时候,直接就求了最大值,没有考虑到不同步的问题。还有因为是求最小公倍数,记得开long long 。
在这儿用%I64d会PE。
附上ac代码
#include<stdio.h>typedef long long ll;ll a[105];ll GCD(ll a, ll b){ if(a % b == 0) return b; return GCD(b, a % b);}ll LCM(ll a, ll b){ return a * b / GCD(a, b);}int main(){ int n; while(scanf("%lld", &n) != EOF){ for(ll i = 1; i <= n; i++) scanf("%lld", &a[i]); ll ans = 1; //模拟 for(ll i = 1; i <= n; i++){ ll t = a[i]; ll cnt = 1; while(t != i){ t = a[t]; cnt++; } ans = LCM(ans, cnt); } printf("%lld\n",ans); } return 0;}
- hihocoder:
- hihoCoder
- Hihocoder
- hihocoder
- hihocoder
- hihocoder
- hihocoder
- hihocoder
- HihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- hihoCoder
- HihoCoder
- HihoCoder
- HihoCoder
- 串口下载代码CH340驱动安装
- 感知器
- linux设备驱动模型--class
- bzoj4952 [Wf2017]Need for Speed(二分答案)
- CSS-同一个li下,图片和文字一起,如何使得全部垂直居中
- HihoCoder
- Unity入门学习 //07_Profiler游戏监察器(游戏性能分析器)
- VC++ ado事务处理
- Windows CMD常用命令
- struct union数据对齐和sizeof大小
- MySQL 覆盖索引
- 是否是同一棵二叉搜索树——不需要真正建树的版本
- 华为机试-明明的随机数
- Codeforces Round #431 (Div. 2) 题解