Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

。。。。

Posted by keys at 2012-08-08 20:59:58 on Problem 2369
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <map>
using namespace std;
int c[1010];
bool use[1010];

__int64 gcd(__int64 a,__int64 b)
{
    return !b?a:gcd(b,a%b);
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",c+i);
    memset(use,0,sizeof(use));
    __int64 ans=1,tmp,sum;
    for(int i=1;i<=n;i++)
    {
        if(use[i]==0)
        {
            sum=1;
            int j=i;
            while(c[j]!=i)
            {
                sum++;
                use[j]=1;
                j=c[j];
            }
            use[j]=1;
            ans=ans/gcd(sum,ans)*sum;
        }
    }
    printf("%I64d\n",ans);
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator