poj 2369 Permutations 置换
给一个数列, 求这个数列置换成1, 2, 3....n需要多少次。
就是里面所有小的置换的长度的lcm。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int a[], vis[];
int gcd(int x, int y) {
return y?gcd(y, x%y):x;
}
int lcm(int x, int y) {
return x/gcd(x, y)*y;
}
int main()
{
int n;
while(cin>>n) {
for(int i = ; i<n; i++){
scanf("%d", &a[i]);
a[i]--;
}
int ans = ;
for(int i = ; i<n; i++) {
if(!vis[i]) {
int tmp = i, len = ;
while(!vis[tmp]) {
vis[tmp] = ;
len++;
tmp = a[tmp];
}
ans = lcm(ans, len);
}
}
cout<<ans<<endl;
}
return ;
}
poj 2369 Permutations 置换的更多相关文章
- poj 2369 Permutations (置换入门)
题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...
- POJ 2369 Permutations (置换的秩P^k = I)
题意 给定一个置换形式如,问经过几次置换可以变为恒等置换 思路 就是求k使得Pk = I. 我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数. 把一个置换转换成轮换的方法也 ...
- 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(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- POJ 2369 Permutations
傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- UVA - 11077 Find the Permutations (置换)
Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...
随机推荐
- css黑魔法
多行文本溢出显示省略号(...)的方法 p { overflow : hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-li ...
- 查找EBS中各种文件版本(Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER)
Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER (文档 ID 85895 ...
- Mysql中表名作为参数的问题
近期由于程序的异常,导致数据库中创建了大量的表(约4000个),纠结的是表中的数据还都是有用的. 需要合并到一个表中,首先想到的就是使用存储过程来处理,但由于表名都是动态生成的,需要解决在存储过程中处 ...
- spring 源码之 ioc 容器的初始化和注入简图
IoC最核心就是两个过程:IoC容器初始化和IoC依赖注入,下面通过简单的图示来表述其中的关键过程:
- [置顶] boost使用(六)
今天继续带来boost关于线程锁的一些使用,今天我使用一个更复杂的锁来控制同步,先来段简单的代码 #include <boost/thread.hpp> #include <iost ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
- Python调用C/C++的种种方法
Python调用C/C++的种种方法 2010-12-07 09:59 28433人阅读 评论(1) 收藏 Python是解释性语言, 底层就是用c实现的, 所以用python调用C是很容易的, 下面 ...
- 玉兔IM(康林的博客)
玉兔即时通信 作者:康林(msn.email:kl222@126.com:QQ:16614119) 博客:http://blog.csdn.net/kl222http://blog.csdn.net/ ...
- 如何显示Mac OS X上的隐藏文件和文件夹
显示隐藏文件以及文件夹命令: defaults write com.apple.finder AppleShowAllFiles YES Mac显示隐藏文件 对于OS X Mavericks 10.9 ...
- SICP 练习 (2.9)解决摘要:宽度和区间运算的关系间隔
SICP 2.9 像是一个数学题,要我们证明区间的和与差的宽度是被加和被减的区间的宽度的函数,而对于乘法和除法来说不成立. 书中所谓宽度就是区间起点和终点差的一半.以我看来更像是区间宽度的一半.无论怎 ...