Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
。。。。#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator