POJ2369 Permutations【置换群】
题目链接:
http://poj.org/problem?id=2369
题目大意:
给定一个序列。问最少须要多少次置换才干变为 1、2、…、N 的有序序列。比方说给
定5个数的序列 4 1 5 2 3。表示置换为:
( 1 2 3 4 5 ) ,即 (1 4 2)(3 5)
4 1 5 2 3
解题思路:
对于每一位找到自己轮换内轮换到自己的次数。求不相交的轮换之间的次数的公倍数,
即为终于结果。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std; int GCD(int a,int b)
{
if(b == 0)
return a;
return GCD(b,a%b);
} int LCM(int a,int b)
{
return a / GCD(a,b) * b;
} int A[1100],vis[1100];//vis[]标记轮换 int main()
{
int N;
while(~scanf("%d",&N))
{
memset(vis,0,sizeof(vis));
for(int i = 1; i <= N; ++i)
scanf("%d",&A[i]);
int Ans = 1;
for(int i = 1; i <= N; ++i)
{
int tmp = A[i];
int Num = 1;
vis[i] = 1;
while(tmp != i && !vis[tmp])
{
vis[tmp] = 1;
tmp = A[tmp];
Num++;
}
Ans = LCM(Ans,Num);
}
printf("%d\n",Ans);
} return 0;
}
POJ2369 Permutations【置换群】的更多相关文章
- poj2369 Permutations ——置换群
link:http://poj.org/problem?id=2369 置换群,最简单的那种. 找所有数字循环节的最小公倍数. /* ID: zypz4571 LANG: C++ TASK: perm ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
- POJ置换群入门[3/3]
POJ 3270 Cow Sorting 题意: 一个序列变为升序,操作为交换两个元素,代价为两元素之和,求最小代价 题解: 看了黑书... 首先循环因子分解 一个循环完成的最小代价要么是循环中最小元 ...
- acm数学(待续)
意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...
- poj1026 Cipher ——置换群
link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...
- poj 2369(置换群)
Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3041 Accepted: 1641 Desc ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Linux CentOs6.5误卸载自带python和yum后的解决办法
事故背景:前几天因项目需要,在服务器上搭建python-mysql模块,结果没安装好,于是乎想卸载重装,遂在网上查询卸载python的方法,结果一不小心直接把系统的python删了个干净....... ...
- 使用 vue + thinkjs 开发博客程序记录
一入冬懒癌发作,给自己找点事干.之前博客程序写过几次,php 的写过两次,nodejs 用 ThinkJS 写过,随着 ThinkJS 版本从1.x 升级到 2.x 之前的博客程序也做过升级.但是因为 ...
- pytorch 2 variable 变量
import torch from torch.autograd import Variable tensor = torch.FloatTensor([[1, 2], [3, 4]]) variab ...
- linux查找某个命令属于哪个rpm包
我们拿pip命令来举例 方法一: 确认命令的路径 # which pip /usr/bin/pip 用yum命令查找pip属于哪个rpm包 # yum whatprovides /usr/b ...
- Leetcode-Best Time to Buy and Sell Stock -java
题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】
[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with ...
- zzulioj--1825-- 会长爱数学(模拟)
1825: 会长爱数学 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 6 Solved: 2 SubmitStatusWeb Board Descr ...
- Lists and strings
A string is a sequence of characters and a list is a sequence of values, but a list of characters is ...
- 5.QT制作编译器,可以简单支持中文编程
学习了文件操作,那么先做一个自制的IDE吧,就是简单的读取,修改,保存文件,使用QT语言,附上github的代码:QT基本文件操作实现中文编程 截图: 运行效果
- Spring Boot AutoConfiguration注解@ConditionalXXXX之前生今世
1.注解@Conditional的定义 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHO ...