hiho week 167 register

Ended

Participants:334

Verdict:Accepted
Score:100 / 100
Submitted:2017-09-12 21:08:20

Lang:G++

Edit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
using namespace std;
int gcd(int a, int b) {
    int buf;
    while (b){
        buf = b;
        b = a % b;
        a = buf;
    }
    return a;
}
int lcm(int a,int b) {
    return a/gcd(a,b)*b;
}
int  main(){
    int N;
    int a[110];
    cin >> N;
    for (int i = 1; i <= N; i++)
        cin >> a[i];
    int ans = 1,p,len;
    for (int i = 1; i <= N; i++){
        p = a[i];
        len = 1;
        while (p != i){
            p = a[p];
            len++;
        }
        ans = lcm(len, ans);
    }
    cout << ans << endl;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX