2011: Permutation

2011: Permutation


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
5s8192K749218Standard

There are a group of strange people live in a strange island, where every morning each people gives his shoes(the unique) to his best friend(also the unique). Different people have different best friends. After a certain amount of days, they finally find that all the shoes come to their original owners. But they forget how many days have passed, so they ask Mr. Jojer, the most clever programmer, to find the least possible number of days.

Input and Output

The input may contain several test cases. Each test case contains an integer N(<30000) indicating the number of people(numbered by 1,2,…N) lived in the island. The following line of each test case contains N integers(range from 1 to N), the i th of which indicates the best friend of the person who is numbered by i.

For each test case, you should print the least possible number of days in a single line.

Sample Input

2
2 1

Sample Output

2
#include<stdio.h>
int gcd(int x,int y)
{
    if(y==0) return x;
    return gcd(y,x%y);
}
int main()
{
    int f[30000];
    int n,i,j;
    while(scanf("%d",&n)==1)
    {
        int b[30000]={0},t=1;
        for(i=1;i<=n;i++) scanf("%d",&f[i]);
        for(i=1;i<=n;i++)
        {
            if(b[i]==0)
            {
                int ci=0;j=i;
                while(b[j]==0)
                {
                    ci++;
                    b[j]=1;
                    b[j]=1;
                    j=f[j];
                }
                t=t*ci/gcd(t,ci);
            }
        }
        printf("%d/n",t);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值