POJ 2369
我们知道,当循环长度为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的更多相关文章
- poj 2369 Permutations - 数论
We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...
- POJ 2369 Permutations
傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- poj 2369 Permutations 置换
题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- poj 2369 Permutations (置换入门)
题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- poj 2369(置换群)
Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3041 Accepted: 1641 Desc ...
- POJ 2369 Permutations (置换的秩P^k = I)
题意 给定一个置换形式如,问经过几次置换可以变为恒等置换 思路 就是求k使得Pk = I. 我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数. 把一个置换转换成轮换的方法也 ...
- acm数学(待续)
意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...
随机推荐
- DesignPattern_Java:Factory Method Pattern
工厂方法模式 Factory Method :(虚拟构造函数模式 Virtual Constructor,多态性工厂模式 Ploymorphic Facoty) Define an interface ...
- 精简Linux文件路径
精简Linux的文件路径: ..回退的功能 .留在当前文件夹 //仅仅保留一个/ abc/..要返回. 报错 删除最后一个/ 主要思路: 用栈记录路径的起始位置,讨论/后的不同情况就可以: #incl ...
- Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数
E. Beautiful Subarrays One day, ZS the Coder wrote down an array of integers a with elements a1, ...
- 9.9递归和动态规划(八)——给定数量不限的硬币,币值为25分,10分,5分,1分,计算n分有几种表示法
/** * 功能:给定数量不限的硬币.币值为25分,10分.5分.1分,计算n分有几种表示法. */ public static int makeChange(int n){ return make ...
- php面向对象之__isset和__unset
php面向对象之__isset和__unset 一.简介 __isset和__unset都是对不可访问属性的操作,前者是检验的时候自动调用,后者是销毁的时候自动调用. 比如说在类外访问private的 ...
- svn是什么
svn是什么 SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Sub ...
- hdoj--3440--House Man(差分约束)
House Man Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 杂项:CMM
ylbtech-杂项:CMM CMM是指“能力成熟度模型”,其英文全称为Capability Maturity Model for Software,英文缩写为SW-CMM,简称CMM.它是对于软件组 ...
- java多线程 interrupt(), interrupted(), isInterrupted()方法区别
interrupt()方法: 作用是中断线程. 本线程中断自身是被允许的,且"中断标记"设置为true 其它线程调用本线程的interrupt()方法时,会通过checkAcces ...
- sql语句获取本周、本月、本年数据
本周:select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段本月:select * ...