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 ...
随机推荐
- c# 根据中文汉字获取到拼音
public static String ConvertToPinyin(String str) { if (String.IsNullOrEmpty(str)) return String.Empt ...
- java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils
页面报错: root: java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.util.JSONUtils 出错 ...
- PO状态为“处理中”的处理方法
EBS中经常会出现PO提交审批后状态为“处理中”的情况,此时PO创建人无法打开,审批人也无法打开,工作流等查看也无异常,可以使用一下SQL处理再进行审批: --set serveroutput on ...
- 创建SDE表空间
创建空间数据存储类型为ST_Geometry的要素类有2种方法:1)使用SDE创建要素类从9.3 开始,默认创建的要素类都使用ST_Geometry存储空间数据,9.3 版本之前,可以通过配置dbtu ...
- WTL CEdit关联绑定ID,滚动到最新的一行
绑定控件 HWND logEdit = ::GetDlgItem(this->m_hWnd, IDC_EDIT_LOG); m_outputlogEdit.Attach(logEdit); 滚动 ...
- ecside入门
ECSide是有一个基于jsp tag的开源列表组件. 简单的说,它就是一组可以帮助你快速实现强大的列表的jsp标签. 它的工作原理很简单. 您将要展现的列表的数据集合(Collection),放入r ...
- Linux网络管理——IP地址
1. 网络基础 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",&q ...
- Android 数据库ORM框架GreenDao学习心得及使用总结<一>
转: http://www.it165.net/pro/html/201401/9026.html 最近在对开发项目的性能进行优化.由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁的读 ...
- codeforces 3D . Least Cost Bracket Sequence 贪心
题目链接 给一个字符串, 由( ) 以及? 组成, 将?换成( 或者 ) 组成合法的括号序列, 每一个?换成( 或者 ) 的代价都不相同, 问你最小代价是多少, 如果不能满足输出-1. 弄一个变量nu ...
- virtualBox使用nat模式下ssh连接
virtualBox本地虚拟机通过ssh连接一般可通过桥接模式和Nat模式 桥接模式下,共享本地主机网卡,在同一个局域网之下,直接获取Ip地址就可以进行连接了. Nat模式下,获取的Ip与本地主机不是 ...