标签:io for sp amp on c as poj r
//问最少置换多少次变成有序序列 //每个位置都有个循环节 求全部位置循环节的最小公倍数 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int gcd(int x,int y) { if(y==0) return x; return gcd(y,x%y); } int lcm(int x,int y) { return x/gcd(x,y)*y;//先除后乘,不会超 } int main() { int n,i,cot,t; int p[1010],p1[1010]; while(~scanf("%d",&n)) { for(i=1; i<=n; i++) scanf("%d",&p[i]); for(i=1; i<=n; i++) { cot=1; t=p[i]; while(t!=i) { cot++; t=p[t]; } p1[i]=cot; } for(i=2; i<=n; i++) //最小公倍数 { p1[i]=lcm(p1[i],p1[i-1]); } printf("%d\n",p1[n]); } return 0; }
标签:io for sp amp on c as poj r
原文地址:http://blog.csdn.net/lp_opai/article/details/39007175