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 ...
随机推荐
- bootstarp(carousel)组件
##### 1.5.1.Bootstrap中轮播图插件叫作Carousel ##### 1.5.2.基本的轮播图实现 ```html <!-- 以下容器就是整个轮播图组件的整体, 注意该盒子必须 ...
- PHP程序效率优化
1.在可以用file_get_contents替代file.fopen.feof.fgets等系列方法的情况下,尽量用 file_get_contents,因为他的效率高得多!但是要注意file_ge ...
- 查看MDB格式文件数据表
当打开一个MDB格式的ACCESS文件后,如果里面默认的都是窗体视图,要查看数据表的信息,可以“创建-查询设计”查看表信息,或是在SQL视图中编写SQL语句来实现. 或按着Shift键打开文件.
- C#用网易邮箱发送邮件(同步异步)
SmtpClient smtpServer = new SmtpClient("smtp.163.com"); smtpServer.Port = ; smtpServer.Cre ...
- 获取ini文件所有的Sections和Keys
获取ini文件中所有的Sections和Keys,并以pair对的方式存入到vector中 #include <iostream> #include <windows.h> # ...
- lightoj 1030
递推,倒着递推. #include<stdio.h> #define maxn 1010 #define min(a,b) (a)>(b)?(b):(a) int main() { ...
- [C#参考]Struct结构体
结构体是一种简单的用户自定义类型,也是类的一种轻量级的替代品. 相似之处:他们都有构造函数.属性.方法.字段.操作符.嵌套类型和索引器. 差异之处:类是一种引用类型,而结构体是一种值类型.因此结构体一 ...
- codeforces 659E . New Reform 强连通
题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...
- Art of Unit Test (1) - Breaking Dependency
#!/usr/bin/env python # encoding: utf-8 import unittest """ the simplyest way to test ...
- SQL Server 触发器的修改与删除
修改: alter trigger trigger_name on ..... as ..... #把create 修成 alter 就可以了. 删除: drop trigger trigger ...