我们知道,当循环长度为L时,置换群幂次为K ,则结果是GCD(L,K)个积相乘。

于是,我们只需要求出每个循环的长度,求得它们的最小公倍数即为解。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> #define LL __int64
#define N 1000
using namespace std; int stack[N+1],top;
int num[N+1];
bool vis[N+1]; LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
} int main(){
int n,tmp;
while(scanf("%d",&n)!=EOF){
top=0;
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++){
tmp=0;
if(!vis[i]){
while(!vis[i]){
vis[i]=true;
tmp++;
i=num[i];
}
stack[++top]=tmp;
}
}
LL ans=(LL)stack[top];
for(int i=top-1;i>0;i--){
ans=ans*(LL)stack[i]/gcd(ans,(LL)stack[i]);
}
printf("%I64d\n",ans);
}
return 0;
}

  

POJ 2369的更多相关文章

  1. poj 2369 Permutations - 数论

    We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...

  2. POJ 2369 Permutations

    傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  3. poj 2369 Permutations 置换

    题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...

  4. poj 2369 Permutations 更换水称号

    寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...

  5. poj 2369 Permutations (置换入门)

    题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...

  6. POJ 2369 Permutations(置换群概念题)

    Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...

  7. poj 2369(置换群)

    Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3041   Accepted: 1641 Desc ...

  8. POJ 2369 Permutations (置换的秩P^k = I)

    题意 给定一个置换形式如,问经过几次置换可以变为恒等置换 思路 就是求k使得Pk = I. 我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数. 把一个置换转换成轮换的方法也 ...

  9. acm数学(待续)

    意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...

随机推荐

  1. DynaActionForm(动态ActionForm)的使用

    在struts中利用DynaActionForm(动态ActionForm)可以节省代码的编写. 1.在struts-config.xml中配置DynaActionForm:加入这个Form中有三个属 ...

  2. HDU 3579 线性同余方程组

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  3. JavaSE 最easy出错的几个简单的问题

    案例1. package cn.itcast.oop; public class ThisDemo { public static void main(String[] args) { Student ...

  4. [Golang] 从零開始写Socket Server(3): 对长、短连接的处理策略(模拟心跳)

    通过前两章,我们成功是写出了一套凑合能用的Server和Client,并在二者之间实现了通过协议交流.这么一来,一个简易的socket通讯框架已经初具雏形了,那么我们接下来做的.就是想办法让这个框架更 ...

  5. UI设计师不可不知的安卓屏幕知识-安卓100分享

    http://www.android100.org/html/201505/24/149342.html UI设计师不可不知的安卓屏幕知识-安卓100分享 不少设计师和工程师都被安卓设备纷繁的屏幕搞得 ...

  6. Control-of-Flow

    https://docs.microsoft.com/en-us/sql/t-sql/language-elements/control-of-flow The Transact-SQL contro ...

  7. tflearn中一些CNN RNN的例子

    lstm.py # -*- coding: utf-8 -*- """ Simple example using LSTM recurrent neural networ ...

  8. UINavi中push控制器的时候隐藏TabBar

    当一个UITabbarController管理多个UINavigationController的时候,我们又从这每一个UINavigationController中push一个ViewControll ...

  9. POJ 3134 Power Calculus ID-DFS +剪枝

    题意:给你个数n 让你求从x出发用乘除法最少多少步算出x^n. 思路: 一看数据范围 n<=1000 好了,,暴搜.. 但是 一开始写的辣鸡暴搜 样例只能过一半.. 大数据跑了10分钟才跑出来. ...

  10. C#操作IIS服务

    进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...